added a Vector3DFormat class

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@680029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-07-26 18:31:41 +00:00
parent 7004e490aa
commit ccbe22eb02
6 changed files with 864 additions and 2 deletions

View File

@ -19,6 +19,8 @@ package org.apache.commons.math.geometry;
import java.io.Serializable;
import org.apache.commons.math.util.MathUtils;
/**
* This class implements vectors in a three-dimensional space.
* <p>Instance of this class are guaranteed to be immutable.</p>
@ -50,8 +52,23 @@ public class Vector3D
/** Opposite of the third canonical vector (coordinates: 0, 0, -1). */
public static final Vector3D MINUS_K = new Vector3D(0, 0, -1);
/** A vector with all coordinates set to NaN. */
public static final Vector3D NaN = new Vector3D(Double.NaN, Double.NaN, Double.NaN);
/** A vector with all coordinates set to positive infinity. */
public static final Vector3D POSITIVE_INFINITY =
new Vector3D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
/** A vector with all coordinates set to negative infinity. */
public static final Vector3D NEGATIVE_INFINITY =
new Vector3D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
/** Default format. */
private static final Vector3DFormat DEFAULT_FORMAT =
Vector3DFormat.getInstance();
/** Serializable version identifier. */
private static final long serialVersionUID = -6155041477622120793L;
private static final long serialVersionUID = 5133268763396045979L;
/** Abscissa. */
private final double x;
@ -337,6 +354,84 @@ public class Vector3D
return new Vector3D(a * x, a * y, a * z);
}
/**
* Returns true if any coordinate of this vector is NaN; false otherwise
* @return true if any coordinate of this vector is NaN; false otherwise
*/
public boolean isNaN() {
return Double.isNaN(x) || Double.isNaN(y) || Double.isNaN(z);
}
/**
* Returns true if any coordinate of this vector is infinite and none are NaN;
* false otherwise
* @return true if any coordinate of this vector is infinite and none are NaN;
* false otherwise
*/
public boolean isInfinite() {
return !isNaN() && (Double.isInfinite(x) || Double.isInfinite(y) || Double.isInfinite(z));
}
/**
* Test for the equality of two 3D vectors.
* <p>
* If all coordinates of two 3D vectors are exactly the same, and none are
* <code>Double.NaN</code>, the two 3D vectors are considered to be equal.
* </p>
* <p>
* All <code>NaN</code> values are considered to be equal - i.e, if either
* (or all) coordinates of the 3D vector are equal to <code>Double.NaN</code>,
* the complex number is equal to
* <code>Complex.NaN</code>.</p>
*
* @param other Object to test for equality to this
* @return true if two 3D vector objects are equal, false if
* object is null, not an instance of Vector3D, or
* not equal to this Vector3D instance
*
*/
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
try {
Vector3D rhs = (Vector3D)other;
if (rhs.isNaN()) {
return this.isNaN();
}
return (Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(rhs.x)) &&
(Double.doubleToRawLongBits(y) == Double.doubleToRawLongBits(rhs.y)) &&
(Double.doubleToRawLongBits(z) == Double.doubleToRawLongBits(rhs.z));
} catch (ClassCastException ex) {
// ignore exception
return false;
}
}
/**
* Get a hashCode for the 3D vector.
* <p>
* All NaN values have the same hash code.</p>
*
* @return a hash code value for this object
*/
public int hashCode() {
if (isNaN()) {
return 8;
}
return 31 * (23 * MathUtils.hash(x) + 19 * MathUtils.hash(y) + MathUtils.hash(z));
}
/** Compute the dot-product of two vectors.
* @param v1 first vector
* @param v2 second vector
@ -387,4 +482,11 @@ public class Vector3D
return dx * dx + dy * dy + dz * dz;
}
/** Get a string representation of this vector.
* @return a string representation of this vector
*/
public String toString() {
return DEFAULT_FORMAT.format(this);
}
}

View File

