Removed setters. Removed coresponding unit tests.
Added a "getInstance" method so that some tests can still work.
Removed a seemingly unnecessary call to the "clone" method.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1056493 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-01-07 20:33:12 +00:00
parent fa4135a048
commit c8e8d8de18
2 changed files with 42 additions and 103 deletions

View File

@ -91,23 +91,43 @@ public class ComplexFormat {
* @param format the custom format for both real and imaginary parts. * @param format the custom format for both real and imaginary parts.
*/ */
public ComplexFormat(String imaginaryCharacter, NumberFormat format) { public ComplexFormat(String imaginaryCharacter, NumberFormat format) {
this(imaginaryCharacter, format, (NumberFormat)format.clone()); this(imaginaryCharacter, format, format);
} }
/** /**
* Create an instance with a custom imaginary character, a custom number * Create an instance with a custom imaginary character, a custom number
* format for the real part, and a custom number format for the imaginary * format for the real part, and a custom number format for the imaginary
* part. * part.
*
* @param imaginaryCharacter The custom imaginary character. * @param imaginaryCharacter The custom imaginary character.
* @param realFormat the custom format for the real part. * @param realFormat the custom format for the real part.
* @param imaginaryFormat the custom format for the imaginary part. * @param imaginaryFormat the custom format for the imaginary part.
* @throws NullArgumentException if {@code imaginaryCharacter} is
* {@code null}.
* @throws NoDataException if {@code imaginaryCharacter} is an
* empty string.
* @throws NullArgumentException if {@code imaginaryFormat} is {@code null}.
* @throws NullArgumentException if {@code realFormat} is {@code null}.
*/ */
public ComplexFormat(String imaginaryCharacter, NumberFormat realFormat, public ComplexFormat(String imaginaryCharacter,
NumberFormat realFormat,
NumberFormat imaginaryFormat) { NumberFormat imaginaryFormat) {
super(); if (imaginaryCharacter == null) {
setImaginaryCharacter(imaginaryCharacter); throw new NullArgumentException();
setImaginaryFormat(imaginaryFormat); }
setRealFormat(realFormat); if (imaginaryCharacter.length() == 0) {
throw new NoDataException();
}
if (imaginaryFormat == null) {
throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT);
}
if (realFormat == null) {
throw new NullArgumentException(LocalizedFormats.REAL_FORMAT);
}
this.imaginaryCharacter = imaginaryCharacter;
this.imaginaryFormat = imaginaryFormat;
this.realFormat = realFormat;
} }
/** /**
@ -237,6 +257,18 @@ public class ComplexFormat {
return new ComplexFormat(f); return new ComplexFormat(f);
} }
/**
* Returns the default complex format for the given locale.
* @param locale the specific locale used by the format.
* @param imaginaryCharacter Imaginary character.
* @return the complex format specific to the given locale.
*/
public static ComplexFormat getInstance(String imaginaryCharacter,
Locale locale) {
NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
return new ComplexFormat(imaginaryCharacter, f);
}
/** /**
* Access the realFormat. * Access the realFormat.
* @return the realFormat. * @return the realFormat.
@ -330,46 +362,4 @@ public class ComplexFormat {
return new Complex(re.doubleValue(), im.doubleValue() * sign); return new Complex(re.doubleValue(), im.doubleValue() * sign);
} }
/**
* Modify the imaginaryCharacter.
* @param imaginaryCharacter The new imaginaryCharacter value.
* @throws NullArgumentException if {@code imaginaryCharacter} is
* {@code null}.
* @throws NoDataException if {@code imaginaryCharacter} is an
* empty string.
*/
public void setImaginaryCharacter(String imaginaryCharacter) {
if (imaginaryCharacter == null) {
throw new NullArgumentException();
}
if (imaginaryCharacter.length() == 0) {
throw new NoDataException();
}
this.imaginaryCharacter = imaginaryCharacter;
}
/**
* Modify the imaginaryFormat.
* @param imaginaryFormat The new imaginaryFormat value.
* @throws NullArgumentException if {@code imaginaryFormat} is {@code null}.
*/
public void setImaginaryFormat(NumberFormat imaginaryFormat) {
if (imaginaryFormat == null) {
throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT);
}
this.imaginaryFormat = imaginaryFormat;
}
/**
* Modify the realFormat.
* @param realFormat The new realFormat value.
* @throws NullArgumentException if {@code realFormat} is {@code null}.
*/
public void setRealFormat(NumberFormat realFormat) {
if (realFormat == null) {
throw new NullArgumentException(LocalizedFormats.REAL_FORMAT);
}
this.realFormat = realFormat;
}
} }

View File

@ -40,8 +40,7 @@ public abstract class ComplexFormatAbstractTest {
protected ComplexFormatAbstractTest() { protected ComplexFormatAbstractTest() {
complexFormat = ComplexFormat.getInstance(getLocale()); complexFormat = ComplexFormat.getInstance(getLocale());
complexFormatJ = ComplexFormat.getInstance(getLocale()); complexFormatJ = ComplexFormat.getInstance("j", getLocale());
complexFormatJ.setImaginaryCharacter("j");
} }
@Test @Test
@ -308,67 +307,17 @@ public abstract class ComplexFormatAbstractTest {
@Test @Test
public void testGetImaginaryFormat() { public void testGetImaginaryFormat() {
NumberFormat nf = NumberFormat.getInstance(); NumberFormat nf = NumberFormat.getInstance();
ComplexFormat cf = new ComplexFormat(); ComplexFormat cf = new ComplexFormat(nf);
Assert.assertNotSame(nf, cf.getImaginaryFormat());
cf.setImaginaryFormat(nf);
Assert.assertSame(nf, cf.getImaginaryFormat()); Assert.assertSame(nf, cf.getImaginaryFormat());
} }
@Test
public void testSetImaginaryFormatNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryFormat(null);
Assert.fail();
} catch (NullArgumentException ex) {
// success
}
}
@Test
public void testSetRealFormatNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setRealFormat(null);
Assert.fail();
} catch (NullArgumentException ex) {
// success
}
}
@Test @Test
public void testGetRealFormat() { public void testGetRealFormat() {
NumberFormat nf = NumberFormat.getInstance(); NumberFormat nf = NumberFormat.getInstance();
ComplexFormat cf = new ComplexFormat(); ComplexFormat cf = new ComplexFormat(nf);
Assert.assertNotSame(nf, cf.getRealFormat());
cf.setRealFormat(nf);
Assert.assertSame(nf, cf.getRealFormat()); Assert.assertSame(nf, cf.getRealFormat());
} }
@Test
public void testSetImaginaryCharacterNull() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryCharacter(null);
Assert.fail();
} catch (NullArgumentException ex) {
// success
}
}
@Test
public void testSetImaginaryCharacterEmpty() {
try {
ComplexFormat cf = new ComplexFormat();
cf.setImaginaryCharacter("");
Assert.fail();
} catch (MathIllegalArgumentException ex) {
// success
}
}
@Test @Test
public void testFormatNumber() { public void testFormatNumber() {
ComplexFormat cf = ComplexFormat.getInstance(getLocale()); ComplexFormat cf = ComplexFormat.getInstance(getLocale());