FYI: few DecimalFormat fixes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Il giorno dom, 26/11/2006 alle 21.06 +0100, Mark Wielaard ha scritto:

> - java.sql.Timestamp.TimestampTest
> - java.text.MessageFormat.format
> - java.text.MessageFormat.parse
> - java.text.NumberFormat.UK
> - locales.LocaleTest
> 
> These all seem to be caused by the last Decimal/NumberFormat patch. We
> get a couple of new passes back for it, but it would be good to
> investigate these failures.
> 
> - javax.swing.JSpinner.DefaultEditor.propertyChange
> - javax.swing.JSpinner.DefaultEditor.stateChanged
> 
> Similar. Both have checks fail like:
>   FAIL:  line 49: (PropertyChangeEvent) [3] -- got 88.0 but expected 88
>   FAIL:  line 46: (ChangeEvent) [3] -- got 99.0 but expected 99

I've fixed some of them. There are a couple of them difficult to find,
though.

In one case the specs defines two mutually exclusives scenarios :)

They say (Format) that "pos" should not be null, or an NPE would be
generated, but then, in MessageFormat, let pass this example in one of
the methods:

format(argument, new StringBuffer(), null);
				^^^^^^
:)

I've fixed this, though we now check in DecimalFormat for null, which is
not really correct.

I'll search for the other failures tomorrow.

Mario

2006-11-27  neugens  <neugens@xxxxxxxxxxxxxxxxxxxxxxxx>

	* java/text/DecimalFormat.java (formatInternal): Add an explicit test
	for FieldPosition to be null.
	Check if the factional part is just 0 and can be omitted from the
	result.
	(scanNegativePattern): Fixed index out of bound exception when
searching
	for international currency symbol in negative pattern.

-- 
Lima Software, SO.PR.IND. s.r.l.
http://www.limasoftware.net/
pgp key: http://subkeys.pgp.net/

Please, support open standards:
http://opendocumentfellowship.org/petition/
http://www.nosoftwarepatents.com/
### Eclipse Workspace Patch 1.0
#P classpath
Index: java/text/DecimalFormat.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/DecimalFormat.java,v
retrieving revision 1.27
diff -u -r1.27 DecimalFormat.java
--- java/text/DecimalFormat.java	25 Nov 2006 18:20:54 -0000	1.27
+++ java/text/DecimalFormat.java	27 Nov 2006 02:42:20 -0000
@@ -1650,7 +1650,7 @@
             currencySymbol = CURRENCY_SYMBOL;
 
             // if \u00A4 is doubled, we use the international currency symbol
-            if (i < len && pattern.charAt(i + 1) == '\u00A4')
+            if ((i + 1) < len && pattern.charAt(i + 1) == '\u00A4')
               {
                 currencySymbol = symbols.getInternationalCurrencySymbol();
                 i = i + 2;
@@ -1728,6 +1728,15 @@
   private void formatInternal(BigDecimal number, boolean isLong,
                               StringBuffer dest, FieldPosition fieldPos)
   {
+    // The specs says that fieldPos should not be null, and that we
+    // should throw a NPE, but it seems that in few classes that
+    // reference this one, fieldPos is set to null.
+    // This is even defined in the javadoc, see for example MessageFormat.
+    // I think the best here is to check for fieldPos and build one if it is
+    // null. If it cause harms or regressions, just remove this line and
+    // fix the classes in the point of call, insted.
+    if (fieldPos == null) fieldPos = new FieldPosition(0); 
+    
     int _multiplier = this.multiplier;
     
     // used to track attribute starting position for each attribute
@@ -1853,7 +1862,7 @@
         // the sum of the minimum integer and maximum fraction
         // digits, and does not take into account the maximun integer
         // digits to display.
-        // This methods takes care of the integer portion of the mantissa.
+        
         if (attributeStart < 0)
           attributeStart = Math.max(dest.length() - 1, 0);
         appendDigit(intPart, dest, this.groupingUsed);
@@ -1916,10 +1925,33 @@
           }
         
         fractPart = adjustTrailingZeros(fractPart, digits);
-        appendDigit(fractPart, dest, false);
         
-        endIndexFract = dest.length();
-        addAttribute(Field.FRACTION, attributeStart, endIndexFract);
+        // FIXME: this code must be improved
+        // now check if the factional part is just 0, in this case
+        // we need to remove the '.' unless requested
+        boolean allZeros = true;
+        char fracts[] = fractPart.toCharArray();
+        for (int i = 0; i < fracts.length; i++)
+          {
+            if (fracts[i] != '0')
+              allZeros = false;
+          }
+        
+        if (!allZeros || minimumFractionDigits > 0)
+          {
+            appendDigit(fractPart, dest, false);
+            endIndexFract = dest.length();
+            addAttribute(Field.FRACTION, attributeStart, endIndexFract);
+          }
+        else if (!this.decimalSeparatorAlwaysShown)
+          {
+            dest.deleteCharAt(dest.length() - 1);
+          }
+        else
+          {
+            endIndexFract = dest.length();
+            addAttribute(Field.FRACTION, attributeStart, endIndexFract);
+          }
       }
     
     // and the exponent

[Index of Archives]     [Linux Kernel]     [Linux Cryptography]     [Fedora]     [Fedora Directory]     [Red Hat Development]

  Powered by Linux