    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
    018    package org.apache.commons.math3.geometry.euclidean.twod;
    019
    020    import java.text.FieldPosition;
    021    import java.text.NumberFormat;
    022    import java.text.ParsePosition;
    023    import java.util.Locale;
    024
    025    import org.apache.commons.math3.exception.MathParseException;
    026    import org.apache.commons.math3.geometry.Vector;
    027    import org.apache.commons.math3.geometry.VectorFormat;
    028    import 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     *
    041     * @version $Id: Vector2DFormat.java 1416643 2012-12-03 19:37:14Z tn $
    042     * @since 3.0
    043     */
    044    public class Vector2DFormat extends VectorFormat<Euclidean2D> {
    045
    046        /**
    047         * Create an instance with default settings.
    048         * <p>The instance uses the default prefix, suffix and separator:
    049         * "{", "}", and "; " and the default number format for components.</p>
    050         */
    051        public Vector2DFormat() {
    052            super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR,
    053                  CompositeFormat.getDefaultNumberFormat());
    054        }
    055
    056        /**
    057         * Create an instance with a custom number format for components.
    058         * @param format the custom format for components.
    059         */
    060        public Vector2DFormat(final NumberFormat format) {
    061            super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
    062        }
    063
    064        /**
    065         * Create an instance with custom prefix, suffix and separator.
    066         * @param prefix prefix to use instead of the default "{"
    067         * @param suffix suffix to use instead of the default "}"
    068         * @param separator separator to use instead of the default "; "
    069         */
    070        public Vector2DFormat(final String prefix, final String suffix,
    071                             final String separator) {
    072            super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat());
    073        }
    074
    075        /**
    076         * Create an instance with custom prefix, suffix, separator and format
    077         * for components.
    078         * @param prefix prefix to use instead of the default "{"
    079         * @param suffix suffix to use instead of the default "}"
    080         * @param separator separator to use instead of the default "; "
    081         * @param format the custom format for components.
    082         */
    083        public Vector2DFormat(final String prefix, final String suffix,
    084                             final String separator, final NumberFormat format) {
    085            super(prefix, suffix, separator, format);
    086        }
    087
    088        /**
    089         * Returns the default 2D vector format for the current locale.
    090         * @return the default 2D vector format.
    091         */
    092        public static Vector2DFormat getInstance() {
    093            return getInstance(Locale.getDefault());
    094        }
    095
    096        /**
    097         * Returns the default 2D vector format for the given locale.
    098         * @param locale the specific locale used by the format.
    099         * @return the 2D vector format specific to the given locale.
    100         */
    101        public static Vector2DFormat getInstance(final Locale locale) {
    102            return new Vector2DFormat(CompositeFormat.getDefaultNumberFormat(locale));
    103        }
    104
    105        /** {@inheritDoc} */
    106        @Override
    107        public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo,
    108                                   final FieldPosition pos) {
    109            final Vector2D p2 = (Vector2D) vector;
    110            return format(toAppendTo, pos, p2.getX(), p2.getY());
    111        }
    112
    113        /** {@inheritDoc} */
    114        @Override
    115        public Vector2D parse(final String source) throws MathParseException {
    116            ParsePosition parsePosition = new ParsePosition(0);
    117            Vector2D result = parse(source, parsePosition);
    118            if (parsePosition.getIndex() == 0) {
    119                throw new MathParseException(source,
    120                                             parsePosition.getErrorIndex(),
    121                                             Vector2D.class);
    122            }
    123            return result;
    124        }
    125
    126        /** {@inheritDoc} */
    127        @Override
    128        public Vector2D parse(final String source, final ParsePosition pos) {
    129            final double[] coordinates = parseCoordinates(2, source, pos);
    130            if (coordinates == null) {
    131                return null;
    132            }
    133            return new Vector2D(coordinates[0], coordinates[1]);
    134        }
    135
    136    }