View Javadoc

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  
18  package org.apache.commons.math3.geometry.euclidean.threed;
19  
20  import java.text.FieldPosition;
21  import java.text.NumberFormat;
22  import java.text.ParsePosition;
23  import java.util.Locale;
24  
25  import org.apache.commons.math3.exception.MathParseException;
26  import org.apache.commons.math3.geometry.Vector;
27  import org.apache.commons.math3.geometry.VectorFormat;
28  import org.apache.commons.math3.util.CompositeFormat;
29  
30  /**
31   * Formats a 3D vector in components list format "{x; y; z}".
32   * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by
33   * any user-defined strings. The number format for components can be configured.</p>
34   * <p>White space is ignored at parse time, even if it is in the prefix, suffix
35   * or separator specifications. So even if the default separator does include a space
36   * character that is used at format time, both input string "{1;1;1}" and
37   * " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be
38   * returned. In the second case, however, the parse position after parsing will be
39   * just after the closing curly brace, i.e. just before the trailing space.</p>
40   *
41   * @version $Id: Vector3DFormat.java 1416643 2012-12-03 19:37:14Z tn $
42   */
43  public class Vector3DFormat extends VectorFormat<Euclidean3D> {
44  
45      /**
46       * Create an instance with default settings.
47       * <p>The instance uses the default prefix, suffix and separator:
48       * "{", "}", and "; " and the default number format for components.</p>
49       */
50      public Vector3DFormat() {
51          super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR,
52                CompositeFormat.getDefaultNumberFormat());
53      }
54  
55      /**
56       * Create an instance with a custom number format for components.
57       * @param format the custom format for components.
58       */
59      public Vector3DFormat(final NumberFormat format) {
60          super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
61      }
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      /**
75       * Create an instance with custom prefix, suffix, separator and format
76       * for components.
77       * @param prefix prefix to use instead of the default "{"
78       * @param suffix suffix to use instead of the default "}"
79       * @param separator separator to use instead of the default "; "
80       * @param format the custom format for components.
81       */
82      public Vector3DFormat(final String prefix, final String suffix,
83                           final String separator, final NumberFormat format) {
84          super(prefix, suffix, separator, format);
85      }
86  
87      /**
88       * Returns the default 3D vector format for the current locale.
89       * @return the default 3D vector format.
90       */
91      public static Vector3DFormat getInstance() {
92          return getInstance(Locale.getDefault());
93      }
94  
95      /**
96       * Returns the default 3D vector format for the given locale.
97       * @param locale the specific locale used by the format.
98       * @return the 3D vector format specific to the given locale.
99       */
100     public static Vector3DFormat getInstance(final Locale locale) {
101         return new Vector3DFormat(CompositeFormat.getDefaultNumberFormat(locale));
102     }
103 
104     /**
105      * Formats a {@link Vector3D} object to produce a string.
106      * @param vector the object to format.
107      * @param toAppendTo where the text is to be appended
108      * @param pos On input: an alignment field, if desired. On output: the
109      *            offsets of the alignment field
110      * @return the value passed in as toAppendTo.
111      */
112     @Override
113     public StringBuffer format(final Vector<Euclidean3D> vector, final StringBuffer toAppendTo,
114                                final FieldPosition pos) {
115         final Vector3D v3 = (Vector3D) vector;
116         return format(toAppendTo, pos, v3.getX(), v3.getY(), v3.getZ());
117     }
118 
119     /**
120      * Parses a string to produce a {@link Vector3D} object.
121      * @param source the string to parse
122      * @return the parsed {@link Vector3D} object.
123      * @throws MathParseException if the beginning of the specified string
124      * cannot be parsed.
125      */
126     @Override
127     public Vector3D parse(final String source) throws MathParseException {
128         ParsePosition parsePosition = new ParsePosition(0);
129         Vector3D result = parse(source, parsePosition);
130         if (parsePosition.getIndex() == 0) {
131             throw new MathParseException(source,
132                                          parsePosition.getErrorIndex(),
133                                          Vector3D.class);
134         }
135         return result;
136     }
137 
138     /**
139      * Parses a string to produce a {@link Vector3D} object.
140      * @param source the string to parse
141      * @param pos input/ouput parsing parameter.
142      * @return the parsed {@link Vector3D} object.
143      */
144     @Override
145     public Vector3D parse(final String source, final ParsePosition pos) {
146         final double[] coordinates = parseCoordinates(3, source, pos);
147         if (coordinates == null) {
148             return null;
149         }
150         return new Vector3D(coordinates[0], coordinates[1], coordinates[2]);
151     }
152 
153 }