Vector3DFormat.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math3.geometry.euclidean.threed;

  18. import java.text.FieldPosition;
  19. import java.text.NumberFormat;
  20. import java.text.ParsePosition;
  21. import java.util.Locale;

  22. import org.apache.commons.math3.exception.MathParseException;
  23. import org.apache.commons.math3.geometry.Vector;
  24. import org.apache.commons.math3.geometry.VectorFormat;
  25. import org.apache.commons.math3.util.CompositeFormat;

  26. /**
  27.  * Formats a 3D vector in components list format "{x; y; z}".
  28.  * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by
  29.  * any user-defined strings. The number format for components can be configured.</p>
  30.  * <p>White space is ignored at parse time, even if it is in the prefix, suffix
  31.  * or separator specifications. So even if the default separator does include a space
  32.  * character that is used at format time, both input string "{1;1;1}" and
  33.  * " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be
  34.  * returned. In the second case, however, the parse position after parsing will be
  35.  * just after the closing curly brace, i.e. just before the trailing space.</p>
  36.  * <p><b>Note:</b> using "," as a separator may interfere with the grouping separator
  37.  * of the default {@link NumberFormat} for the current locale. Thus it is advised
  38.  * to use a {@link NumberFormat} instance with disabled grouping in such a case.</p>
  39.  *
  40.  */
  41. public class Vector3DFormat extends VectorFormat<Euclidean3D> {

  42.     /**
  43.      * Create an instance with default settings.
  44.      * <p>The instance uses the default prefix, suffix and separator:
  45.      * "{", "}", and "; " and the default number format for components.</p>
  46.      */
  47.     public Vector3DFormat() {
  48.         super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR,
  49.               CompositeFormat.getDefaultNumberFormat());
  50.     }

  51.     /**
  52.      * Create an instance with a custom number format for components.
  53.      * @param format the custom format for components.
  54.      */
  55.     public Vector3DFormat(final NumberFormat format) {
  56.         super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
  57.     }

  58.     /**
  59.      * Create an instance with custom prefix, suffix and separator.
  60.      * @param prefix prefix to use instead of the default "{"
  61.      * @param suffix suffix to use instead of the default "}"
  62.      * @param separator separator to use instead of the default "; "
  63.      */
  64.     public Vector3DFormat(final String prefix, final String suffix,
  65.                          final String separator) {
  66.         super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat());
  67.     }

  68.     /**
  69.      * Create an instance with custom prefix, suffix, separator and format
  70.      * for components.
  71.      * @param prefix prefix to use instead of the default "{"
  72.      * @param suffix suffix to use instead of the default "}"
  73.      * @param separator separator to use instead of the default "; "
  74.      * @param format the custom format for components.
  75.      */
  76.     public Vector3DFormat(final String prefix, final String suffix,
  77.                          final String separator, final NumberFormat format) {
  78.         super(prefix, suffix, separator, format);
  79.     }

  80.     /**
  81.      * Returns the default 3D vector format for the current locale.
  82.      * @return the default 3D vector format.
  83.      */
  84.     public static Vector3DFormat getInstance() {
  85.         return getInstance(Locale.getDefault());
  86.     }

  87.     /**
  88.      * Returns the default 3D vector format for the given locale.
  89.      * @param locale the specific locale used by the format.
  90.      * @return the 3D vector format specific to the given locale.
  91.      */
  92.     public static Vector3DFormat getInstance(final Locale locale) {
  93.         return new Vector3DFormat(CompositeFormat.getDefaultNumberFormat(locale));
  94.     }

  95.     /**
  96.      * Formats a {@link Vector3D} object to produce a string.
  97.      * @param vector the object to format.
  98.      * @param toAppendTo where the text is to be appended
  99.      * @param pos On input: an alignment field, if desired. On output: the
  100.      *            offsets of the alignment field
  101.      * @return the value passed in as toAppendTo.
  102.      */
  103.     @Override
  104.     public StringBuffer format(final Vector<Euclidean3D> vector, final StringBuffer toAppendTo,
  105.                                final FieldPosition pos) {
  106.         final Vector3D v3 = (Vector3D) vector;
  107.         return format(toAppendTo, pos, v3.getX(), v3.getY(), v3.getZ());
  108.     }

  109.     /**
  110.      * Parses a string to produce a {@link Vector3D} object.
  111.      * @param source the string to parse
  112.      * @return the parsed {@link Vector3D} object.
  113.      * @throws MathParseException if the beginning of the specified string
  114.      * cannot be parsed.
  115.      */
  116.     @Override
  117.     public Vector3D parse(final String source) throws MathParseException {
  118.         ParsePosition parsePosition = new ParsePosition(0);
  119.         Vector3D result = parse(source, parsePosition);
  120.         if (parsePosition.getIndex() == 0) {
  121.             throw new MathParseException(source,
  122.                                          parsePosition.getErrorIndex(),
  123.                                          Vector3D.class);
  124.         }
  125.         return result;
  126.     }

  127.     /**
  128.      * Parses a string to produce a {@link Vector3D} object.
  129.      * @param source the string to parse
  130.      * @param pos input/ouput parsing parameter.
  131.      * @return the parsed {@link Vector3D} object.
  132.      */
  133.     @Override
  134.     public Vector3D parse(final String source, final ParsePosition pos) {
  135.         final double[] coordinates = parseCoordinates(3, source, pos);
  136.         if (coordinates == null) {
  137.             return null;
  138.         }
  139.         return new Vector3D(coordinates[0], coordinates[1], coordinates[2]);
  140.     }

  141. }