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.isEmpty()) {
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).append(getImaginaryCharacter());
211 } else if (im > 0.0 || Double.isNaN(im)) {
212 toAppendTo.append(" + ");
213 imAppendTo = formatImaginary(im, new StringBuffer(), pos);
214 toAppendTo.append(imAppendTo).append(getImaginaryCharacter());
215 }
216
217 return toAppendTo;
218 }
219
220
221
222
223
224
225
226
227
228
229 private StringBuffer formatImaginary(double absIm,
230 StringBuffer toAppendTo,
231 FieldPosition pos) {
232 pos.setBeginIndex(0);
233 pos.setEndIndex(0);
234
235 CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
236 if ("1".equals(toAppendTo.toString())) {
237
238 toAppendTo.setLength(0);
239 }
240
241 return toAppendTo;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public StringBuffer format(Object obj, StringBuffer toAppendTo,
258 FieldPosition pos)
259 throws MathIllegalArgumentException {
260
261 if (obj instanceof Complex) {
262 return format( (Complex)obj, toAppendTo, pos);
263 } else if (obj instanceof Number) {
264 return format(new Complex(((Number)obj).doubleValue(), 0.0), toAppendTo, pos);
265 } else {
266 throw new MathIllegalArgumentException(LocalizedCoreFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX,
267 obj.getClass().getName());
268 }
269
270 }
271
272
273
274
275
276 public String getImaginaryCharacter() {
277 return imaginaryCharacter;
278 }
279
280
281
282
283
284 public NumberFormat getImaginaryFormat() {
285 return imaginaryFormat;
286 }
287
288
289
290
291
292
293 public static ComplexFormat getComplexFormat() {
294 return getComplexFormat(Locale.getDefault());
295 }
296
297
298
299
300
301
302
303 public static ComplexFormat getComplexFormat(Locale locale) {
304 NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
305 return new ComplexFormat(f);
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319 public static ComplexFormat getComplexFormat(String imaginaryCharacter, Locale locale)
320 throws MathIllegalArgumentException, NullArgumentException {
321 NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
322 return new ComplexFormat(imaginaryCharacter, f);
323 }
324
325
326
327
328
329 public NumberFormat getRealFormat() {
330 return realFormat;
331 }
332
333
334
335
336
337
338
339
340
341 public Complex parse(String source) throws MathIllegalStateException {
342 ParsePosition parsePosition = new ParsePosition(0);
343 Complex result = parse(source, parsePosition);
344 if (parsePosition.getIndex() == 0) {
345 throw new MathIllegalStateException(LocalizedCoreFormats.CANNOT_PARSE_AS_TYPE,
346 source, parsePosition.getErrorIndex(),
347 Complex.class);
348 }
349 return result;
350 }
351
352
353
354
355
356
357
358
359 public Complex parse(String source, ParsePosition pos) {
360 int initialIndex = pos.getIndex();
361
362
363 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
364
365
366 Number re = CompositeFormat.parseNumber(source, getRealFormat(), pos);
367 if (re == null) {
368
369
370 pos.setIndex(initialIndex);
371 return null;
372 }
373
374
375 int startIndex = pos.getIndex();
376 char c = CompositeFormat.parseNextCharacter(source, pos);
377 int sign;
378 switch (c) {
379 case 0 :
380
381
382 return new Complex(re.doubleValue(), 0.0);
383 case '-' :
384 sign = -1;
385 break;
386 case '+' :
387 sign = 1;
388 break;
389 default :
390
391
392
393 pos.setIndex(initialIndex);
394 pos.setErrorIndex(startIndex);
395 return null;
396 }
397
398
399 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
400
401
402 Number im = CompositeFormat.parseNumber(source, getRealFormat(), pos);
403 if (im == null) {
404
405
406 pos.setIndex(initialIndex);
407 return null;
408 }
409
410
411 if (!CompositeFormat.parseFixedstring(source, getImaginaryCharacter(), pos)) {
412 return null;
413 }
414
415 return new Complex(re.doubleValue(), im.doubleValue() * sign);
416
417 }
418 }