    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.oned;
    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 1D vector in components list format "{x}".
    032     * <p>The prefix and suffix "{" and "}" 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}" and
    037     * " { 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: Vector1DFormat.java 1416643 2012-12-03 19:37:14Z tn $
    042     * @since 3.0
    043     */
    044    public class Vector1DFormat extends VectorFormat<Euclidean1D> {
    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 Vector1DFormat() {
    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 Vector1DFormat(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         */
    069        public Vector1DFormat(final String prefix, final String suffix) {
    070            super(prefix, suffix, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat());
    071        }
    072
    073        /**
    074         * Create an instance with custom prefix, suffix, separator and format
    075         * for components.
    076         * @param prefix prefix to use instead of the default "{"
    077         * @param suffix suffix to use instead of the default "}"
    078         * @param format the custom format for components.
    079         */
    080        public Vector1DFormat(final String prefix, final String suffix,
    081                             final NumberFormat format) {
    082            super(prefix, suffix, DEFAULT_SEPARATOR, format);
    083        }
    084
    085        /**
    086         * Returns the default 1D vector format for the current locale.
    087         * @return the default 1D vector format.
    088         */
    089        public static Vector1DFormat getInstance() {
    090            return getInstance(Locale.getDefault());
    091        }
    092
    093        /**
    094         * Returns the default 1D vector format for the given locale.
    095         * @param locale the specific locale used by the format.
    096         * @return the 1D vector format specific to the given locale.
    097         */
    098        public static Vector1DFormat getInstance(final Locale locale) {
    099            return new Vector1DFormat(CompositeFormat.getDefaultNumberFormat(locale));
    100        }
    101
    102        /** {@inheritDoc} */
    103        @Override
    104        public StringBuffer format(final Vector<Euclidean1D> vector, final StringBuffer toAppendTo,
    105                                   final FieldPosition pos) {
    106            final Vector1D p1 = (Vector1D) vector;
    107            return format(toAppendTo, pos, p1.getX());
    108        }
    109
    110        /** {@inheritDoc} */
    111        @Override
    112        public Vector1D parse(final String source) throws MathParseException {
    113            ParsePosition parsePosition = new ParsePosition(0);
    114            Vector1D result = parse(source, parsePosition);
    115            if (parsePosition.getIndex() == 0) {
    116                throw new MathParseException(source,
    117                                             parsePosition.getErrorIndex(),
    118                                             Vector1D.class);
    119            }
    120            return result;
    121        }
    122
    123        /** {@inheritDoc} */
    124        @Override
    125        public Vector1D parse(final String source, final ParsePosition pos) {
    126            final double[] coordinates = parseCoordinates(1, source, pos);
    127            if (coordinates == null) {
    128                return null;
    129            }
    130            return new Vector1D(coordinates[0]);
    131        }
    132
    133    }