1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math;
18
19 import java.io.EOFException;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.text.MessageFormat;
24 import java.text.ParseException;
25 import java.util.ConcurrentModificationException;
26 import java.util.Locale;
27 import java.util.MissingResourceException;
28 import java.util.NoSuchElementException;
29 import java.util.ResourceBundle;
30
31
32
33
34
35
36
37 public class MathRuntimeException extends RuntimeException {
38
39
40 private static final long serialVersionUID = -5128983364075381060L;
41
42
43
44
45 private final String pattern;
46
47
48
49
50 private final Object[] arguments;
51
52
53
54
55
56
57
58
59 private static String translate(final String s, final Locale locale) {
60 try {
61 ResourceBundle bundle =
62 ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale);
63 if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
64
65 return bundle.getString(s);
66 }
67
68 } catch (MissingResourceException mre) {
69
70 }
71
72
73
74 return s;
75
76 }
77
78
79
80
81
82
83
84
85 private static String buildMessage(final Locale locale, final String pattern,
86 final Object ... arguments) {
87 return (pattern == null) ? "" : new MessageFormat(translate(pattern, locale), locale).format(arguments);
88 }
89
90
91
92
93
94
95
96
97 public MathRuntimeException(final String pattern, final Object ... arguments) {
98 super(buildMessage(Locale.US, pattern, arguments));
99 this.pattern = pattern;
100 this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
101 }
102
103
104
105
106
107
108
109
110 public MathRuntimeException(final Throwable rootCause) {
111 super(rootCause);
112 this.pattern = getMessage();
113 this.arguments = new Object[0];
114 }
115
116
117
118
119
120
121
122
123
124
125 public MathRuntimeException(final Throwable rootCause,
126 final String pattern, final Object ... arguments) {
127 super(buildMessage(Locale.US, pattern, arguments), rootCause);
128 this.pattern = pattern;
129 this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
130 }
131
132
133
134
135
136 public String getPattern() {
137 return pattern;
138 }
139
140
141
142
143
144 public Object[] getArguments() {
145 return arguments.clone();
146 }
147
148
149
150
151
152
153
154 public String getMessage(final Locale locale) {
155 return buildMessage(locale, pattern, arguments);
156 }
157
158
159 @Override
160 public String getLocalizedMessage() {
161 return getMessage(Locale.getDefault());
162 }
163
164
165
166
167 @Override
168 public void printStackTrace() {
169 printStackTrace(System.err);
170 }
171
172
173
174
175
176
177 @Override
178 public void printStackTrace(final PrintStream out) {
179 synchronized (out) {
180 PrintWriter pw = new PrintWriter(out, false);
181 printStackTrace(pw);
182
183 pw.flush();
184 }
185 }
186
187
188
189
190
191
192
193
194 public static ArithmeticException createArithmeticException(final String pattern,
195 final Object ... arguments) {
196 return new ArithmeticException(buildMessage(Locale.US, pattern, arguments)) {
197
198
199 private static final long serialVersionUID = 7705628723242533939L;
200
201
202 @Override
203 public String getLocalizedMessage() {
204 return buildMessage(Locale.getDefault(), pattern, arguments);
205 }
206
207 };
208 }
209
210
211
212
213
214
215
216
217 public static ArrayIndexOutOfBoundsException createArrayIndexOutOfBoundsException(final String pattern,
218 final Object ... arguments) {
219 return new ArrayIndexOutOfBoundsException(buildMessage(Locale.US, pattern, arguments)) {
220
221
222 private static final long serialVersionUID = -3394748305449283486L;
223
224
225 @Override
226 public String getLocalizedMessage() {
227 return buildMessage(Locale.getDefault(), pattern, arguments);
228 }
229
230 };
231 }
232
233
234
235
236
237
238
239
240 public static EOFException createEOFException(final String pattern,
241 final Object ... arguments) {
242 return new EOFException(buildMessage(Locale.US, pattern, arguments)) {
243
244
245 private static final long serialVersionUID = 279461544586092584L;
246
247
248 @Override
249 public String getLocalizedMessage() {
250 return buildMessage(Locale.getDefault(), pattern, arguments);
251 }
252
253 };
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267 public static IOException createIOException(final Throwable rootCause) {
268 IOException ioe = new IOException(rootCause.getLocalizedMessage());
269 ioe.initCause(rootCause);
270 return ioe;
271 }
272
273
274
275
276
277
278
279
280 public static IllegalArgumentException createIllegalArgumentException(final String pattern,
281 final Object ... arguments) {
282 return new IllegalArgumentException(buildMessage(Locale.US, pattern, arguments)) {
283
284
285 private static final long serialVersionUID = -6555453980658317913L;
286
287
288 @Override
289 public String getLocalizedMessage() {
290 return buildMessage(Locale.getDefault(), pattern, arguments);
291 }
292
293 };
294 }
295
296
297
298
299
300
301
302
303 public static IllegalArgumentException createIllegalArgumentException(final Throwable rootCause) {
304 IllegalArgumentException iae = new IllegalArgumentException(rootCause.getLocalizedMessage());
305 iae.initCause(rootCause);
306 return iae;
307 }
308
309
310
311
312
313
314
315
316 public static IllegalStateException createIllegalStateException(final String pattern,
317 final Object ... arguments) {
318 return new IllegalStateException(buildMessage(Locale.US, pattern, arguments)) {
319
320
321 private static final long serialVersionUID = -95247648156277208L;
322
323
324 @Override
325 public String getLocalizedMessage() {
326 return buildMessage(Locale.getDefault(), pattern, arguments);
327 }
328
329 };
330 }
331
332
333
334
335
336
337
338
339 public static ConcurrentModificationException createConcurrentModificationException(final String pattern,
340 final Object ... arguments) {
341 return new ConcurrentModificationException(buildMessage(Locale.US, pattern, arguments)) {
342
343
344 private static final long serialVersionUID = 6134247282754009421L;
345
346
347 @Override
348 public String getLocalizedMessage() {
349 return buildMessage(Locale.getDefault(), pattern, arguments);
350 }
351
352 };
353 }
354
355
356
357
358
359
360
361
362 public static NoSuchElementException createNoSuchElementException(final String pattern,
363 final Object ... arguments) {
364 return new NoSuchElementException(buildMessage(Locale.US, pattern, arguments)) {
365
366
367 private static final long serialVersionUID = 7304273322489425799L;
368
369
370 @Override
371 public String getLocalizedMessage() {
372 return buildMessage(Locale.getDefault(), pattern, arguments);
373 }
374
375 };
376 }
377
378
379
380
381
382
383
384
385 public static NullPointerException createNullPointerException(final String pattern,
386 final Object ... arguments) {
387 return new NullPointerException(buildMessage(Locale.US, pattern, arguments)) {
388
389
390 private static final long serialVersionUID = -3075660477939965216L;
391
392
393 @Override
394 public String getLocalizedMessage() {
395 return buildMessage(Locale.getDefault(), pattern, arguments);
396 }
397
398 };
399 }
400
401
402
403
404
405
406
407
408
409
410 public static ParseException createParseException(final int offset,
411 final String pattern,
412 final Object ... arguments) {
413 return new ParseException(buildMessage(Locale.US, pattern, arguments), offset) {
414
415
416 private static final long serialVersionUID = -1103502177342465975L;
417
418
419 @Override
420 public String getLocalizedMessage() {
421 return buildMessage(Locale.getDefault(), pattern, arguments);
422 }
423
424 };
425 }
426
427
428
429
430
431 public static RuntimeException createInternalError(final Throwable cause) {
432
433 final String pattern = "internal error, please fill a bug report at {0}";
434 final String argument = "https://issues.apache.org/jira/browse/MATH";
435
436 return new RuntimeException(buildMessage(Locale.US, pattern, argument)) {
437
438
439 private static final long serialVersionUID = -201865440834027016L;
440
441
442 @Override
443 public String getLocalizedMessage() {
444 return buildMessage(Locale.getDefault(), pattern, argument);
445 }
446
447 };
448
449 }
450
451 }