Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 42620 invoked from network); 6 Jan 2011 11:32:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 6 Jan 2011 11:32:40 -0000 Received: (qmail 27668 invoked by uid 500); 6 Jan 2011 11:32:40 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 27345 invoked by uid 500); 6 Jan 2011 11:32:37 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 27338 invoked by uid 99); 6 Jan 2011 11:32:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Jan 2011 11:32:37 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Jan 2011 11:32:29 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 80FAE2388A1C; Thu, 6 Jan 2011 11:32:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1055835 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/complex/ main/java/org/apache/commons/math/geometry/ main/java/org/apache/commons/math/linear/ main/java/org/apache/commons/math/util/ site/xdoc/ test/java/org/... Date: Thu, 06 Jan 2011 11:32:08 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110106113208.80FAE2388A1C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erans Date: Thu Jan 6 11:32:07 2011 New Revision: 1055835 URL: http://svn.apache.org/viewvc?rev=1055835&view=rev Log: MATH-461 "RealVectorFormat", "ComplexFormat", "Vector3DFormat" do not inherit anymore from the Java standard "Format". Removed methods that were mandated by this base class (because they were not compatible with the new policy that CM should only throw unchecked exceptions). "CompositeFormat" now contains only static methods: Made it a utility class (i.e. with a "private" constructor). Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3DFormat.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVectorFormat.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/CompositeFormat.java commons/proper/math/trunk/src/site/xdoc/changes.xml commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/Vector3DFormatAbstractTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/ComplexFormat.java Thu Jan 6 11:32:07 2011 @@ -19,13 +19,12 @@ package org.apache.commons.math.complex; 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.MathRuntimeException; import org.apache.commons.math.util.CompositeFormat; import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.exception.MathParseException; import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.NullArgumentException; import org.apache.commons.math.exception.NoDataException; @@ -37,20 +36,15 @@ import org.apache.commons.math.exception * * @version $Revision$ $Date$ */ -public class ComplexFormat extends CompositeFormat { - +public class ComplexFormat { /** Serializable version identifier */ private static final long serialVersionUID = -3343698360149467646L; - /** The default imaginary character. */ private static final String DEFAULT_IMAGINARY_CHARACTER = "i"; - /** The notation used to signify the imaginary part of the complex number. */ private String imaginaryCharacter; - /** The format used for the imaginary part. */ private NumberFormat imaginaryFormat; - /** The format used for the real part. */ private NumberFormat realFormat; @@ -59,7 +53,7 @@ public class ComplexFormat extends Compo * default number format for both real and imaginary parts. */ public ComplexFormat() { - this(DEFAULT_IMAGINARY_CHARACTER, getDefaultNumberFormat()); + this(DEFAULT_IMAGINARY_CHARACTER, CompositeFormat.getDefaultNumberFormat()); } /** @@ -87,7 +81,7 @@ public class ComplexFormat extends Compo * @param imaginaryCharacter The custom imaginary character. */ public ComplexFormat(String imaginaryCharacter) { - this(imaginaryCharacter, getDefaultNumberFormat()); + this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat()); } /** @@ -126,14 +120,23 @@ public class ComplexFormat extends Compo } /** - * This static method calls {@link #format(Object)} on a default instance of - * ComplexFormat. + * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. + * + * @param c Complex object to format. + * @return A formatted number in the form "Re(c) + Im(c)i". + */ + public String format(Complex c) { + return format(c, new StringBuffer(), new FieldPosition(0)).toString(); + } + + /** + * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * - * @param c Complex object to format - * @return A formatted number in the form "Re(c) + Im(c)i" + * @param c Double object to format. + * @return A formatted number. */ - public static String formatComplex(Complex c) { - return getInstance().format(c); + public String format(Double c) { + return format(new Complex(c, 0), new StringBuffer(), new FieldPosition(0)).toString(); } /** @@ -146,24 +149,23 @@ public class ComplexFormat extends Compo * @return the value passed in as toAppendTo. */ public StringBuffer format(Complex complex, StringBuffer toAppendTo, - FieldPosition pos) { - + FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); // format real double re = complex.getReal(); - formatDouble(re, getRealFormat(), toAppendTo, pos); + CompositeFormat.formatDouble(re, getRealFormat(), toAppendTo, pos); // format sign and imaginary double im = complex.getImaginary(); if (im < 0.0) { toAppendTo.append(" - "); - formatDouble(-im, getImaginaryFormat(), toAppendTo, pos); + CompositeFormat.formatDouble(-im, getImaginaryFormat(), toAppendTo, pos); toAppendTo.append(getImaginaryCharacter()); } else if (im > 0.0 || Double.isNaN(im)) { toAppendTo.append(" + "); - formatDouble(im, getImaginaryFormat(), toAppendTo, pos); + CompositeFormat.formatDouble(im, getImaginaryFormat(), toAppendTo, pos); toAppendTo.append(getImaginaryCharacter()); } @@ -183,17 +185,16 @@ public class ComplexFormat extends Compo * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws IllegalArgumentException is {@code obj} is not a valid type. */ - @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, - FieldPosition pos) { + FieldPosition pos) { StringBuffer ret = null; if (obj instanceof Complex) { ret = format( (Complex)obj, toAppendTo, pos); } else if (obj instanceof Number) { - ret = format( new Complex(((Number)obj).doubleValue(), 0.0), - toAppendTo, pos); + ret = format(new Complex(((Number)obj).doubleValue(), 0.0), + toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX, obj.getClass().getName()); @@ -232,7 +233,7 @@ public class ComplexFormat extends Compo * @return the complex format specific to the given locale. */ public static ComplexFormat getInstance(Locale locale) { - NumberFormat f = getDefaultNumberFormat(locale); + NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale); return new ComplexFormat(f); } @@ -247,18 +248,18 @@ public class ComplexFormat extends Compo /** * Parses a string to produce a {@link Complex} object. * - * @param source the string to parse + * @param source the string to parse. * @return the parsed {@link Complex} object. - * @exception ParseException if the beginning of the specified string - * cannot be parsed. + * @throws MathParseException if the beginning of the specified string + * cannot be parsed. */ - public Complex parse(String source) throws ParseException { + public Complex parse(String source) { ParsePosition parsePosition = new ParsePosition(0); Complex result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { - throw MathRuntimeException.createParseException( - parsePosition.getErrorIndex(), - LocalizedFormats.UNPARSEABLE_COMPLEX_NUMBER, source); + throw new MathParseException(source, + parsePosition.getErrorIndex(), + Complex.class); } return result; } @@ -274,10 +275,10 @@ public class ComplexFormat extends Compo int initialIndex = pos.getIndex(); // parse whitespace - parseAndIgnoreWhitespace(source, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse real - Number re = parseNumber(source, getRealFormat(), pos); + Number re = CompositeFormat.parseNumber(source, getRealFormat(), pos); if (re == null) { // invalid real number // set index back to initial, error index should already be set @@ -287,7 +288,7 @@ public class ComplexFormat extends Compo // parse sign int startIndex = pos.getIndex(); - char c = parseNextCharacter(source, pos); + char c = CompositeFormat.parseNextCharacter(source, pos); int sign = 0; switch (c) { case 0 : @@ -310,10 +311,10 @@ public class ComplexFormat extends Compo } // parse whitespace - parseAndIgnoreWhitespace(source, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse imaginary - Number im = parseNumber(source, getRealFormat(), pos); + Number im = CompositeFormat.parseNumber(source, getRealFormat(), pos); if (im == null) { // invalid imaginary number // set index back to initial, error index should already be set @@ -322,7 +323,7 @@ public class ComplexFormat extends Compo } // parse imaginary character - if (!parseFixedstring(source, getImaginaryCharacter(), pos)) { + if (!CompositeFormat.parseFixedstring(source, getImaginaryCharacter(), pos)) { return null; } @@ -331,19 +332,6 @@ public class ComplexFormat extends Compo } /** - * 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) - */ - @Override - public Object parseObject(String source, ParsePosition pos) { - return parse(source, pos); - } - - /** * Modify the imaginaryCharacter. * @param imaginaryCharacter The new imaginaryCharacter value. * @throws NullArgumentException if {@code imaginaryCharacter} is Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3DFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3DFormat.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3DFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector3DFormat.java Thu Jan 6 11:32:07 2011 @@ -19,13 +19,11 @@ 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.MathRuntimeException; -import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.CompositeFormat; +import org.apache.commons.math.exception.MathParseException; /** * Formats a 3D vector in components list format "{x; y; z}". @@ -40,38 +38,27 @@ import org.apache.commons.math.util.Comp * * @version $Revision$ $Date$ */ -public class Vector3DFormat extends CompositeFormat { - +public class Vector3DFormat { /** 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 final NumberFormat format; @@ -81,7 +68,8 @@ public class Vector3DFormat extends Comp * "{", "}", and "; " and the default number format for components.

