1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.misc;
17
18 import james.core.data.DBConnectionData;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.text.DecimalFormat;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35
36 import org.w3c.dom.Document;
37
38 import p3j.simulation.ExecutionMode;
39
40
41
42
43
44
45
46
47
48
49 public final class Misc {
50
51
52
53
54
55
56 private static final String DELIMITER_OF_HIBERNATE_TYPES = "$$";
57
58
59 public static final int BASE_NUM = 10;
60
61
62 public static final DecimalFormat NUMBER_FORMAT = new DecimalFormat(
63 "0.00000000");
64
65
66 public static final double EPSILON = 0.0000000001;
67
68
69 public static final String CONFIG_FILE = "conf/config.xml";
70
71
72 public static final String DEFAULT_DB_URL = "jdbc:hsqldb:file:./pppm_db";
73
74
75 public static final String DEFAULT_DB_USER = "root";
76
77
78 public static final String DEFAULT_DB_PWD = "";
79
80
81 public static final ExecutionMode DEFAULT_EXEC_MODE = ExecutionMode.MONTE_CARLO;
82
83
84 public static final DBConnectionData DEFAULT_DB_CONN = new DBConnectionData(
85 DEFAULT_DB_URL, DEFAULT_DB_USER, DEFAULT_DB_PWD, null);
86
87
88 public static final String DEFAULT_HIBERNATE_CONFIG_FILE = "conf/hibernate.cfg.xml";
89
90
91 public static final String TEST_HIBERNATE_CONFIG_FILE = "test.hibernate.cfg.xml";
92
93
94 public static final int DEFAULT_NUM_TRIALS = 1000;
95
96
97 public static final int DEFAULT_NUM_PARALLEL_THREADS = 1;
98
99
100
101
102 public static final String PREF_DB_URL = "Database URL";
103
104
105 public static final String PREF_DB_USER = "Database User Name";
106
107
108 public static final String PREF_DB_PWD = "Database Password";
109
110
111 public static final String PREF_NUM_TRIALS = "Number of Trials";
112
113
114 public static final String PREF_NUM_PARALLEL_THREADS = "Parallel Threads (Monte-Carlo)";
115
116
117 public static final String PREF_EXECUTION_MODE = "Execution Mode";
118
119
120
121
122 public static final String GUI_LABEL_PROBABILITY = "Probability (in [0,1]):";
123
124
125 public static final String GUI_LABEL_NAME = "Name:";
126
127
128 public static final String GUI_LABEL_DESCRIPTION = "Description:";
129
130
131 public static final String GUI_LABEL_DEVIATION = "Standard deviation";
132
133
134 public static final String GUI_LABEL_JUMP_OFF_YEAR = "Jump-Off Year";
135
136
137 public static final String GUI_LABEL_NUM_AGE_CLASSES = "Number of Age Classes:";
138
139
140 public static final String GUI_LABEL_DESCENDANT_GENERATIONS = "Descendant Generations:";
141
142
143 public static final String GUI_LABEL_PROJECTION_HORIZON = "Projection Horizon:";
144
145
146
147
148 private Misc() {
149 }
150
151
152
153
154
155
156
157
158
159 public static String getFileEnding(File file) {
160 return file.getName().substring(file.getName().lastIndexOf('.') + 1);
161 }
162
163
164
165
166
167
168
169
170
171 public static double parseToDouble(Object value) {
172 return Double.parseDouble(value.toString().replace(',', '.'));
173 }
174
175
176
177
178
179
180
181
182
183 public static double parseToDoubleProb(String probString) {
184 double probability = parseToDouble(probString);
185 if (probability < 0 || probability > 1) {
186 return 0;
187 }
188 return probability;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202 public static boolean numEqual(double x, double y) {
203 return Math.abs(x - y) < EPSILON;
204 }
205
206
207
208
209
210
211
212
213
214 public static int parseToInt(String numString) {
215 return Integer.parseInt(numString);
216 }
217
218
219
220
221
222
223
224
225
226
227
228 public static double round(double value, int digits) {
229 return ((int) (value * Math.pow(BASE_NUM, digits)) / Math.pow(BASE_NUM,
230 digits));
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 @Deprecated
247 public static void writeDocumentToFile(String fileName, Document document)
248 throws FileNotFoundException, TransformerException {
249
250 File f = new File(fileName);
251
252 FileOutputStream fop = new FileOutputStream(f);
253
254
255 TransformerFactory tff = TransformerFactory.newInstance();
256 Transformer tf = tff.newTransformer();
257
258 tf.setOutputProperty(OutputKeys.METHOD, "xml");
259 tf.setOutputProperty(OutputKeys.INDENT, "yes");
260
261 DOMSource source = new DOMSource(document);
262 StreamResult result = new StreamResult(fop);
263
264 tf.transform(source, result);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 @SuppressWarnings("unchecked")
281 public static <X> X autoCast(Object o) {
282 return (X) o;
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public static boolean checkClassEquality(Class<?> class1, Class<?> class2) {
298 return getCleanedClassName(class1).compareTo(getCleanedClassName(class2)) == 0;
299 }
300
301
302
303
304
305
306
307
308
309
310
311 public static String getCleanedClassName(Class<?> theClass) {
312 String name = theClass.getCanonicalName();
313 int cutOffIndex = name.indexOf(DELIMITER_OF_HIBERNATE_TYPES);
314 return cutOffIndex < 0 ? name : name.substring(0, cutOffIndex);
315 }
316
317
318
319
320
321
322
323
324
325
326 public static <D> List<D> mergeList(List<D>... listsToMerge) {
327 List<D> result = new ArrayList<D>();
328 for (List<D> listToMerge : listsToMerge) {
329 result.addAll(listToMerge);
330 }
331 return result;
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public static <X, Y> void invertMap(Map<X, Y> src, Map<Y, X> dest) {
348 dest.clear();
349 for (Entry<X, Y> entry : src.entrySet()) {
350 if (dest.containsKey(entry.getValue())) {
351 throw new IllegalArgumentException("Passed map contains value '"
352 + entry + "' twice, cannot be inverted.");
353 }
354 dest.put(entry.getValue(), entry.getKey());
355 }
356 }
357
358 }