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 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 23 package org.hipparchus.geometry.euclidean.twod; 24 25 import java.text.FieldPosition; 26 import java.text.NumberFormat; 27 import java.text.ParsePosition; 28 import java.util.Locale; 29 30 import org.hipparchus.exception.LocalizedCoreFormats; 31 import org.hipparchus.exception.MathIllegalStateException; 32 import org.hipparchus.geometry.Vector; 33 import org.hipparchus.geometry.VectorFormat; 34 import org.hipparchus.util.CompositeFormat; 35 36 /** 37 * Formats a 2D vector in components list format "{x; y}". 38 * <p>The prefix and suffix "{" and "}" and the separator "; " can be replaced by 39 * any user-defined strings. The number format for components can be configured.</p> 40 * <p>White space is ignored at parse time, even if it is in the prefix, suffix 41 * or separator specifications. So even if the default separator does include a space 42 * character that is used at format time, both input string "{1;1}" and 43 * " { 1 ; 1 } " will be parsed without error and the same vector will be 44 * returned. In the second case, however, the parse position after parsing will be 45 * just after the closing curly brace, i.e. just before the trailing space.</p> 46 * <p><b>Note:</b> using "," as a separator may interfere with the grouping separator 47 * of the default {@link NumberFormat} for the current locale. Thus it is advised 48 * to use a {@link NumberFormat} instance with disabled grouping in such a case.</p> 49 * 50 */ 51 public class Vector2DFormat extends VectorFormat<Euclidean2D, Vector2D> { 52 53 /** 54 * Create an instance with default settings. 55 * <p>The instance uses the default prefix, suffix and separator: 56 * "{", "}", and "; " and the default number format for components.</p> 57 */ 58 public Vector2DFormat() { 59 super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, 60 CompositeFormat.getDefaultNumberFormat()); 61 } 62 63 /** 64 * Create an instance with a custom number format for components. 65 * @param format the custom format for components. 66 */ 67 public Vector2DFormat(final NumberFormat format) { 68 super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); 69 } 70 71 /** 72 * Create an instance with custom prefix, suffix and separator. 73 * @param prefix prefix to use instead of the default "{" 74 * @param suffix suffix to use instead of the default "}" 75 * @param separator separator to use instead of the default "; " 76 */ 77 public Vector2DFormat(final String prefix, final String suffix, 78 final String separator) { 79 super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); 80 } 81 82 /** 83 * Create an instance with custom prefix, suffix, separator and format 84 * for components. 85 * @param prefix prefix to use instead of the default "{" 86 * @param suffix suffix to use instead of the default "}" 87 * @param separator separator to use instead of the default "; " 88 * @param format the custom format for components. 89 */ 90 public Vector2DFormat(final String prefix, final String suffix, 91 final String separator, final NumberFormat format) { 92 super(prefix, suffix, separator, format); 93 } 94 95 /** 96 * Returns the default 2D vector format for the current locale. 97 * @return the default 2D vector format. 98 * @since 1.4 99 */ 100 public static Vector2DFormat getVector2DFormat() { 101 return getVector2DFormat(Locale.getDefault()); 102 } 103 104 /** 105 * Returns the default 2D vector format for the given locale. 106 * @param locale the specific locale used by the format. 107 * @return the 2D vector format specific to the given locale. 108 * @since 1.4 109 */ 110 public static Vector2DFormat getVector2DFormat(final Locale locale) { 111 return new Vector2DFormat(CompositeFormat.getDefaultNumberFormat(locale)); 112 } 113 114 /** {@inheritDoc} */ 115 @Override 116 public StringBuffer format(final Vector<Euclidean2D, Vector2D> vector, final StringBuffer toAppendTo, 117 final FieldPosition pos) { 118 final Vector2D p2 = (Vector2D) vector; 119 return format(toAppendTo, pos, p2.getX(), p2.getY()); 120 } 121 122 /** {@inheritDoc} */ 123 @Override 124 public Vector2D parse(final String source) throws MathIllegalStateException { 125 ParsePosition parsePosition = new ParsePosition(0); 126 Vector2D result = parse(source, parsePosition); 127 if (parsePosition.getIndex() == 0) { 128 throw new MathIllegalStateException(LocalizedCoreFormats.CANNOT_PARSE_AS_TYPE, 129 source, parsePosition.getErrorIndex(), 130 Vector2D.class); 131 } 132 return result; 133 } 134 135 /** {@inheritDoc} */ 136 @Override 137 public Vector2D parse(final String source, final ParsePosition pos) { 138 final double[] coordinates = parseCoordinates(2, source, pos); 139 if (coordinates == null) { 140 return null; 141 } 142 return new Vector2D(coordinates[0], coordinates[1]); 143 } 144 145 }