1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.exception;
23
24 import java.text.MessageFormat;
25 import java.util.Locale;
26
27
28
29
30 public class MathRuntimeException extends RuntimeException implements LocalizedException {
31
32
33 private static final long serialVersionUID = 20160217L;
34
35
36 private static final String REPORT_URL = "https://github.com/Hipparchus-Math/hipparchus/issues";
37
38
39 private final Localizable specifier;
40
41
42 private final Object[] parts;
43
44
45
46
47
48 public MathRuntimeException(final Localizable specifier, final Object ... parts) {
49 this.specifier = specifier;
50 this.parts = (parts == null) ? new Object[0] : parts.clone();
51 }
52
53
54
55
56
57
58 public MathRuntimeException(final Throwable cause, final Localizable specifier,
59 final Object ... parts) {
60 super(cause);
61 this.specifier = specifier;
62 this.parts = (parts == null) ? new Object[0] : parts.clone();
63 }
64
65
66
67
68 public static MathRuntimeException createInternalError() {
69 return new MathRuntimeException(LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
70 }
71
72
73
74
75
76
77 public static MathRuntimeException createInternalError(final Throwable cause) {
78 return new MathRuntimeException(cause, LocalizedCoreFormats.INTERNAL_ERROR, REPORT_URL);
79 }
80
81
82 @Override
83 public String getMessage(final Locale locale) {
84 return buildMessage(locale);
85 }
86
87
88 @Override
89 public String getMessage() {
90 return getMessage(Locale.US);
91 }
92
93
94 @Override
95 public String getLocalizedMessage() {
96 return getMessage(Locale.getDefault());
97 }
98
99
100 @Override
101 public Localizable getSpecifier() {
102 return specifier;
103 }
104
105
106 @Override
107 public Object[] getParts() {
108 return parts.clone();
109 }
110
111
112
113
114
115
116 @SuppressWarnings("PMD.AvoidCatchingGenericException")
117 private String buildMessage(final Locale locale) {
118 if (specifier == null) {
119 return "";
120 }
121
122 try {
123 return new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
124 } catch (Exception e) {
125 this.addSuppressed(e);
126 return specifier.getSourceString();
127 }
128
129 }
130
131 }