Added locale support to complex format. Added test cases for specific locales.
PR: 31325 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141451 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ff0fe83175
commit
7ae35df8c0
|
@ -22,6 +22,7 @@ import java.text.Format;
|
|||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Formats a Complex number in cartesian format "Re(c) + Im(c)i". 'i' can
|
||||
|
@ -29,16 +30,13 @@ import java.text.ParsePosition;
|
|||
* can be configured.
|
||||
*
|
||||
* @author Apache Software Foundation
|
||||
* @version $Revision: 1.9 $ $Date: 2004/06/23 16:26:16 $
|
||||
* @version $Revision: 1.10 $ $Date: 2004/09/21 04:45:55 $
|
||||
*/
|
||||
public class ComplexFormat extends Format implements Serializable {
|
||||
|
||||
/** Serializable version identifier */
|
||||
static final long serialVersionUID = -6337346779577272306L;
|
||||
|
||||
/** The default complex format. */
|
||||
private static final ComplexFormat DEFAULT = new ComplexFormat();
|
||||
|
||||
/** The default imaginary character. */
|
||||
private static final String DEFAULT_IMAGINARY_CHARACTER = "i";
|
||||
|
||||
|
@ -122,20 +120,7 @@ public class ComplexFormat extends Format implements Serializable {
|
|||
* @return A formatted number in the form "Re(c) + Im(c)i"
|
||||
*/
|
||||
public static String formatComplex( Complex c ) {
|
||||
return DEFAULT.format( c );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default number format. The default number format is based on
|
||||
* {@link NumberFormat#getInstance()} with the only customizing is the
|
||||
* maximum number of fraction digits, which is set to 2.
|
||||
*
|
||||
* @return the default number format.
|
||||
*/
|
||||
private static NumberFormat getDefaultNumberFormat() {
|
||||
NumberFormat nf = NumberFormat.getInstance();
|
||||
nf.setMaximumFractionDigits(2);
|
||||
return nf;
|
||||
return getInstance().format( c );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -232,6 +217,38 @@ public class ComplexFormat extends Format implements Serializable {
|
|||
return toAppendTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the set of locales for which complex formats are available. This
|
||||
* is the same set as the {@link NumberFormat} set.
|
||||
* @return available complex format locales.
|
||||
*/
|
||||
public static Locale[] getAvailableLocales() {
|
||||
return NumberFormat.getAvailableLocales();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default number format. The default number format is based on
|
||||
* {@link NumberFormat#getInstance()} with the only customizing is the
|
||||
* maximum number of fraction digits, which is set to 2.
|
||||
* @return the default number format.
|
||||
*/
|
||||
private static NumberFormat getDefaultNumberFormat() {
|
||||
return getDefaultNumberFormat(Locale.getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default number format. The default number format is based on
|
||||
* {@link NumberFormat#getInstance(java.util.Locale)} with the only
|
||||
* customizing is the maximum number of fraction digits, which is set to 2.
|
||||
* @param locale the specific locale used by the format.
|
||||
* @return the default number format specific to the given locale.
|
||||
*/
|
||||
private static NumberFormat getDefaultNumberFormat(Locale locale) {
|
||||
NumberFormat nf = NumberFormat.getInstance(locale);
|
||||
nf.setMaximumFractionDigits(2);
|
||||
return nf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the imaginaryCharacter.
|
||||
* @return the imaginaryCharacter.
|
||||
|
@ -248,6 +265,24 @@ public class ComplexFormat extends Format implements Serializable {
|
|||
return imaginaryFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default complex format for the current locale.
|
||||
* @return the default complex format.
|
||||
*/
|
||||
public static ComplexFormat 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 ComplexFormat getInstance(Locale locale) {
|
||||
NumberFormat f = getDefaultNumberFormat(locale);
|
||||
return new ComplexFormat(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the realFormat.
|
||||
* @return the realFormat.
|
||||
|
|
|
@ -0,0 +1,350 @@
|
|||
/*
|
||||
* Copyright 2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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.complex;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public abstract class AbstractComplexFormatTest extends TestCase {
|
||||
|
||||
ComplexFormat complexFormat = null;
|
||||
ComplexFormat complexFormatJ = null;
|
||||
|
||||
protected abstract Locale getLocale();
|
||||
|
||||
protected abstract char getDecimalCharacter();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
complexFormat = ComplexFormat.getInstance(getLocale());
|
||||
complexFormatJ = ComplexFormat.getInstance(getLocale());
|
||||
complexFormatJ.setImaginaryCharacter("j");
|
||||
}
|
||||
|
||||
public void testSimpleNoDecimals() {
|
||||
Complex c = new Complex(1, 1);
|
||||
String expected = "1 + 1i";
|
||||
String actual = complexFormat.format(c);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void testZeroImaginary() {
|
||||
Complex c = new Complex(30.233, 0);
|
||||
String expected = "30" + getDecimalCharacter() + "23";
|
||||
String actual = complexFormat.format(c);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
public void testDifferentImaginaryChar() {
|
||||
Complex c = new Complex(1, 1);
|
||||
String expected = "1 + 1j";
|
||||
String actual = complexFormatJ.format(c);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
public void testStaticFormatComplex() {
|
||||
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);
|
||||
|
||||
Locale.setDefault(defaultLocal);
|
||||
}
|
||||
|
||||
public void testNan() {
|
||||
Complex c = new Complex(Double.NaN, Double.NaN);
|
||||
String expected = "(NaN) + (NaN)i";
|
||||
String actual = complexFormat.format(c);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
public void testConstructorSingleFormat() {
|
||||
NumberFormat nf = NumberFormat.getInstance();
|
||||
ComplexFormat cf = new ComplexFormat(nf);
|
||||
assertNotNull(cf);
|
||||
assertEquals(nf, cf.getRealFormat());
|
||||
}
|
||||
|
||||
public void testGetImaginaryFormat() {
|
||||
NumberFormat nf = NumberFormat.getInstance();
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
|
||||
assertNotSame(nf, cf.getImaginaryFormat());
|
||||
cf.setImaginaryFormat(nf);
|
||||
assertSame(nf, cf.getImaginaryFormat());
|
||||
}
|
||||
|
||||
public void testSetImaginaryFormatNull() {
|
||||
try {
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
cf.setImaginaryFormat(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetRealFormatNull() {
|
||||
try {
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
cf.setRealFormat(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetRealFormat() {
|
||||
NumberFormat nf = NumberFormat.getInstance();
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
|
||||
assertNotSame(nf, cf.getRealFormat());
|
||||
cf.setRealFormat(nf);
|
||||
assertSame(nf, cf.getRealFormat());
|
||||
}
|
||||
|
||||
public void testSetImaginaryCharacterNull() {
|
||||
try {
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
cf.setImaginaryCharacter(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetImaginaryCharacterEmpty() {
|
||||
try {
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
cf.setImaginaryCharacter("");
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
public void testFormatNumber() {
|
||||
ComplexFormat cf = ComplexFormat.getInstance(getLocale());
|
||||
Double pi = new Double(Math.PI);
|
||||
String text = cf.format(pi);
|
||||
assertEquals("3" + getDecimalCharacter() + "14", text);
|
||||
}
|
||||
|
||||
public void testFormatObject() {
|
||||
try {
|
||||
ComplexFormat cf = new ComplexFormat();
|
||||
Object object = new Object();
|
||||
cf.format(object);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright 2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed 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.complex;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
public class FrenchComplexFormatTest extends AbstractComplexFormatTest {
|
||||
|
||||
protected char getDecimalCharacter() {
|
||||
return ',';
|
||||
}
|
||||
|
||||
protected Locale getLocale() {
|
||||
return Locale.FRENCH;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue