summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/geometry/VectorFormat.java
blob: 7c6d0c5ad534f1a3c4201f5e44b76b2069eb3787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.math3.geometry;

import org.apache.commons.math3.exception.MathParseException;
import org.apache.commons.math3.util.CompositeFormat;

import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;

/**
 * Formats a vector in components list format "{x; y; ...}".
 *
 * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by any user-defined
 * strings. The number format for components can be configured.
 *
 * <p>White space is ignored at parse time, even if it is in the prefix, suffix or separator
 * specifications. So even if the default separator does include a space character that is used at
 * format time, both input string "{1;1;1}" and " { 1 ; 1 ; 1 } " will be parsed without error and
 * the same vector will be returned. In the second case, however, the parse position after parsing
 * will be just after the closing curly brace, i.e. just before the trailing space.
 *
 * <p><b>Note:</b> using "," as a separator may interfere with the grouping separator of the default
 * {@link NumberFormat} for the current locale. Thus it is advised to use a {@link NumberFormat}
 * instance with disabled grouping in such a case.
 *
 * @param <S> Type of the space.
 * @since 3.0
 */
public abstract class VectorFormat<S extends Space> {

    /** The default prefix: "{". */
    public static final String DEFAULT_PREFIX = "{";

    /** The default suffix: "}". */
    public static final String DEFAULT_SUFFIX = "}";

    /** The default separator: ", ". */
    public static final String DEFAULT_SEPARATOR = "; ";

    /** Prefix. */
    private final String prefix;

    /** Suffix. */
    private final String suffix;

    /** Separator. */
    private final String separator;

    /** Trimmed prefix. */
    private final String trimmedPrefix;

    /** Trimmed suffix. */
    private final String trimmedSuffix;

    /** Trimmed separator. */
    private final String trimmedSeparator;

    /** The format used for components. */
    private final NumberFormat format;

    /**
     * Create an instance with default settings.
     *
     * <p>The instance uses the default prefix, suffix and separator: "{", "}", and "; " and the
     * default number format for components.
     */
    protected VectorFormat() {
        this(
                DEFAULT_PREFIX,
                DEFAULT_SUFFIX,
                DEFAULT_SEPARATOR,
                CompositeFormat.getDefaultNumberFormat());
    }

    /**
     * Create an instance with a custom number format for components.
     *
     * @param format the custom format for components.
     */
    protected VectorFormat(final NumberFormat format) {
        this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
    }

    /**
     * Create an instance with custom prefix, suffix and separator.
     *
     * @param prefix prefix to use instead of the default "{"
     * @param suffix suffix to use instead of the default "}"
     * @param separator separator to use instead of the default "; "
     */
    protected VectorFormat(final String prefix, final String suffix, final String separator) {
        this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat());
    }

    /**
     * Create an instance with custom prefix, suffix, separator and format for components.
     *
     * @param prefix prefix to use instead of the default "{"
     * @param suffix suffix to use instead of the default "}"
     * @param separator separator to use instead of the default "; "
     * @param format the custom format for components.
     */
    protected VectorFormat(
            final String prefix,
            final String suffix,
            final String separator,
            final NumberFormat format) {
        this.prefix = prefix;
        this.suffix = suffix;
        this.separator = separator;
        trimmedPrefix = prefix.trim();
        trimmedSuffix = suffix.trim();
        trimmedSeparator = separator.trim();
        this.format = format;
    }

    /**
     * Get the set of locales for which point/vector formats are available.
     *
     * <p>This is the same set as the {@link NumberFormat} set.
     *
     * @return available point/vector format locales.
     */
    public static Locale[] getAvailableLocales() {
        return NumberFormat.getAvailableLocales();
    }

    /**
     * Get the format prefix.
     *
     * @return format prefix.
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * Get the format suffix.
     *
     * @return format suffix.
     */
    public String getSuffix() {
        return suffix;
    }

    /**
     * Get the format separator between components.
     *
     * @return format separator.
     */
    public String getSeparator() {
        return separator;
    }

    /**
     * Get the components format.
     *
     * @return components format.
     */
    public NumberFormat getFormat() {
        return format;
    }

    /**
     * Formats a {@link Vector} object to produce a string.
     *
     * @param vector the object to format.
     * @return a formatted string.
     */
    public String format(Vector<S> vector) {
        return format(vector, new StringBuffer(), new FieldPosition(0)).toString();
    }

    /**
     * Formats a {@link Vector} object to produce a string.
     *
     * @param vector the object to format.
     * @param toAppendTo where the text is to be appended
     * @param pos On input: an alignment field, if desired. On output: the offsets of the alignment
     *     field
     * @return the value passed in as toAppendTo.
     */
    public abstract StringBuffer format(
            Vector<S> vector, StringBuffer toAppendTo, FieldPosition pos);

    /**
     * Formats the coordinates of a {@link Vector} to produce a string.
     *
     * @param toAppendTo where the text is to be appended
     * @param pos On input: an alignment field, if desired. On output: the offsets of the alignment
     *     field
     * @param coordinates coordinates of the object to format.
     * @return the value passed in as toAppendTo.
     */
    protected StringBuffer format(
            StringBuffer toAppendTo, FieldPosition pos, double... coordinates) {

        pos.setBeginIndex(0);
        pos.setEndIndex(0);

        // format prefix
        toAppendTo.append(prefix);

        // format components
        for (int i = 0; i < coordinates.length; ++i) {
            if (i > 0) {
                toAppendTo.append(separator);
            }
            CompositeFormat.formatDouble(coordinates[i], format, toAppendTo, pos);
        }

        // format suffix
        toAppendTo.append(suffix);

        return toAppendTo;
    }

    /**
     * Parses a string to produce a {@link Vector} object.
     *
     * @param source the string to parse
     * @return the parsed {@link Vector} object.
     * @throws MathParseException if the beginning of the specified string cannot be parsed.
     */
    public abstract Vector<S> parse(String source) throws MathParseException;

    /**
     * Parses a string to produce a {@link Vector} object.
     *
     * @param source the string to parse
     * @param pos input/output parsing parameter.
     * @return the parsed {@link Vector} object.
     */
    public abstract Vector<S> parse(String source, ParsePosition pos);

    /**
     * Parses a string to produce an array of coordinates.
     *
     * @param dimension dimension of the space
     * @param source the string to parse
     * @param pos input/output parsing parameter.
     * @return coordinates array.
     */
    protected double[] parseCoordinates(int dimension, String source, ParsePosition pos) {

        int initialIndex = pos.getIndex();
        double[] coordinates = new double[dimension];

        // parse prefix
        CompositeFormat.parseAndIgnoreWhitespace(source, pos);
        if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) {
            return null;
        }

        for (int i = 0; i < dimension; ++i) {

            // skip whitespace
            CompositeFormat.parseAndIgnoreWhitespace(source, pos);

            // parse separator
            if (i > 0 && !CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) {
                return null;
            }

            // skip whitespace
            CompositeFormat.parseAndIgnoreWhitespace(source, pos);

            // parse coordinate
            Number c = CompositeFormat.parseNumber(source, format, pos);
            if (c == null) {
                // invalid coordinate
                // set index back to initial, error index should already be set
                pos.setIndex(initialIndex);
                return null;
            }

            // store coordinate
            coordinates[i] = c.doubleValue();
        }

        // parse suffix
        CompositeFormat.parseAndIgnoreWhitespace(source, pos);
        if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) {
            return null;
        }

        return coordinates;
    }
}