brentworden    2004/09/20 21:45:55

  Modified:    math/src/java/org/apache/commons/math/complex
                        ComplexFormat.java
  Added:       math/src/test/org/apache/commons/math/complex
                        AbstractComplexFormatTest.java
                        FrenchComplexFormatTest.java
  Log:
  Added locale support to complex format.  Added test cases for specific locales.
  PR: 31325
  
  Revision  Changes    Path
  1.1                  
jakarta-commons/math/src/test/org/apache/commons/math/complex/AbstractComplexFormatTest.java
  
  Index: AbstractComplexFormatTest.java
  ===================================================================
  /*
   * 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
          }
      }
  }
  
  
  
  1.1                  
jakarta-commons/math/src/test/org/apache/commons/math/complex/FrenchComplexFormatTest.java
  
  Index: FrenchComplexFormatTest.java
  ===================================================================
  /*
   * 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;
      }
  }
  
  
  
  1.10      +53 -18    
jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexFormat.java
  
  Index: ComplexFormat.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexFormat.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ComplexFormat.java        23 Jun 2004 16:26:16 -0000      1.9
  +++ ComplexFormat.java        21 Sep 2004 04:45:55 -0000      1.10
  @@ -22,6 +22,7 @@
   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
  @@ -36,9 +37,6 @@
       /** 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 @@
        * @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
  -     * [EMAIL PROTECTED] 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 );
       }
       
       /**
  @@ -233,6 +218,38 @@
       }
       
       /**
  +     * Get the set of locales for which complex formats are available.  This
  +     * is the same set as the [EMAIL PROTECTED] 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
  +     * [EMAIL PROTECTED] 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
  +     * [EMAIL PROTECTED] 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.
        */
  @@ -246,6 +263,24 @@
        */
       public NumberFormat getImaginaryFormat() {
           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);
       }
       
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to