@ -0,0 +1,337 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.geometry;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math.util.CompositeFormat;
/**
* Formats a 3D vector in components list format "{x; y; z}".
* <p>The prefix and suffix "{" and "}" and the separator ", " can be replaced by
* any user-defined strings. The number format for components can be configured.</p>
* <p>White space is ignored at parse time, even if it is in the prefix, suffix
* or separator specifications. So even if the default separator does include a space
* character that is used at format time, both input string "{1;1;1}" and
* " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be
* returned. In the second case, however, the parse position after parsing will be
* just after the closing curly brace, i.e. just before the trailing space.</p>
*
* @version $Revision$ $Date$
*/
public class Vector3DFormat extends CompositeFormat {
/** Serializable version identifier */
private static final long serialVersionUID = -5447606608652576301L;
/** The default prefix: "{". */
private static final String DEFAULT_PREFIX = "{";
/** The default suffix: "}". */
private static final String DEFAULT_SUFFIX = "}";
/** The default separator: ", ". */
private static final String DEFAULT_SEPARATOR = "; ";
/** Prefix. */
private final String prefix;
/** Suffix. */
private final String suffix;
/** Separator. */
private final String separator;
/** Trimmed prefix. */
private final String trimmedPrefix;
/** Trimmed suffix. */
private final String trimmedSuffix;
/** Trimmed separator. */
private final String trimmedSeparator;
/** The format used for components. */
private NumberFormat format;
/**
* Create an instance with default settings.
* <p>The instance uses the default prefix, suffix and separator:
* "{", "}", and "; " and the default number format for components.</p>
*/
public Vector3DFormat() {
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, getDefaultNumberFormat());
}
/**
* Create an instance with a custom number format for components.
* @param format the custom format for components.
*/
public Vector3DFormat(final NumberFormat format) {
this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format);
}
/**
* Create an instance with custom prefix, suffix and separator.
* @param prefix prefix to use instead of the default "{"
* @param suffix suffix to use instead of the default "}"
* @param separator separator to use instead of the default "; "
*/
public Vector3DFormat(final String prefix, final String suffix,
final String separator) {
this(prefix, suffix, separator, getDefaultNumberFormat());
}
/**
* Create an instance with custom prefix, suffix, separator and format
* for components.
* @param prefix prefix to use instead of the default "{"
* @param suffix suffix to use instead of the default "}"
* @param separator separator to use instead of the default "; "
* @param format the custom format for components.
*/
public Vector3DFormat(final String prefix, final String suffix,
final String separator, final NumberFormat format) {
this.prefix = prefix;
this.suffix = suffix;
this.separator = separator;
trimmedPrefix = prefix.trim();
trimmedSuffix = suffix.trim();
trimmedSeparator = separator.trim();
this.format = format;
}
/**
* Get the set of locales for which 3D vectors formats are available.
* <p>This is the same set as the {@link NumberFormat} set.</p>
* @return available complex format locales.
*/
public static Locale[] getAvailableLocales() {
return NumberFormat.getAvailableLocales();
}
/**
* Get the format prefix.
* @return format prefix.
*/
public String getPrefix() {
return prefix;
}
/**
* Get the format suffix.
* @return format suffix.
*/
public String getSuffix() {
return suffix;
}
/**
* Get the format separator between components.
* @return format separator.
*/
public String getSeparator() {
return separator;
}
/**
* Get the components format.
* @return components format.
*/
public NumberFormat getFormat() {
return format;
}
/**
* Returns the default complex format for the current locale.
* @return the default complex format.
*/
public static Vector3DFormat getInstance() {
return getInstance(Locale.getDefault());
}
/**
* Returns the default complex format for the given locale.
* @param locale the specific locale used by the format.
* @return the complex format specific to the given locale.
*/
public static Vector3DFormat getInstance(final Locale locale) {
return new Vector3DFormat(getDefaultNumberFormat(locale));
}
/**
* This static method calls {@link #format(Object)} on a default instance of
* Vector3DFormat.
*
* @param v Vector3D object to format
* @return A formatted vector
*/
public static String formatVector3D(Vector3D v) {
return getInstance().format(v);
}
/**
* Formats a {@link Vector3D} object to produce a string.
* @param vector the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(Vector3D vector, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
formatDouble(vector.getX(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getY(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getZ(), format, toAppendTo, pos);
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
/**
* Formats a object to produce a string.
* <p><code>obj</code> must be a {@link Vector3D} object. Any other type of
* object will result in an {@link IllegalArgumentException} being thrown.</p>
* @param obj the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
* @throws IllegalArgumentException is <code>obj</code> is not a valid type.
*/
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
if (obj instanceof Vector3D) {
return format( (Vector3D)obj, toAppendTo, pos);
}
throw new IllegalArgumentException("Cannot format given Object as a Vector3D");
}
/**
* Parses a string to produce a {@link Vector3D} object.
* @param source the string to parse
* @return the parsed {@link Vector3D} object.
* @exception ParseException if the beginning of the specified string
* cannot be parsed.
*/
public Vector3D parse(String source) throws ParseException {
ParsePosition parsePosition = new ParsePosition(0);
Vector3D result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable 3D vector: \"" + source +
"\"", parsePosition.getErrorIndex());
}
return result;
}
/**
* Parses a string to produce a {@link Vector3D} object.
* @param source the string to parse
* @param pos input/ouput parsing parameter.
* @return the parsed {@link Vector3D} object.
*/
public Vector3D parse(String source, ParsePosition pos) {
int initialIndex = pos.getIndex();
// parse prefix
parseAndIgnoreWhitespace(source, pos);
if (!parseFixedstring(source, trimmedPrefix, pos)) {
return null;
}
// parse X component
parseAndIgnoreWhitespace(source, pos);
Number x = parseNumber(source, format, pos);
if (x == null) {
// invalid abscissa
// set index back to initial, error index should already be set
pos.setIndex(initialIndex);
return null;
}
// parse Y component
parseAndIgnoreWhitespace(source, pos);
if (!parseFixedstring(source, trimmedSeparator, pos)) {
return null;
}
parseAndIgnoreWhitespace(source, pos);
Number y = parseNumber(source, format, pos);
if (y == null) {
// invalid ordinate
// set index back to initial, error index should already be set
pos.setIndex(initialIndex);
return null;
}
// parse Z component
parseAndIgnoreWhitespace(source, pos);
if (!parseFixedstring(source, trimmedSeparator, pos)) {
return null;
}
parseAndIgnoreWhitespace(source, pos);
Number z = parseNumber(source, format, pos);
if (z == null) {
// invalid height
// set index back to initial, error index should already be set
pos.setIndex(initialIndex);
return null;
}
// parse suffix
parseAndIgnoreWhitespace(source, pos);
if (!parseFixedstring(source, trimmedSuffix, pos)) {
return null;
}
return new Vector3D(x.doubleValue(), y.doubleValue(), z.doubleValue());
}
/**
* Parses a string to produce a object.
* @param source the string to parse
* @param pos input/ouput parsing parameter.
* @return the parsed object.
* @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
*/
public Object parseObject(String source, ParsePosition pos) {
return parse(source, pos);
}
}

