1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.complex;
24
25 import java.text.FieldPosition;
26 import java.text.NumberFormat;
27 import java.text.ParsePosition;
28 import java.util.Locale;
29
30 import org.hipparchus.exception.LocalizedCoreFormats;
31 import org.hipparchus.exception.MathIllegalArgumentException;
32 import org.hipparchus.exception.MathIllegalStateException;
33 import org.hipparchus.exception.NullArgumentException;
34 import org.hipparchus.util.CompositeFormat;
35 import org.hipparchus.util.MathUtils;
36
37
38
39
40
41
42 public class ComplexFormat {
43
44
45 private static final String DEFAULT_IMAGINARY_CHARACTER = "i";
46
47 private final String imaginaryCharacter;
48
49 private final NumberFormat imaginaryFormat;
50
51 private final NumberFormat realFormat;
52
53
54
55
56
57 public ComplexFormat() {
58 this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER;
59 this.imaginaryFormat = CompositeFormat.getDefaultNumberFormat();
60 this.realFormat = imaginaryFormat;
61 }
62
63
64
65
66
67
68
69 public ComplexFormat(NumberFormat format) throws NullArgumentException {
70 MathUtils.checkNotNull(format, LocalizedCoreFormats.IMAGINARY_FORMAT);
71 this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER;
72 this.imaginaryFormat = format;
73 this.realFormat = format;
74 }
75
76
77
78
79
80
81
82
83
84 public ComplexFormat(NumberFormat realFormat, NumberFormat imaginaryFormat)
85 throws NullArgumentException {
86 MathUtils.checkNotNull(imaginaryFormat, LocalizedCoreFormats.IMAGINARY_FORMAT);
87 MathUtils.checkNotNull(realFormat, LocalizedCoreFormats.REAL_FORMAT);
88
89 this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER;
90 this.imaginaryFormat = imaginaryFormat;
91 this.realFormat = realFormat;
92 }
93
94
95
96
97
98
99
100
101
102
103 public ComplexFormat(String imaginaryCharacter)
104 throws MathIllegalArgumentException, NullArgumentException {
105 this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat());
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119 public ComplexFormat(String imaginaryCharacter, NumberFormat format)
120 throws MathIllegalArgumentException, NullArgumentException {
121 this(imaginaryCharacter, format, format);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public ComplexFormat(String imaginaryCharacter,
140 NumberFormat realFormat,
141 NumberFormat imaginaryFormat)
142 throws MathIllegalArgumentException, NullArgumentException {
143 if (imaginaryCharacter == null) {
144 throw new NullArgumentException();
145 }
146 if (imaginaryCharacter.length() == 0) {
147 throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
148 }
149 MathUtils.checkNotNull(imaginaryFormat, LocalizedCoreFormats.IMAGINARY_FORMAT);
150 MathUtils.checkNotNull(realFormat, LocalizedCoreFormats.REAL_FORMAT);
151
152 this.imaginaryCharacter = imaginaryCharacter;
153 this.imaginaryFormat = imaginaryFormat;
154 this.realFormat = realFormat;
155 }
156
157
158
159
160
161
162 public static Locale[] getAvailableLocales() {
163 return NumberFormat.getAvailableLocales();
164 }
165
166
167
168
169
170
171
172 public String format(Complex c) {
173 return format(c, new StringBuffer(), new FieldPosition(0)).toString();
174 }
175
176
177
178
179
180
181
182 public String format(Double c) {
183 return format(new Complex(c, 0), new StringBuffer(), new FieldPosition(0)).toString();
184 }
185
186
187
188
189
190
191
192
193
194
195 public StringBuffer format(Complex complex, StringBuffer toAppendTo,
196 FieldPosition pos) {
197 pos.setBeginIndex(0);
198 pos.setEndIndex(0);
199
200
201 double re = complex.getReal();
202 CompositeFormat.formatDouble(re, getRealFormat(), toAppendTo, pos);
203
204
205 double im = complex.getImaginary();
206 StringBuffer imAppendTo;
207 if (im < 0.0) {
208 toAppendTo.append(" - ");
209 imAppendTo = formatImaginary(-im, new StringBuffer(), pos);
210 toAppendTo.append(imAppendTo);
211 toAppendTo.append(getImaginaryCharacter());
212 } else if (im > 0.0 || Double.isNaN(im)) {
213 toAppendTo.append(" + ");
214 imAppendTo = formatImaginary(im, new StringBuffer(), pos);
215 toAppendTo.append(imAppendTo);
216 toAppendTo.append(getImaginaryCharacter());
217 }
218
219 return toAppendTo;
220 }
221
222
223
224
225
226
227
228
229
230
231 private StringBuffer formatImaginary(double absIm,
232 StringBuffer toAppendTo,
233 FieldPosition pos) {
234 pos.setBeginIndex(0);
235 pos.setEndIndex(0);
236
237 CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
238 if ("1".equals(toAppendTo.toString())) {
239
240 toAppendTo.setLength(0);
241 }
242
243 return toAppendTo;
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public StringBuffer format(Object obj, StringBuffer toAppendTo,
260 FieldPosition pos)
261 throws MathIllegalArgumentException {
262
263 if (obj instanceof Complex) {
264 return format( (Complex)obj, toAppendTo, pos);
265 } else if (obj instanceof Number) {
266 return format(new Complex(((Number)obj).doubleValue(), 0.0), toAppendTo, pos);
267 } else {
268 throw new MathIllegalArgumentException(LocalizedCoreFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX,
269 obj.getClass().getName());
270 }
271
272 }
273
274
275
276
277
278 public String getImaginaryCharacter() {
279 return imaginaryCharacter;
280 }
281
282
283
284
285
286 public NumberFormat getImaginaryFormat() {
287 return imaginaryFormat;
288 }
289
290
291
292
293
294
295 public static ComplexFormat getComplexFormat() {
296 return getComplexFormat(Locale.getDefault());
297 }
298
299
300
301
302
303
304
305 public static ComplexFormat getComplexFormat(Locale locale) {
306 NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
307 return new ComplexFormat(f);
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 public static ComplexFormat getComplexFormat(String imaginaryCharacter, Locale locale)
322 throws MathIllegalArgumentException, NullArgumentException {
323 NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
324 return new ComplexFormat(imaginaryCharacter, f);
325 }
326
327
328
329
330
331 public NumberFormat getRealFormat() {
332 return realFormat;
333 }
334
335
336
337
338
339
340
341
342
343 public Complex parse(String source) throws MathIllegalStateException {
344 ParsePosition parsePosition = new ParsePosition(0);
345 Complex result = parse(source, parsePosition);
346 if (parsePosition.getIndex() == 0) {
347 throw new MathIllegalStateException(LocalizedCoreFormats.CANNOT_PARSE_AS_TYPE,
348 source, parsePosition.getErrorIndex(),
349 Complex.class);
350 }
351 return result;
352 }
353
354
355
356
357
358
359
360
361 public Complex parse(String source, ParsePosition pos) {
362 int initialIndex = pos.getIndex();
363
364
365 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
366
367
368 Number re = CompositeFormat.parseNumber(source, getRealFormat(), pos);
369 if (re == null) {
370
371
372 pos.setIndex(initialIndex);
373 return null;
374 }
375
376
377 int startIndex = pos.getIndex();
378 char c = CompositeFormat.parseNextCharacter(source, pos);
379 int sign = 0;
380 switch (c) {
381 case 0 :
382
383
384 return new Complex(re.doubleValue(), 0.0);
385 case '-' :
386 sign = -1;
387 break;
388 case '+' :
389 sign = 1;
390 break;
391 default :
392
393
394
395 pos.setIndex(initialIndex);
396 pos.setErrorIndex(startIndex);
397 return null;
398 }
399
400
401 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
402
403
404 Number im = CompositeFormat.parseNumber(source, getRealFormat(), pos);
405 if (im == null) {
406
407
408 pos.setIndex(initialIndex);
409 return null;
410 }
411
412
413 if (!CompositeFormat.parseFixedstring(source, getImaginaryCharacter(), pos)) {
414 return null;
415 }
416
417 return new Complex(re.doubleValue(), im.doubleValue() * sign);
418
419 }
420 }