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 018package org.apache.commons.math3.geometry.euclidean.oned; 019 020import java.text.FieldPosition; 021import java.text.NumberFormat; 022import java.text.ParsePosition; 023import java.util.Locale; 024 025import org.apache.commons.math3.exception.MathParseException; 026import org.apache.commons.math3.geometry.Vector; 027import org.apache.commons.math3.geometry.VectorFormat; 028import 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 * <p><b>Note:</b> using "," as a separator may interfere with the grouping separator 041 * of the default {@link NumberFormat} for the current locale. Thus it is advised 042 * to use a {@link NumberFormat} instance with disabled grouping in such a case.</p> 043 * 044 * @since 3.0 045 */ 046public class Vector1DFormat extends VectorFormat<Euclidean1D> { 047 048 /** 049 * Create an instance with default settings. 050 * <p>The instance uses the default prefix, suffix and separator: 051 * "{", "}", and "; " and the default number format for components.</p> 052 */ 053 public Vector1DFormat() { 054 super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, 055 CompositeFormat.getDefaultNumberFormat()); 056 } 057 058 /** 059 * Create an instance with a custom number format for components. 060 * @param format the custom format for components. 061 */ 062 public Vector1DFormat(final NumberFormat format) { 063 super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); 064 } 065 066 /** 067 * Create an instance with custom prefix, suffix and separator. 068 * @param prefix prefix to use instead of the default "{" 069 * @param suffix suffix to use instead of the default "}" 070 */ 071 public Vector1DFormat(final String prefix, final String suffix) { 072 super(prefix, suffix, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); 073 } 074 075 /** 076 * Create an instance with custom prefix, suffix, separator and format 077 * for components. 078 * @param prefix prefix to use instead of the default "{" 079 * @param suffix suffix to use instead of the default "}" 080 * @param format the custom format for components. 081 */ 082 public Vector1DFormat(final String prefix, final String suffix, 083 final NumberFormat format) { 084 super(prefix, suffix, DEFAULT_SEPARATOR, format); 085 } 086 087 /** 088 * Returns the default 1D vector format for the current locale. 089 * @return the default 1D vector format. 090 */ 091 public static Vector1DFormat getInstance() { 092 return getInstance(Locale.getDefault()); 093 } 094 095 /** 096 * Returns the default 1D vector format for the given locale. 097 * @param locale the specific locale used by the format. 098 * @return the 1D vector format specific to the given locale. 099 */ 100 public static Vector1DFormat getInstance(final Locale locale) { 101 return new Vector1DFormat(CompositeFormat.getDefaultNumberFormat(locale)); 102 } 103 104 /** {@inheritDoc} */ 105 @Override 106 public StringBuffer format(final Vector<Euclidean1D> vector, final StringBuffer toAppendTo, 107 final FieldPosition pos) { 108 final Vector1D p1 = (Vector1D) vector; 109 return format(toAppendTo, pos, p1.getX()); 110 } 111 112 /** {@inheritDoc} */ 113 @Override 114 public Vector1D parse(final String source) throws MathParseException { 115 ParsePosition parsePosition = new ParsePosition(0); 116 Vector1D result = parse(source, parsePosition); 117 if (parsePosition.getIndex() == 0) { 118 throw new MathParseException(source, 119 parsePosition.getErrorIndex(), 120 Vector1D.class); 121 } 122 return result; 123 } 124 125 /** {@inheritDoc} */ 126 @Override 127 public Vector1D parse(final String source, final ParsePosition pos) { 128 final double[] coordinates = parseCoordinates(1, source, pos); 129 if (coordinates == null) { 130 return null; 131 } 132 return new Vector1D(coordinates[0]); 133 } 134 135}