*/ public Vector3DFormat() { - this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, getDefaultNumberFormat()); + this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, + CompositeFormat.getDefaultNumberFormat()); } /** @@ -100,7 +88,7 @@ public class Vector3DFormat extends Comp */ public Vector3DFormat(final String prefix, final String suffix, final String separator) { - this(prefix, suffix, separator, getDefaultNumberFormat()); + this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** @@ -177,18 +165,17 @@ public class Vector3DFormat extends Comp * @return the 3D vector format specific to the given locale. */ public static Vector3DFormat getInstance(final Locale locale) { - return new Vector3DFormat(getDefaultNumberFormat(locale)); + return new Vector3DFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** - * This static method calls {@link #format(Object)} on a default instance of - * Vector3DFormat. + * This method calls {@link #format(Vector3D,StringBuffer,FieldPosition)}. * - * @param v Vector3D object to format - * @return A formatted vector + * @param v Vector3D object to format. + * @return a formatted vector. */ - public static String formatVector3D(Vector3D v) { - return getInstance().format(v); + public String format(Vector3D v) { + return format(v, new StringBuffer(), new FieldPosition(0)).toString(); } /** @@ -209,58 +196,32 @@ public class Vector3DFormat extends Comp toAppendTo.append(prefix); // format components - formatDouble(vector.getX(), format, toAppendTo, pos); + CompositeFormat.formatDouble(vector.getX(), format, toAppendTo, pos); toAppendTo.append(separator); - formatDouble(vector.getY(), format, toAppendTo, pos); + CompositeFormat.formatDouble(vector.getY(), format, toAppendTo, pos); toAppendTo.append(separator); - formatDouble(vector.getZ(), format, toAppendTo, pos); + CompositeFormat.formatDouble(vector.getZ(), format, toAppendTo, pos); // format suffix toAppendTo.append(suffix); return toAppendTo; - - } - - /** - * Formats a object to produce a string. - *

obj must be a {@link Vector3D} object. Any other type of - * object will result in an {@link IllegalArgumentException} being thrown.

- * @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 obj is not a valid type. - */ - @Override - public StringBuffer format(Object obj, StringBuffer toAppendTo, - FieldPosition pos) { - - if (obj instanceof Vector3D) { - return format( (Vector3D)obj, toAppendTo, pos); - } - - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_3D_VECTOR, - obj.getClass().getName()); - } /** * 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. + * @throws MathParseException if the beginning of the specified string + * cannot be parsed. */ - public Vector3D parse(String source) throws ParseException { + public Vector3D parse(String source) { ParsePosition parsePosition = new ParsePosition(0); Vector3D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { - throw MathRuntimeException.createParseException( - parsePosition.getErrorIndex(), - LocalizedFormats.UNPARSEABLE_3D_VECTOR, source); + throw new MathParseException(source, + parsePosition.getErrorIndex(), + Vector3D.class); } return result; } @@ -275,14 +236,14 @@ public class Vector3DFormat extends Comp int initialIndex = pos.getIndex(); // parse prefix - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedPrefix, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } // parse X component - parseAndIgnoreWhitespace(source, pos); - Number x = parseNumber(source, format, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + Number x = CompositeFormat.parseNumber(source, format, pos); if (x == null) { // invalid abscissa // set index back to initial, error index should already be set @@ -291,12 +252,12 @@ public class Vector3DFormat extends Comp } // parse Y component - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedSeparator, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { return null; } - parseAndIgnoreWhitespace(source, pos); - Number y = parseNumber(source, format, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + Number y = CompositeFormat.parseNumber(source, format, pos); if (y == null) { // invalid ordinate // set index back to initial, error index should already be set @@ -305,12 +266,12 @@ public class Vector3DFormat extends Comp } // parse Z component - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedSeparator, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { return null; } - parseAndIgnoreWhitespace(source, pos); - Number z = parseNumber(source, format, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + Number z = CompositeFormat.parseNumber(source, format, pos); if (z == null) { // invalid height // set index back to initial, error index should already be set @@ -319,25 +280,11 @@ public class Vector3DFormat extends Comp } // parse suffix - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedSuffix, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.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) - */ - @Override - public Object parseObject(String source, ParsePosition pos) { - return parse(source, pos); } - } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java Thu Jan 6 11:32:07 2011 @@ -38,8 +38,7 @@ public class ArrayRealVector extends Abs /** Serializable version identifier. */ private static final long serialVersionUID = -1097961340710804027L; /** Default format. */ - private static final RealVectorFormat DEFAULT_FORMAT = - RealVectorFormat.getInstance(); + private static final RealVectorFormat DEFAULT_FORMAT = RealVectorFormat.getInstance(); /** Entries of the vector. */ protected double data[]; Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVectorFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVectorFormat.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVectorFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/RealVectorFormat.java Thu Jan 6 11:32:07 2011 @@ -25,8 +25,6 @@ import java.util.List; import java.util.Locale; import org.apache.commons.math.exception.MathParseException; -import org.apache.commons.math.exception.MathIllegalArgumentException; -import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.CompositeFormat; /** @@ -43,38 +41,27 @@ import org.apache.commons.math.util.Comp * @version $Revision$ $Date$ * @since 2.0 */ -public class RealVectorFormat extends CompositeFormat { - +public class RealVectorFormat { /** Serializable version identifier */ private static final long serialVersionUID = -708767813036157690L; - /** 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 final NumberFormat format; @@ -84,7 +71,8 @@ public class RealVectorFormat extends Co * "{", "}", and "; " and the default number format for components.

*/ public RealVectorFormat() { - this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, getDefaultNumberFormat()); + this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, + CompositeFormat.getDefaultNumberFormat()); } /** @@ -103,7 +91,8 @@ public class RealVectorFormat extends Co */ public RealVectorFormat(final String prefix, final String suffix, final String separator) { - this(prefix, suffix, separator, getDefaultNumberFormat()); + this(prefix, suffix, separator, + CompositeFormat.getDefaultNumberFormat()); } /** @@ -180,18 +169,17 @@ public class RealVectorFormat extends Co * @return the real vector format specific to the given locale. */ public static RealVectorFormat getInstance(final Locale locale) { - return new RealVectorFormat(getDefaultNumberFormat(locale)); + return new RealVectorFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** - * This static method calls {@link #format(Object)} on a default instance of - * RealVectorFormat. + * This method calls {@link #format(RealVector,StringBuffer,FieldPosition)}. * - * @param v RealVector object to format - * @return A formatted vector + * @param v RealVector object to format. + * @return a formatted vector. */ - public static String formatRealVector(RealVector v) { - return getInstance().format(v); + public String format(RealVector v) { + return format(v, new StringBuffer(), new FieldPosition(0)).toString(); } /** @@ -216,39 +204,13 @@ public class RealVectorFormat extends Co if (i > 0) { toAppendTo.append(separator); } - formatDouble(vector.getEntry(i), format, toAppendTo, pos); + CompositeFormat.formatDouble(vector.getEntry(i), format, toAppendTo, pos); } // format suffix toAppendTo.append(suffix); return toAppendTo; - - } - - /** - * Formats a object to produce a string. - *

obj must be a {@link RealVector} object. Any other type of - * object will result in an {@link IllegalArgumentException} being thrown.

- * @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 MathIllegalArgumentException is {@code obj} is not a valid type. - */ - @Override - public StringBuffer format(Object obj, StringBuffer toAppendTo, - FieldPosition pos) { - - if (obj instanceof RealVector) { - return format((RealVector) obj, toAppendTo, pos); - } - - throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_REAL_VECTOR, - obj.getClass().getName()); - } /** @@ -281,8 +243,8 @@ public class RealVectorFormat extends Co int initialIndex = pos.getIndex(); // parse prefix - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedPrefix, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } @@ -291,15 +253,15 @@ public class RealVectorFormat extends Co for (boolean loop = true; loop;){ if (!components.isEmpty()) { - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedSeparator, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { loop = false; } } if (loop) { - parseAndIgnoreWhitespace(source, pos); - Number component = parseNumber(source, format, pos); + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + Number component = CompositeFormat.parseNumber(source, format, pos); if (component != null) { components.add(component); } else { @@ -313,8 +275,8 @@ public class RealVectorFormat extends Co } // parse suffix - parseAndIgnoreWhitespace(source, pos); - if (!parseFixedstring(source, trimmedSuffix, pos)) { + CompositeFormat.parseAndIgnoreWhitespace(source, pos); + if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { return null; } @@ -325,16 +287,4 @@ public class RealVectorFormat extends Co } return new ArrayRealVector(data, false); } - - /** - * 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) - */ - @Override - public Object parseObject(String source, ParsePosition pos) { - return parse(source, pos); - } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/CompositeFormat.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/CompositeFormat.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/CompositeFormat.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/CompositeFormat.java Thu Jan 6 11:32:07 2011 @@ -17,7 +17,6 @@ package org.apache.commons.math.util; import java.text.FieldPosition; -import java.text.Format; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; @@ -27,18 +26,22 @@ import java.util.Locale; * * @version $Revision$ $Date$ */ -public abstract class CompositeFormat extends Format { - +public class CompositeFormat { /** Serializable version identifier. */ private static final long serialVersionUID = 5358685519349262494L; /** + * Class contains only static methods. + */ + private CompositeFormat() {} + + /** * Create a default number format. The default number format is based on * {@link NumberFormat#getInstance()} with the only customizing that the * maximum number of fraction digits is set to 2. * @return the default number format. */ - protected static NumberFormat getDefaultNumberFormat() { + public static NumberFormat getDefaultNumberFormat() { return getDefaultNumberFormat(Locale.getDefault()); } @@ -49,7 +52,7 @@ public abstract class CompositeFormat ex * @param locale the specific locale used by the format. * @return the default number format specific to the given locale. */ - protected static NumberFormat getDefaultNumberFormat(final Locale locale) { + public static NumberFormat getDefaultNumberFormat(final Locale locale) { final NumberFormat nf = NumberFormat.getInstance(locale); nf.setMaximumFractionDigits(2); return nf; @@ -62,8 +65,8 @@ public abstract class CompositeFormat ex * @param pos input/ouput parsing parameter. On output, pos * holds the index of the next non-whitespace character. */ - protected void parseAndIgnoreWhitespace(final String source, - final ParsePosition pos) { + public static void parseAndIgnoreWhitespace(final String source, + final ParsePosition pos) { parseNextCharacter(source, pos); pos.setIndex(pos.getIndex() - 1); } @@ -75,8 +78,8 @@ public abstract class CompositeFormat ex * @param pos input/ouput parsing parameter. * @return the first non-whitespace character. */ - protected char parseNextCharacter(final String source, - final ParsePosition pos) { + public static char parseNextCharacter(final String source, + final ParsePosition pos) { int index = pos.getIndex(); final int n = source.length(); char ret = 0; @@ -105,8 +108,8 @@ public abstract class CompositeFormat ex * @param pos input/ouput parsing parameter. * @return the special number. */ - private Number parseNumber(final String source, final double value, - final ParsePosition pos) { + private static Number parseNumber(final String source, final double value, + final ParsePosition pos) { Number ret = null; StringBuilder sb = new StringBuilder(); @@ -137,8 +140,8 @@ public abstract class CompositeFormat ex * @param pos input/ouput parsing parameter. * @return the parsed number. */ - protected Number parseNumber(final String source, final NumberFormat format, - final ParsePosition pos) { + public static Number parseNumber(final String source, final NumberFormat format, + final ParsePosition pos) { final int startIndex = pos.getIndex(); Number number = format.parse(source, pos); final int endIndex = pos.getIndex(); @@ -167,8 +170,9 @@ public abstract class CompositeFormat ex * @param pos input/ouput parsing parameter. * @return true if the expected string was there */ - protected boolean parseFixedstring(final String source, final String expected, - final ParsePosition pos) { + public static boolean parseFixedstring(final String source, + final String expected, + final ParsePosition pos) { final int startIndex = pos.getIndex(); final int endIndex = startIndex + expected.length(); @@ -184,7 +188,6 @@ public abstract class CompositeFormat ex // the string was here pos.setIndex(endIndex); return true; - } /** @@ -204,9 +207,9 @@ public abstract class CompositeFormat ex * offsets of the alignment field * @return the value passed in as toAppendTo. */ - protected StringBuffer formatDouble(final double value, final NumberFormat format, - final StringBuffer toAppendTo, - final FieldPosition pos) { + public static StringBuffer formatDouble(final double value, final NumberFormat format, + final StringBuffer toAppendTo, + final FieldPosition pos) { if( Double.isNaN(value) || Double.isInfinite(value) ) { toAppendTo.append('('); toAppendTo.append(value); @@ -216,5 +219,4 @@ public abstract class CompositeFormat ex } return toAppendTo; } - } Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Jan 6 11:32:07 2011 @@ -52,6 +52,13 @@ The type attribute can be add,u If the output is not quite correct, check for invisible trailing spaces! --> + + In order to comply with the new runtime exceptions policy, the classes + "RealVectorFormat", "ComplexFormat", "Vector3DFormat" and "CompositeFormat" + do not inherit anymore from the Java standard "Format" class. Failed parsing + will result in the throwing of a "MathParseException". + "CompositeFormat" is now a utility class ("private" constructor). + By policy, all Commons-Math exceptions must inherit from class "MathRuntimeException". Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java Thu Jan 6 11:32:07 2011 @@ -188,7 +188,7 @@ public class TestUtils { * @param epsilon tolerance */ public static void assertContains(String msg, Complex[] values, - Complex z, double epsilon) { + Complex z, double epsilon) { int i = 0; boolean found = false; while (!found && i < values.length) { @@ -202,7 +202,7 @@ public class TestUtils { } if (!found) { Assert.fail(msg + - " Unable to find " + ComplexFormat.formatComplex(z)); + " Unable to find " + (new ComplexFormat()).format(z)); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java Thu Jan 6 11:32:07 2011 @@ -18,347 +18,370 @@ package org.apache.commons.math.complex; import java.text.NumberFormat; -import java.text.ParseException; import java.text.ParsePosition; import java.util.Locale; +import org.junit.Test; +import org.junit.Assert; + import org.apache.commons.math.util.CompositeFormat; import org.apache.commons.math.util.FastMath; import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.NullArgumentException; +import org.apache.commons.math.exception.MathParseException; -import junit.framework.TestCase; - -public abstract class ComplexFormatAbstractTest extends TestCase { +public abstract class ComplexFormatAbstractTest { - CompositeFormat complexFormat = null; + ComplexFormat complexFormat = null; ComplexFormat complexFormatJ = null; protected abstract Locale getLocale(); protected abstract char getDecimalCharacter(); - @Override - protected void setUp() throws Exception { + protected ComplexFormatAbstractTest() { complexFormat = ComplexFormat.getInstance(getLocale()); complexFormatJ = ComplexFormat.getInstance(getLocale()); complexFormatJ.setImaginaryCharacter("j"); } + @Test public void testSimpleNoDecimals() { Complex c = new Complex(1, 1); String expected = "1 + 1i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testSimpleWithDecimals() { Complex c = new Complex(1.23, 1.43); String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testSimpleWithDecimalsTrunc() { Complex c = new Complex(1.2323, 1.4343); String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeReal() { Complex c = new Complex(-1.2323, 1.4343); String expected = "-1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeImaginary() { Complex c = new Complex(1.2323, -1.4343); String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeBoth() { Complex c = new Complex(-1.2323, -1.4343); String expected = "-1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testZeroReal() { Complex c = new Complex(0.0, -1.4343); String expected = "0 - 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testZeroImaginary() { Complex c = new Complex(30.233, 0); String expected = "30" + getDecimalCharacter() + "23"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testDifferentImaginaryChar() { Complex c = new Complex(1, 1); String expected = "1 + 1j"; String actual = complexFormatJ.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } - public void testStaticFormatComplex() { + @Test + public void testDefaultFormatComplex() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); Complex c = new Complex(232.222, -342.33); String expected = "232" + getDecimalCharacter() + "22 - 342" + getDecimalCharacter() + "33i"; - String actual = ComplexFormat.formatComplex(c); - assertEquals(expected, actual); + String actual = (new ComplexFormat()).format(c); + Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); } + @Test public void testNan() { Complex c = new Complex(Double.NaN, Double.NaN); String expected = "(NaN) + (NaN)i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testPositiveInfinity() { Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); String expected = "(Infinity) + (Infinity)i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeInfinity() { Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); String expected = "(-Infinity) - (Infinity)i"; String actual = complexFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testParseSimpleNoDecimals() { String source = "1 + 1i"; Complex expected = new Complex(1, 1); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseSimpleWithDecimals() { String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; Complex expected = new Complex(1.23, 1.43); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseSimpleWithDecimalsTrunc() { String source = "1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(1.2323, 1.4343); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeReal() { String source = "-1" + getDecimalCharacter() + "2323 + 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(-1.2323, 1.4343); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeImaginary() { String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(1.2323, -1.4343); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeBoth() { String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(-1.2323, -1.4343); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseZeroReal() { String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(0.0, -1.4343); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseZeroImaginary() { String source = "-1" + getDecimalCharacter() + "2323"; Complex expected = new Complex(-1.2323, 0); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseDifferentImaginaryChar() { String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j"; Complex expected = new Complex(-1.2323, -1.4343); try { - Complex actual = (Complex)complexFormatJ.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormatJ.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNan() { String source = "(NaN) + (NaN)i"; Complex expected = new Complex(Double.NaN, Double.NaN); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParsePositiveInfinity() { String source = "(Infinity) + (Infinity)i"; Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testPaseNegativeInfinity() { String source = "(-Infinity) - (Infinity)i"; Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); try { - Complex actual = (Complex)complexFormat.parseObject(source); - assertEquals(expected, actual); - } catch (ParseException ex) { - fail(ex.getMessage()); + Complex actual = (Complex)complexFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testConstructorSingleFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(nf); - assertNotNull(cf); - assertEquals(nf, cf.getRealFormat()); + Assert.assertNotNull(cf); + Assert.assertEquals(nf, cf.getRealFormat()); } + @Test public void testGetImaginaryFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(); - assertNotSame(nf, cf.getImaginaryFormat()); + Assert.assertNotSame(nf, cf.getImaginaryFormat()); cf.setImaginaryFormat(nf); - assertSame(nf, cf.getImaginaryFormat()); + Assert.assertSame(nf, cf.getImaginaryFormat()); } + @Test public void testSetImaginaryFormatNull() { try { ComplexFormat cf = new ComplexFormat(); cf.setImaginaryFormat(null); - fail(); + Assert.fail(); } catch (NullArgumentException ex) { // success } } + @Test public void testSetRealFormatNull() { try { ComplexFormat cf = new ComplexFormat(); cf.setRealFormat(null); - fail(); + Assert.fail(); } catch (NullArgumentException ex) { // success } } + @Test public void testGetRealFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(); - assertNotSame(nf, cf.getRealFormat()); + Assert.assertNotSame(nf, cf.getRealFormat()); cf.setRealFormat(nf); - assertSame(nf, cf.getRealFormat()); + Assert.assertSame(nf, cf.getRealFormat()); } + @Test public void testSetImaginaryCharacterNull() { try { ComplexFormat cf = new ComplexFormat(); cf.setImaginaryCharacter(null); - fail(); + Assert.fail(); } catch (NullArgumentException ex) { // success } } + @Test public void testSetImaginaryCharacterEmpty() { try { ComplexFormat cf = new ComplexFormat(); cf.setImaginaryCharacter(""); - fail(); + Assert.fail(); } catch (MathIllegalArgumentException ex) { // success } } + @Test public void testFormatNumber() { - CompositeFormat cf = ComplexFormat.getInstance(getLocale()); + ComplexFormat cf = ComplexFormat.getInstance(getLocale()); Double pi = Double.valueOf(FastMath.PI); String text = cf.format(pi); - assertEquals("3" + getDecimalCharacter() + "14", text); - } - - public void testFormatObject() { - try { - CompositeFormat cf = new ComplexFormat(); - Object object = new Object(); - cf.format(object); - fail(); - } catch (MathIllegalArgumentException ex) { - // success - } + Assert.assertEquals("3" + getDecimalCharacter() + "14", text); } + @Test public void testForgottenImaginaryCharacter() { ParsePosition pos = new ParsePosition(0); - assertNull(new ComplexFormat().parse("1 + 1", pos)); - assertEquals(5, pos.getErrorIndex()); + Assert.assertNull(new ComplexFormat().parse("1 + 1", pos)); + Assert.assertEquals(5, pos.getErrorIndex()); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/Vector3DFormatAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/Vector3DFormatAbstractTest.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/Vector3DFormatAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/Vector3DFormatAbstractTest.java Thu Jan 6 11:32:07 2011 @@ -18,15 +18,16 @@ 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.junit.Test; +import org.junit.Assert; import org.apache.commons.math.util.CompositeFormat; +import org.apache.commons.math.exception.MathParseException; -public abstract class Vector3DFormatAbstractTest extends TestCase { +public abstract class Vector3DFormatAbstractTest { Vector3DFormat vector3DFormat = null; Vector3DFormat vector3DFormatSquare = null; @@ -35,21 +36,22 @@ public abstract class Vector3DFormatAbst protected abstract char getDecimalCharacter(); - @Override - protected void setUp() throws Exception { + protected Vector3DFormatAbstractTest() { vector3DFormat = Vector3DFormat.getInstance(getLocale()); final NumberFormat nf = NumberFormat.getInstance(getLocale()); nf.setMaximumFractionDigits(2); vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf); } + @Test public void testSimpleNoDecimals() { Vector3D c = new Vector3D(1, 1, 1); String expected = "{1; 1; 1}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testSimpleWithDecimals() { Vector3D c = new Vector3D(1.23, 1.43, 1.63); String expected = @@ -58,9 +60,10 @@ public abstract class Vector3DFormatAbst "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testSimpleWithDecimalsTrunc() { Vector3D c = new Vector3D(1.2323, 1.4343, 1.6333); String expected = @@ -69,9 +72,10 @@ public abstract class Vector3DFormatAbst "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeX() { Vector3D c = new Vector3D(-1.2323, 1.4343, 1.6333); String expected = @@ -80,9 +84,10 @@ public abstract class Vector3DFormatAbst "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeY() { Vector3D c = new Vector3D(1.2323, -1.4343, 1.6333); String expected = @@ -91,9 +96,10 @@ public abstract class Vector3DFormatAbst "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNegativeZ() { Vector3D c = new Vector3D(1.2323, 1.4343, -1.6333); String expected = @@ -102,17 +108,19 @@ public abstract class Vector3DFormatAbst "43; -1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testNonDefaultSetting() { Vector3D c = new Vector3D(1, 1, 1); String expected = "[1 : 1 : 1]"; String actual = vector3DFormatSquare.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } - public void testStaticFormatVector3D() { + @Test + public void testDefaultFormatVector3D() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); @@ -122,56 +130,62 @@ public abstract class Vector3DFormatAbst "22; -342" + getDecimalCharacter() + "33; 432" + getDecimalCharacter() + "44}"; - String actual = Vector3DFormat.formatVector3D(c); - assertEquals(expected, actual); + String actual = (new Vector3DFormat()).format(c); + Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); } + @Test public void testNan() { Vector3D c = Vector3D.NaN; String expected = "{(NaN); (NaN); (NaN)}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void testPositiveInfinity() { Vector3D c = Vector3D.POSITIVE_INFINITY; String expected = "{(Infinity); (Infinity); (Infinity)}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test public void tesNegativeInfinity() { Vector3D c = Vector3D.NEGATIVE_INFINITY; String expected = "{(-Infinity); (-Infinity); (-Infinity)}"; String actual = vector3DFormat.format(c); - assertEquals(expected, actual); + Assert.assertEquals(expected, actual); } + @Test 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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test 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()); + Assert.assertEquals(expected, vector3DFormat.parse(source1, pos1)); + Assert.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()); + Assert.assertEquals(expected, vector3DFormat.parse(source2, pos2)); + Assert.assertEquals(source2.length() - 1, pos2.getIndex()); } + @Test public void testParseSimpleWithDecimals() { String source = "{1" + getDecimalCharacter() + @@ -180,13 +194,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseSimpleWithDecimalsTrunc() { String source = "{1" + getDecimalCharacter() + @@ -195,13 +210,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeX() { String source = "{-1" + getDecimalCharacter() + @@ -210,13 +226,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeY() { String source = "{1" + getDecimalCharacter() + @@ -225,13 +242,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeZ() { String source = "{1" + getDecimalCharacter() + @@ -240,13 +258,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNegativeAll() { String source = "{-1" + getDecimalCharacter() + @@ -255,13 +274,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseZeroX() { String source = "{0" + getDecimalCharacter() + @@ -270,13 +290,14 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test public void testParseNonDefaultSetting() { String source = "[1" + getDecimalCharacter() + @@ -285,77 +306,73 @@ public abstract class Vector3DFormatAbst "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()); + Vector3D actual = (Vector3D) vector3DFormatSquare.parse(source); + Assert.assertEquals(expected, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test 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()); + Vector3D actual = (Vector3D) vector3DFormat.parse(source); + Assert.assertEquals(Vector3D.NaN, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test 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()); + Vector3D actual = (Vector3D)vector3DFormat.parse(source); + Assert.assertEquals(Vector3D.POSITIVE_INFINITY, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test 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()); + Vector3D actual = (Vector3D)vector3DFormat.parse(source); + Assert.assertEquals(Vector3D.NEGATIVE_INFINITY, actual); + } catch (MathParseException ex) { + Assert.fail(ex.getMessage()); } } + @Test 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 - } + Assert.assertNotNull(cf); + Assert.assertEquals(nf, cf.getFormat()); } + @Test public void testForgottenPrefix() { ParsePosition pos = new ParsePosition(0); - assertNull(new Vector3DFormat().parse("1; 1; 1}", pos)); - assertEquals(0, pos.getErrorIndex()); + Assert.assertNull(new Vector3DFormat().parse("1; 1; 1}", pos)); + Assert.assertEquals(0, pos.getErrorIndex()); } + @Test public void testForgottenSeparator() { ParsePosition pos = new ParsePosition(0); - assertNull(new Vector3DFormat().parse("{1; 1 1}", pos)); - assertEquals(6, pos.getErrorIndex()); + Assert.assertNull(new Vector3DFormat().parse("{1; 1 1}", pos)); + Assert.assertEquals(6, pos.getErrorIndex()); } + @Test public void testForgottenSuffix() { ParsePosition pos = new ParsePosition(0); - assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos)); - assertEquals(8, pos.getErrorIndex()); + Assert.assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos)); + Assert.assertEquals(8, pos.getErrorIndex()); } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java?rev=1055835&r1=1055834&r2=1055835&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/RealVectorFormatAbstractTest.java Thu Jan 6 11:32:07 2011 @@ -121,7 +121,7 @@ public abstract class RealVectorFormatAb } @Test - public void testStaticFormatRealVectorImpl() { + public void testDefaultFormatRealVectorImpl() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); @@ -131,7 +131,7 @@ public abstract class RealVectorFormatAb "22; -342" + getDecimalCharacter() + "33; 432" + getDecimalCharacter() + "44}"; - String actual = RealVectorFormat.formatRealVector(c); + String actual = (new RealVectorFormat()).format(c); Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); @@ -380,18 +380,6 @@ public abstract class RealVectorFormatAb } @Test - public void testFormatObject() { - try { - CompositeFormat cf = new RealVectorFormat(); - Object object = new Object(); - cf.format(object); - Assert.fail(); - } catch (MathIllegalArgumentException ex) { - // success - } - } - - @Test public void testForgottenPrefix() { ParsePosition pos = new ParsePosition(0); final String source = "1; 1; 1}";