1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.errors;
18
19 import java.text.MessageFormat;
20 import java.text.ParseException;
21 import java.util.Locale;
22
23 import org.apache.commons.math3.exception.util.ExceptionContext;
24 import org.apache.commons.math3.exception.util.ExceptionContextProvider;
25 import org.apache.commons.math3.exception.util.Localizable;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class OrekitException extends Exception {
45
46
47 private static final long serialVersionUID = 3366757982695469677L;
48
49
50 private final ExceptionContext context;
51
52
53 private final Localizable specifier;
54
55
56 private final Object[] parts;
57
58
59
60
61
62
63 public OrekitException(final Localizable specifier, final Object ... parts) {
64 this.context = null;
65 this.specifier = specifier;
66 this.parts = (parts == null) ? new Object[0] : parts.clone();
67 }
68
69
70
71
72
73 public OrekitException(final OrekitException exception) {
74 super(exception);
75 this.context = exception.context;
76 this.specifier = exception.specifier;
77 this.parts = exception.parts.clone();
78 }
79
80
81
82
83
84
85 public OrekitException(final Localizable message, final Throwable cause) {
86 super(cause);
87 this.context = null;
88 this.specifier = message;
89 this.parts = new Object[0];
90 }
91
92
93
94
95
96
97
98 public OrekitException(final Throwable cause, final Localizable specifier,
99 final Object ... parts) {
100 super(cause);
101 this.context = null;
102 this.specifier = specifier;
103 this.parts = (parts == null) ? new Object[0] : parts.clone();
104 }
105
106
107
108
109
110
111 public OrekitException(final ExceptionContextProvider provider) {
112 super(provider.getContext().getThrowable());
113 this.context = provider.getContext();
114 this.specifier = null;
115 this.parts = new Object[0];
116 }
117
118
119
120
121
122
123 public String getMessage(final Locale locale) {
124 return (context != null) ?
125 context.getMessage(locale) :
126 buildMessage(locale, specifier, parts);
127 }
128
129
130 @Override
131 public String getMessage() {
132 return getMessage(Locale.US);
133 }
134
135
136 @Override
137 public String getLocalizedMessage() {
138 return getMessage(Locale.getDefault());
139 }
140
141
142
143
144
145 public Localizable getSpecifier() {
146 return specifier;
147 }
148
149
150
151
152
153 public Object[] getParts() {
154 return parts.clone();
155 }
156
157
158
159
160
161
162
163
164 private static String buildMessage(final Locale locale, final Localizable specifier,
165 final Object ... parts) {
166 return (specifier == null) ? "" : new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
167 }
168
169
170
171
172
173
174 public static IllegalArgumentException createIllegalArgumentException(final Localizable specifier,
175 final Object ... parts) {
176 return new IllegalArgumentException() {
177
178
179 private static final long serialVersionUID = 2601215225271704045L;
180
181
182 @Override
183 public String getMessage() {
184 return buildMessage(Locale.US, specifier, parts);
185 }
186
187
188 @Override
189 public String getLocalizedMessage() {
190 return buildMessage(Locale.getDefault(), specifier, parts);
191 }
192
193 };
194
195 }
196
197
198
199
200
201
202 public static IllegalStateException createIllegalStateException(final Localizable specifier,
203 final Object ... parts) {
204
205 return new IllegalStateException() {
206
207
208 private static final long serialVersionUID = -5527779242879685212L;
209
210
211 @Override
212 public String getMessage() {
213 return buildMessage(Locale.US, specifier, parts);
214 }
215
216
217 @Override
218 public String getLocalizedMessage() {
219 return buildMessage(Locale.getDefault(), specifier, parts);
220 }
221
222 };
223
224 }
225
226
227
228
229
230
231 public static ParseException createParseException(final Localizable specifier,
232 final Object ... parts) {
233
234 return new ParseException("", 0) {
235
236
237 private static final long serialVersionUID = 4771367217940584391L;
238
239
240 @Override
241 public String getMessage() {
242 return buildMessage(Locale.US, specifier, parts);
243 }
244
245
246 @Override
247 public String getLocalizedMessage() {
248 return buildMessage(Locale.getDefault(), specifier, parts);
249 }
250
251 };
252
253 }
254
255
256
257
258
259 public static RuntimeException createInternalError(final Throwable cause) {
260
261
262 final Localizable specifier = OrekitMessages.INTERNAL_ERROR;
263
264
265 final String parts = "orekit@c-s.fr";
266
267 return new RuntimeException() {
268
269
270 private static final long serialVersionUID = -6493358459835909138L;
271
272
273 @Override
274 public String getMessage() {
275 return buildMessage(Locale.US, specifier, parts);
276 }
277
278
279 @Override
280 public String getLocalizedMessage() {
281 return buildMessage(Locale.getDefault(), specifier, parts);
282 }
283
284 };
285
286 }
287
288 }