1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.linear;
19
20 import java.text.FieldPosition;
21 import java.text.NumberFormat;
22 import java.text.ParsePosition;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.commons.math4.legacy.exception.MathParseException;
28 import org.apache.commons.math4.legacy.util.CompositeFormat;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class RealVectorFormat {
44
45
46 private static final String DEFAULT_PREFIX = "{";
47
48 private static final String DEFAULT_SUFFIX = "}";
49
50 private static final String DEFAULT_SEPARATOR = "; ";
51
52 private final String prefix;
53
54 private final String suffix;
55
56 private final String separator;
57
58 private final String trimmedPrefix;
59
60 private final String trimmedSuffix;
61
62 private final String trimmedSeparator;
63
64 private final NumberFormat format;
65
66
67
68
69
70
71 public RealVectorFormat() {
72 this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR,
73 CompositeFormat.getDefaultNumberFormat());
74 }
75
76
77
78
79
80 public RealVectorFormat(final NumberFormat format) {
81 this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
82 }
83
84
85
86
87
88
89
90 public RealVectorFormat(final String prefix, final String suffix,
91 final String separator) {
92 this(prefix, suffix, separator,
93 CompositeFormat.getDefaultNumberFormat());
94 }
95
96
97
98
99
100
101
102
103
104 public RealVectorFormat(final String prefix, final String suffix,
105 final String separator, final NumberFormat format) {
106 this.prefix = prefix;
107 this.suffix = suffix;
108 this.separator = separator;
109 trimmedPrefix = prefix.trim();
110 trimmedSuffix = suffix.trim();
111 trimmedSeparator = separator.trim();
112 this.format = format;
113 }
114
115
116
117
118
119
120 public static Locale[] getAvailableLocales() {
121 return NumberFormat.getAvailableLocales();
122 }
123
124
125
126
127
128 public String getPrefix() {
129 return prefix;
130 }
131
132
133
134
135
136 public String getSuffix() {
137 return suffix;
138 }
139
140
141
142
143
144 public String getSeparator() {
145 return separator;
146 }
147
148
149
150
151
152 public NumberFormat getFormat() {
153 return format;
154 }
155
156
157
158
159
160 public static RealVectorFormat getInstance() {
161 return getInstance(Locale.getDefault());
162 }
163
164
165
166
167
168
169 public static RealVectorFormat getInstance(final Locale locale) {
170 return new RealVectorFormat(CompositeFormat.getDefaultNumberFormat(locale));
171 }
172
173
174
175
176
177
178
179 public String format(RealVector v) {
180 return format(v, new StringBuffer(), new FieldPosition(0)).toString();
181 }
182
183
184
185
186
187
188
189
190
191 public StringBuffer format(RealVector vector, StringBuffer toAppendTo,
192 FieldPosition pos) {
193
194 pos.setBeginIndex(0);
195 pos.setEndIndex(0);
196
197
198 toAppendTo.append(prefix);
199
200
201 for (int i = 0; i < vector.getDimension(); ++i) {
202 if (i > 0) {
203 toAppendTo.append(separator);
204 }
205 CompositeFormat.formatDouble(vector.getEntry(i), format, toAppendTo, pos);
206 }
207
208
209 toAppendTo.append(suffix);
210
211 return toAppendTo;
212 }
213
214
215
216
217
218
219
220
221
222 public ArrayRealVector parse(String source) {
223 final ParsePosition parsePosition = new ParsePosition(0);
224 final ArrayRealVector result = parse(source, parsePosition);
225 if (parsePosition.getIndex() == 0) {
226 throw new MathParseException(source,
227 parsePosition.getErrorIndex(),
228 ArrayRealVector.class);
229 }
230 return result;
231 }
232
233
234
235
236
237
238
239
240 public ArrayRealVector parse(String source, ParsePosition pos) {
241 int initialIndex = pos.getIndex();
242
243
244 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
245 if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) {
246 return null;
247 }
248
249
250 List<Number> components = new ArrayList<>();
251 for (boolean loop = true; loop;){
252
253 if (!components.isEmpty()) {
254 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
255 if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) {
256 loop = false;
257 }
258 }
259
260 if (loop) {
261 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
262 Number component = CompositeFormat.parseNumber(source, format, pos);
263 if (component != null) {
264 components.add(component);
265 } else {
266
267
268 pos.setIndex(initialIndex);
269 return null;
270 }
271 }
272 }
273
274
275 CompositeFormat.parseAndIgnoreWhitespace(source, pos);
276 if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) {
277 return null;
278 }
279
280
281 double[] data = new double[components.size()];
282 for (int i = 0; i < data.length; ++i) {
283 data[i] = components.get(i).doubleValue();
284 }
285 return new ArrayRealVector(data, false);
286 }
287 }