View File

@ -94,7 +94,7 @@ The <action> type attribute can be add,update,fix,remove.
handlers to be embedded into users Serializable classes.
</action>
<action dev="luc" type="add">
Added several convenience methods for Vector3D and Rotation.
Added several convenience methods and constants for Vector3D and Rotation.
</action>
<action dev="luc" type="update">
Replaced public no argument constructors with IDENTITY or ZERO

View File

@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.geometry;
import java.util.Locale;
public class FrenchVector3DFormatTest extends Vector3DFormatAbstractTest {
protected char getDecimalCharacter() {
return ',';
}
protected Locale getLocale() {
return Locale.FRENCH;
}
}

View File

@ -0,0 +1,360 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.geometry;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import junit.framework.TestCase;
import org.apache.commons.math.util.CompositeFormat;
public abstract class Vector3DFormatAbstractTest extends TestCase {
Vector3DFormat vector3DFormat = null;
Vector3DFormat vector3DFormatSquare = null;
protected abstract Locale getLocale();
protected abstract char getDecimalCharacter();
protected void setUp() throws Exception {
vector3DFormat = Vector3DFormat.getInstance(getLocale());
final NumberFormat nf = NumberFormat.getInstance(getLocale());
nf.setMaximumFractionDigits(2);
vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf);
}
public void testSimpleNoDecimals() {
Vector3D c = new Vector3D(1, 1, 1);
String expected = "{1; 1; 1}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testSimpleWithDecimals() {
Vector3D c = new Vector3D(1.23, 1.43, 1.63);
String expected =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43; 1" + getDecimalCharacter() +
"63}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testSimpleWithDecimalsTrunc() {
Vector3D c = new Vector3D(1.2323, 1.4343, 1.6333);
String expected =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43; 1" + getDecimalCharacter() +
"63}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeX() {
Vector3D c = new Vector3D(-1.2323, 1.4343, 1.6333);
String expected =
"{-1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43; 1" + getDecimalCharacter() +
"63}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeY() {
Vector3D c = new Vector3D(1.2323, -1.4343, 1.6333);
String expected =
"{1" + getDecimalCharacter() +
"23; -1" + getDecimalCharacter() +
"43; 1" + getDecimalCharacter() +
"63}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testNegativeZ() {
Vector3D c = new Vector3D(1.2323, 1.4343, -1.6333);
String expected =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43; -1" + getDecimalCharacter() +
"63}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testNonDefaultSetting() {
Vector3D c = new Vector3D(1, 1, 1);
String expected = "[1 : 1 : 1]";
String actual = vector3DFormatSquare.format(c);
assertEquals(expected, actual);
}
public void testStaticFormatVector3D() {
Locale defaultLocal = Locale.getDefault();
Locale.setDefault(getLocale());
Vector3D c = new Vector3D(232.222, -342.33, 432.444);
String expected =
"{232" + getDecimalCharacter() +
"22; -342" + getDecimalCharacter() +
"33; 432" + getDecimalCharacter() +
"44}";
String actual = Vector3DFormat.formatVector3D(c);
assertEquals(expected, actual);
Locale.setDefault(defaultLocal);
}
public void testNan() {
Vector3D c = Vector3D.NaN;
String expected = "{(NaN); (NaN); (NaN)}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testPositiveInfinity() {
Vector3D c = Vector3D.POSITIVE_INFINITY;
String expected = "{(Infinity); (Infinity); (Infinity)}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void tesNegativeInfinity() {
Vector3D c = Vector3D.NEGATIVE_INFINITY;
String expected = "{(-Infinity); (-Infinity); (-Infinity)}";
String actual = vector3DFormat.format(c);
assertEquals(expected, actual);
}
public void testParseSimpleNoDecimals() {
String source = "{1; 1; 1}";
Vector3D expected = new Vector3D(1, 1, 1);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseIgnoredWhitespace() {
Vector3D expected = new Vector3D(1, 1, 1);
ParsePosition pos1 = new ParsePosition(0);
String source1 = "{1;1;1}";
assertEquals(expected, vector3DFormat.parseObject(source1, pos1));
assertEquals(source1.length(), pos1.getIndex());
ParsePosition pos2 = new ParsePosition(0);
String source2 = " { 1 ; 1 ; 1 } ";
assertEquals(expected, vector3DFormat.parseObject(source2, pos2));
assertEquals(source2.length() - 1, pos2.getIndex());
}
public void testParseSimpleWithDecimals() {
String source =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43; 1" + getDecimalCharacter() +
"63}";
Vector3D expected = new Vector3D(1.23, 1.43, 1.63);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseSimpleWithDecimalsTrunc() {
String source =
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343; 1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeX() {
String source =
"{-1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343; 1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(-1.2323, 1.4343, 1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeY() {
String source =
"{1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343; 1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(1.2323, -1.4343, 1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeZ() {
String source =
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343; -1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(1.2323, 1.4343, -1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeAll() {
String source =
"{-1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343; -1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(-1.2323, -1.4343, -1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseZeroX() {
String source =
"{0" + getDecimalCharacter() +
"0; -1" + getDecimalCharacter() +
"4343; 1" + getDecimalCharacter() +
"6333}";
Vector3D expected = new Vector3D(0.0, -1.4343, 1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNonDefaultSetting() {
String source =
"[1" + getDecimalCharacter() +
"2323 : 1" + getDecimalCharacter() +
"4343 : 1" + getDecimalCharacter() +
"6333]";
Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333);
try {
Vector3D actual = (Vector3D) vector3DFormatSquare.parseObject(source);
assertEquals(expected, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNan() {
String source = "{(NaN); (NaN); (NaN)}";
try {
Vector3D actual = (Vector3D) vector3DFormat.parseObject(source);
assertEquals(Vector3D.NaN, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParsePositiveInfinity() {
String source = "{(Infinity); (Infinity); (Infinity)}";
try {
Vector3D actual = (Vector3D)vector3DFormat.parseObject(source);
assertEquals(Vector3D.POSITIVE_INFINITY, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testParseNegativeInfinity() {
String source = "{(-Infinity); (-Infinity); (-Infinity)}";
try {
Vector3D actual = (Vector3D)vector3DFormat.parseObject(source);
assertEquals(Vector3D.NEGATIVE_INFINITY, actual);
} catch (ParseException ex) {
fail(ex.getMessage());
}
}
public void testConstructorSingleFormat() {
NumberFormat nf = NumberFormat.getInstance();
Vector3DFormat cf = new Vector3DFormat(nf);
assertNotNull(cf);
assertEquals(nf, cf.getFormat());
}
public void testFormatObject() {
try {
CompositeFormat cf = new Vector3DFormat();
Object object = new Object();
cf.format(object);
fail();
} catch (IllegalArgumentException ex) {
// success
}
}
public void testForgottenPrefix() {
ParsePosition pos = new ParsePosition(0);
assertNull(new Vector3DFormat().parse("1; 1; 1}", pos));
assertEquals(0, pos.getErrorIndex());
}
public void testForgottenSeparator() {
ParsePosition pos = new ParsePosition(0);
assertNull(new Vector3DFormat().parse("{1; 1 1}", pos));
assertEquals(6, pos.getErrorIndex());
}
public void testForgottenSuffix() {
ParsePosition pos = new ParsePosition(0);
assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos));
assertEquals(8, pos.getErrorIndex());
}
}

View File

@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.geometry;
import java.util.Locale;
public class Vector3DFormatTest extends Vector3DFormatAbstractTest {
protected char getDecimalCharacter() {
return '.';
}
protected Locale getLocale() {
return Locale.US;
}
}