001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.geometry.euclidean.twod;
019
020import java.text.FieldPosition;
021import java.text.NumberFormat;
022import java.text.ParsePosition;
023import java.util.Locale;
024
025import org.apache.commons.math3.exception.MathParseException;
026import org.apache.commons.math3.geometry.Vector;
027import org.apache.commons.math3.geometry.VectorFormat;
028import org.apache.commons.math3.util.CompositeFormat;
029
030/**
031 * Formats a 2D vector in components list format "{x; y}".
032 * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by
033 * any user-defined strings. The number format for components can be configured.</p>
034 * <p>White space is ignored at parse time, even if it is in the prefix, suffix
035 * or separator specifications. So even if the default separator does include a space
036 * character that is used at format time, both input string "{1;1}" and
037 * " { 1 ; 1 } " will be parsed without error and the same vector will be
038 * returned. In the second case, however, the parse position after parsing will be
039 * just after the closing curly brace, i.e. just before the trailing space.</p>
040 * <p><b>Note:</b> using "," as a separator may interfere with the grouping separator
041 * of the default {@link NumberFormat} for the current locale. Thus it is advised
042 * to use a {@link NumberFormat} instance with disabled grouping in such a case.</p>
043 *
044 * @version $Id: Vector2DFormat.java 1467801 2013-04-14 16:19:33Z tn $
045 * @since 3.0
046 */
047public class Vector2DFormat extends VectorFormat<Euclidean2D> {
048
049    /**
050     * Create an instance with default settings.
051     * <p>The instance uses the default prefix, suffix and separator:
052     * "{", "}", and "; " and the default number format for components.</p>
053     */
054    public Vector2DFormat() {
055        super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR,
056              CompositeFormat.getDefaultNumberFormat());
057    }
058
059    /**
060     * Create an instance with a custom number format for components.
061     * @param format the custom format for components.
062     */
063    public Vector2DFormat(final NumberFormat format) {
064        super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
065    }
066
067    /**
068     * Create an instance with custom prefix, suffix and separator.
069     * @param prefix prefix to use instead of the default "{"
070     * @param suffix suffix to use instead of the default "}"
071     * @param separator separator to use instead of the default "; "
072     */
073    public Vector2DFormat(final String prefix, final String suffix,
074                         final String separator) {
075        super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat());
076    }
077
078    /**
079     * Create an instance with custom prefix, suffix, separator and format
080     * for components.
081     * @param prefix prefix to use instead of the default "{"
082     * @param suffix suffix to use instead of the default "}"
083     * @param separator separator to use instead of the default "; "
084     * @param format the custom format for components.
085     */
086    public Vector2DFormat(final String prefix, final String suffix,
087                         final String separator, final NumberFormat format) {
088        super(prefix, suffix, separator, format);
089    }
090
091    /**
092     * Returns the default 2D vector format for the current locale.
093     * @return the default 2D vector format.
094     */
095    public static Vector2DFormat getInstance() {
096        return getInstance(Locale.getDefault());
097    }
098
099    /**
100     * Returns the default 2D vector format for the given locale.
101     * @param locale the specific locale used by the format.
102     * @return the 2D vector format specific to the given locale.
103     */
104    public static Vector2DFormat getInstance(final Locale locale) {
105        return new Vector2DFormat(CompositeFormat.getDefaultNumberFormat(locale));
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo,
111                               final FieldPosition pos) {
112        final Vector2D p2 = (Vector2D) vector;
113        return format(toAppendTo, pos, p2.getX(), p2.getY());
114    }
115
116    /** {@inheritDoc} */
117    @Override
118    public Vector2D parse(final String source) throws MathParseException {
119        ParsePosition parsePosition = new ParsePosition(0);
120        Vector2D result = parse(source, parsePosition);
121        if (parsePosition.getIndex() == 0) {
122            throw new MathParseException(source,
123                                         parsePosition.getErrorIndex(),
124                                         Vector2D.class);
125        }
126        return result;
127    }
128
129    /** {@inheritDoc} */
130    @Override
131    public Vector2D parse(final String source, final ParsePosition pos) {
132        final double[] coordinates = parseCoordinates(2, source, pos);
133        if (coordinates == null) {
134            return null;
135        }
136        return new Vector2D(coordinates[0], coordinates[1]);
137    }
138
139}