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