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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.geometry.euclidean.threed;

  22. import java.text.FieldPosition;
  23. import java.text.NumberFormat;
  24. import java.text.ParsePosition;
  25. import java.util.Locale;

  26. import org.hipparchus.exception.LocalizedCoreFormats;
  27. import org.hipparchus.exception.MathIllegalStateException;
  28. import org.hipparchus.geometry.Vector;
  29. import org.hipparchus.geometry.VectorFormat;
  30. import org.hipparchus.util.CompositeFormat;

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

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

  56.     /**
  57.      * Create an instance with a custom number format for components.
  58.      * @param format the custom format for components.
  59.      */
  60.     public Vector3DFormat(final NumberFormat format) {
  61.         super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
  62.     }

  63.     /**
  64.      * Create an instance with custom prefix, suffix and separator.
  65.      * @param prefix prefix to use instead of the default "{"
  66.      * @param suffix suffix to use instead of the default "}"
  67.      * @param separator separator to use instead of the default "; "
  68.      */
  69.     public Vector3DFormat(final String prefix, final String suffix,
  70.                          final String separator) {
  71.         super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat());
  72.     }

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

  85.     /**
  86.      * Returns the default 3D vector format for the current locale.
  87.      * @return the default 3D vector format.
  88.      * @since 1.4
  89.      */
  90.     public static Vector3DFormat getVector3DFormat() {
  91.         return getVector3DFormat(Locale.getDefault());
  92.     }

  93.     /**
  94.      * Returns the default 3D vector format for the given locale.
  95.      * @param locale the specific locale used by the format.
  96.      * @return the 3D vector format specific to the given locale.
  97.      * @since 1.4
  98.      */
  99.     public static Vector3DFormat getVector3DFormat(final Locale locale) {
  100.         return new Vector3DFormat(CompositeFormat.getDefaultNumberFormat(locale));
  101.     }

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

  116.     /**
  117.      * Parses a string to produce a {@link Vector3D} object.
  118.      * @param source the string to parse
  119.      * @return the parsed {@link Vector3D} object.
  120.      * @throws MathIllegalStateException if the beginning of the specified string
  121.      * cannot be parsed.
  122.      */
  123.     @Override
  124.     public Vector3D parse(final String source) throws MathIllegalStateException {
  125.         ParsePosition parsePosition = new ParsePosition(0);
  126.         Vector3D result = parse(source, parsePosition);
  127.         if (parsePosition.getIndex() == 0) {
  128.             throw new MathIllegalStateException(LocalizedCoreFormats.CANNOT_PARSE_AS_TYPE,
  129.                                                 source, parsePosition.getErrorIndex(),
  130.                                                 Vector3D.class);
  131.         }
  132.         return result;
  133.     }

  134.     /**
  135.      * Parses a string to produce a {@link Vector3D} object.
  136.      * @param source the string to parse
  137.      * @param pos input/ouput parsing parameter.
  138.      * @return the parsed {@link Vector3D} object.
  139.      */
  140.     @Override
  141.     public Vector3D parse(final String source, final ParsePosition pos) {
  142.         final double[] coordinates = parseCoordinates(3, source, pos);
  143.         if (coordinates == null) {
  144.             return null;
  145.         }
  146.         return new Vector3D(coordinates[0], coordinates[1], coordinates[2]);
  147.     }

  148. }