1 /*
2
3 dsh-venn-tools Command line tools for venn diagrams.
4 Copyright (c) 2010-2013 held jointly by the individual authors.
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
20 > http://www.fsf.org/licensing/licenses/lgpl.html
21 > http://www.opensource.org/licenses/lgpl-license.php
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 * Abstract venn runnable.
42 *
43 * @author Michael Heuer
44 * @version $Revision$ $Date$
45 */
46 abstract class AbstractVennRunnable
47 implements Runnable
48 {
49 /** True to output count(s) only. */
50 private final boolean count;
51
52 /** True to output header(s). */
53 private final boolean header;
54
55 /** Marker file, write to stdout. */
56 protected static final File STDOUT = new File(".");
57
58
59 /**
60 * Create a new abstract venn runnable with the specified arguments.
61 *
62 * @param count true to output count(s) only
63 * @param header true to ouput header(s)
64 */
65 protected AbstractVennRunnable(final boolean count, final boolean header)
66 {
67 this.count = count;
68 this.header = header;
69 }
70
71
72 /**
73 * Return true to output count(s) only.
74 *
75 * @return true to output count(s) only
76 */
77 protected final boolean count()
78 {
79 return count;
80 }
81
82 /**
83 * Return true to output header(s).
84 *
85 * @return true to output header(s)
86 */
87 protected final boolean header()
88 {
89 return header;
90 }
91
92 /**
93 * Write the specified label text to stdout.
94 *
95 * @param write true to write
96 * @param labelText label text to write
97 * @param stdout stdout
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 * Write the specified size to stdout.
110 *
111 * @param write true to write
112 * @param size size to write
113 * @param stdout stdout
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 * Write one value from the specified iterator to stdout.
126 *
127 * @param write true to write
128 * @param it iterator to write from
129 * @param stdout stdout
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 * Read a set of strings from the specified input file.
145 *
146 * @param input input file
147 * @return a set of strings
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 //throw new IllegalArgumentException("could not read input file " + input, e); // jdk 1.6+
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 // ignore
175 }
176 }
177 return result;
178 }
179
180 /**
181 * Write the specified set view to the specified file if valid.
182 *
183 * @param headerText header text
184 * @param view view
185 * @param file file
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 //throw new IllegalArgumentException("could not write to output file " + file, e); // jdk 1.6+
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 // ignore
227 }
228 }
229 }
230
231 /**
232 * Default to the specified default value if the argument was found and has a null
233 * value or if the value matches either of <code>value0</code> or <code>value1</code>.
234 *
235 * @param <T> argument type
236 * @param argument argument
237 * @param value0 first value
238 * @param value1 second value
239 * @param defaultValue default value
240 * @return the specified default value if the argument was found and has a null
241 * value or if the value matches either of <code>value0</code> or <code>value1</code>
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 * Default to the specified default value if the argument was found and has a null
265 * value or if the value matches either of <code>value0</code>, <code>value1</code>,
266 * or <code>value2</code>.
267 *
268 * @param <T> argument type
269 * @param argument argument
270 * @param value0 first value
271 * @param value1 second value
272 * @param value2 third value
273 * @param defaultValue default value
274 * @return the specified default value if the argument was found and has a null
275 * value or if the value matches either of <code>value0</code>, <code>value1</code>,
276 * or <code>value2</code>
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 * Default to the specified default value if the argument was found and has a null
300 * value or if the value matches either of <code>value0</code>, <code>value1</code>,
301 * <code>value2</code>, or <code>value3</code>.
302 *
303 * @param <T> argument type
304 * @param argument argument
305 * @param value0 first value
306 * @param value1 second value
307 * @param value2 third value
308 * @param value3 fourth value
309 * @param defaultValue default value
310 * @return the specified default value if the argument was found and has a null
311 * value or if the value matches either of <code>value0</code>, <code>value1</code>,
312 * <code>value2</code>, or <code>value3</code>
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 }