1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.venn.tools;
25
26 import java.io.BufferedReader;
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.Set;
37
38 import org.dishevelled.commandline.Argument;
39
40
41
42
43
44
45
46 abstract class AbstractVennRunnable
47 implements Runnable
48 {
49
50 private final boolean count;
51
52
53 private final boolean header;
54
55
56 protected static final File STDOUT = new File(".");
57
58
59
60
61
62
63
64
65 protected AbstractVennRunnable(final boolean count, final boolean header)
66 {
67 this.count = count;
68 this.header = header;
69 }
70
71
72
73
74
75
76
77 protected final boolean count()
78 {
79 return count;
80 }
81
82
83
84
85
86
87 protected final boolean header()
88 {
89 return header;
90 }
91
92
93
94
95
96
97
98
99 protected static final void write(final boolean write, final String labelText, final PrintWriter stdout)
100 {
101 if (write)
102 {
103 stdout.print(labelText);
104 stdout.print("\t");
105 }
106 }
107
108
109
110
111
112
113
114
115 protected static final void write(final boolean write, final int size, final PrintWriter stdout)
116 {
117 if (write)
118 {
119 stdout.print(size);
120 stdout.print("\t");
121 }
122 }
123
124
125
126
127
128
129
130
131 protected static final void write(final boolean write, final Iterator<String> it, final PrintWriter stdout)
132 {
133 if (write)
134 {
135 if (it.hasNext())
136 {
137 stdout.print(it.next());
138 }
139 stdout.print("\t");
140 }
141 }
142
143
144
145
146
147
148
149 protected static final Set<String> read(final File input)
150 {
151 BufferedReader reader = null;
152 Set<String> result = new HashSet<String>(Math.max(16, (int) input.length() / 64));
153 try
154 {
155 reader = new BufferedReader(new FileReader(input));
156 while (reader.ready())
157 {
158 result.add(reader.readLine().trim());
159 }
160 }
161 catch (IOException e)
162 {
163
164 throw new IllegalArgumentException("could not read input file " + input + ", " + e.getMessage());
165 }
166 finally
167 {
168 try
169 {
170 reader.close();
171 }
172 catch (Exception e)
173 {
174
175 }
176 }
177 return result;
178 }
179
180
181
182
183
184
185
186
187 protected void write(final String headerText, final Set<String> view, final File file)
188 {
189 if (file == null || STDOUT.equals(file))
190 {
191 return;
192 }
193 PrintWriter writer = null;
194 try
195 {
196 writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
197 if (header)
198 {
199 writer.println(headerText);
200 }
201 if (count)
202 {
203 writer.println(view.size());
204 }
205 else
206 {
207 for (String s : view)
208 {
209 writer.println(s);
210 }
211 }
212 }
213 catch (IOException e)
214 {
215
216 throw new IllegalArgumentException("could not write to output file " + file + ", " + e.getMessage());
217 }
218 finally
219 {
220 try
221 {
222 writer.close();
223 }
224 catch (Exception e)
225 {
226
227 }
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243 protected static final <T> T defaultIfFound(final Argument<T> argument, final T value0, final T value1, final T defaultValue)
244 {
245 if (argument.wasFound())
246 {
247 if (argument.getValue() == null)
248 {
249 return defaultValue;
250 }
251 else
252 {
253 if (value0.equals(argument.getValue()) || value1.equals(argument.getValue()))
254 {
255 return defaultValue;
256 }
257 return argument.getValue();
258 }
259 }
260 return null;
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 protected static final <T> T defaultIfFound(final Argument<T> argument, final T value0, final T value1, final T value2, final T defaultValue)
279 {
280 if (argument.wasFound())
281 {
282 if (argument.getValue() == null)
283 {
284 return defaultValue;
285 }
286 else
287 {
288 if (value0.equals(argument.getValue()) || value1.equals(argument.getValue()) || value2.equals(argument.getValue()))
289 {
290 return defaultValue;
291 }
292 return argument.getValue();
293 }
294 }
295 return null;
296 }
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 protected static final <T> T defaultIfFound(final Argument<T> argument, final T value0, final T value1, final T value2, final T value3, final T defaultValue)
315 {
316 if (argument.wasFound())
317 {
318 if (argument.getValue() == null)
319 {
320 return defaultValue;
321 }
322 else
323 {
324 if (value0.equals(argument.getValue()) || value1.equals(argument.getValue()) || value2.equals(argument.getValue()) || value3.equals(argument.getValue()))
325 {
326 return defaultValue;
327 }
328 return argument.getValue();
329 }
330 }
331 return null;
332 }
333 }