myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r1067484 - in /myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src: main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/ test/java/org/apache/myfaces/extensions/cdi/message/test/
Date Sat, 05 Feb 2011 19:03:07 GMT
Author: gpetracek
Date: Sat Feb  5 19:03:06 2011
New Revision: 1067484

URL: http://svn.apache.org/viewvc?rev=1067484&view=rev
Log:
EXTCDI-129 max digits and exponent separator

Added:
    myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestCustomNumberConfig.java
      - copied, changed from r1059626, myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestGermanNumberConfig.java
Removed:
    myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestGermanNumberConfig.java
Modified:
    myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/DefaultNumberFormatter.java
    myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/NumberFormatterConfigKeys.java
    myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/FormattedMessageArgumentTest.java

Modified: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/DefaultNumberFormatter.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/DefaultNumberFormatter.java?rev=1067484&r1=1067483&r2=1067484&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/DefaultNumberFormatter.java
(original)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/DefaultNumberFormatter.java
Sat Feb  5 19:03:06 2011
@@ -36,10 +36,13 @@ class DefaultNumberFormatter implements 
 {
     private static final long serialVersionUID = 1639250543559140704L;
 
-    private Character groupingSeparator;
-    private Character decimalSeparator;
-    private Integer minimumFractionDigits;
-    private Integer minimumIntegerDigits;
+    private Character groupingSeparator = null;
+    private Character decimalSeparator = null;
+    private Integer minimumFractionDigits = null;
+    private Integer minimumIntegerDigits = null;
+    private Integer maximumFractionDigits = null;
+    private Integer maximumIntegerDigits = null;
+    private String exponentSeparator = null;
 
     DefaultNumberFormatter()
     {
@@ -57,19 +60,15 @@ class DefaultNumberFormatter implements 
 
     public String format(MessageContext messageContext, Number valueToFormat)
     {
+        Locale locale = messageContext.getLocale();
+
         GenericConfig formatterConfig = messageContext.config().getFormatterFactory()
-                .findFormatterConfig(Number.class, messageContext.getLocale());
+                .findFormatterConfig(Number.class, locale);
 
         if (formatterConfig != null)
         {
             useCustomConfig(formatterConfig);
         }
-        else
-        {
-            resetConfig();
-        }
-
-        Locale locale = messageContext.config().getLocaleResolver().getLocale();
 
         if (valueToFormat instanceof BigDecimal || valueToFormat instanceof Double || valueToFormat
instanceof Float)
         {
@@ -81,14 +80,6 @@ class DefaultNumberFormatter implements 
         }
     }
 
-    private void resetConfig()
-    {
-        this.groupingSeparator = null;
-        this.decimalSeparator = null;
-        this.minimumFractionDigits = null;
-        this.minimumIntegerDigits = null;
-    }
-
     private void useCustomConfig(GenericConfig formatterConfig)
     {
         if (formatterConfig.containsProperty(NumberFormatterConfigKeys.GROUPING_SEPARATOR_KEY))
@@ -114,6 +105,24 @@ class DefaultNumberFormatter implements 
             this.minimumIntegerDigits = formatterConfig
                     .getProperty(NumberFormatterConfigKeys.MINIMUM_INTEGER_DIGITS_KEY, Integer.class);
         }
+
+        if (formatterConfig.containsProperty(NumberFormatterConfigKeys.MAXIMUM_FRACTION_DIGITS))
+        {
+            this.maximumFractionDigits = formatterConfig
+                    .getProperty(NumberFormatterConfigKeys.MAXIMUM_FRACTION_DIGITS, Integer.class);
+        }
+
+        if (formatterConfig.containsProperty(NumberFormatterConfigKeys.MAXIMUM_INTEGER_DIGITS))
+        {
+            this.maximumIntegerDigits = formatterConfig
+                    .getProperty(NumberFormatterConfigKeys.MAXIMUM_INTEGER_DIGITS, Integer.class);
+        }
+
+        if (formatterConfig.containsProperty(NumberFormatterConfigKeys.EXPONENT_SEPARATOR))
+        {
+            this.exponentSeparator = formatterConfig
+                    .getProperty(NumberFormatterConfigKeys.EXPONENT_SEPARATOR, String.class);
+        }
     }
 
     private NumberFormat getNumberFormat(Locale locale)
@@ -128,6 +137,10 @@ class DefaultNumberFormatter implements 
         {
             symbols.setDecimalSeparator(this.decimalSeparator);
         }
+        if (this.exponentSeparator != null)
+        {
+            symbols.setExponentSeparator(this.exponentSeparator);
+        }
         return new DecimalFormat("", symbols);
     }
 
@@ -143,6 +156,10 @@ class DefaultNumberFormatter implements 
         {
             symbols.setDecimalSeparator(this.decimalSeparator);
         }
+        if (this.exponentSeparator != null)
+        {
+            symbols.setExponentSeparator(this.exponentSeparator);
+        }
 
         DecimalFormat format = new DecimalFormat("", symbols);
 
@@ -154,6 +171,16 @@ class DefaultNumberFormatter implements 
         {
             format.setMinimumIntegerDigits(this.minimumIntegerDigits);//for 0.**
         }
+
+        if (this.maximumFractionDigits != null)
+        {
+            format.setMaximumFractionDigits(this.maximumFractionDigits);
+        }
+
+        if (this.maximumIntegerDigits != null)
+        {
+            format.setMaximumIntegerDigits(this.maximumIntegerDigits);
+        }
         return format;
     }
 
@@ -171,23 +198,38 @@ class DefaultNumberFormatter implements 
 
         DefaultNumberFormatter that = (DefaultNumberFormatter) o;
 
-        if (decimalSeparator != null ? !decimalSeparator.equals(that.decimalSeparator) :
that.decimalSeparator
-                != null)
+        if (decimalSeparator != null ? !decimalSeparator.equals(that.decimalSeparator) :
+                that.decimalSeparator != null)
+        {
+            return false;
+        }
+        if (exponentSeparator != null ? !exponentSeparator.equals(that.exponentSeparator)
:
+                that.exponentSeparator != null)
+        {
+            return false;
+        }
+        if (groupingSeparator != null ? !groupingSeparator.equals(that.groupingSeparator)
:
+                that.groupingSeparator != null)
+        {
+            return false;
+        }
+        if (maximumFractionDigits != null ? !maximumFractionDigits.equals(that.maximumFractionDigits)
:
+                that.maximumFractionDigits != null)
         {
             return false;
         }
-        if (groupingSeparator != null ? !groupingSeparator.equals(that.groupingSeparator)
: that.groupingSeparator
-                != null)
+        if (maximumIntegerDigits != null ? !maximumIntegerDigits.equals(that.maximumIntegerDigits)
:
+                that.maximumIntegerDigits != null)
         {
             return false;
         }
-        if (minimumFractionDigits != null ?
-                !minimumFractionDigits.equals(that.minimumFractionDigits) : that.minimumFractionDigits
!= null)
+        if (minimumFractionDigits != null ? !minimumFractionDigits.equals(that.minimumFractionDigits)
:
+                that.minimumFractionDigits != null)
         {
             return false;
         }
-        if (minimumIntegerDigits != null ?
-                !minimumIntegerDigits.equals(that.minimumIntegerDigits) : that.minimumIntegerDigits
!= null)
+        if (minimumIntegerDigits != null ? !minimumIntegerDigits.equals(that.minimumIntegerDigits)
:
+                that.minimumIntegerDigits != null)
         {
             return false;
         }

Modified: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/NumberFormatterConfigKeys.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/NumberFormatterConfigKeys.java?rev=1067484&r1=1067483&r2=1067484&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/NumberFormatterConfigKeys.java
(original)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/message/impl/formatter/NumberFormatterConfigKeys.java
Sat Feb  5 19:03:06 2011
@@ -30,4 +30,10 @@ public interface NumberFormatterConfigKe
     static final String MINIMUM_FRACTION_DIGITS_KEY = "minimum_fraction_digits";
 
     static final String MINIMUM_INTEGER_DIGITS_KEY = "minimum_integer_digits";
+
+    static final String MAXIMUM_FRACTION_DIGITS = "maximum_fraction_digits";
+    
+    static final String MAXIMUM_INTEGER_DIGITS = "maximum_integer_digits";
+    
+    static final String EXPONENT_SEPARATOR = "exponent_separator";
 }
\ No newline at end of file

Modified: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/FormattedMessageArgumentTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/FormattedMessageArgumentTest.java?rev=1067484&r1=1067483&r2=1067484&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/FormattedMessageArgumentTest.java
(original)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/FormattedMessageArgumentTest.java
Sat Feb  5 19:03:06 2011
@@ -35,7 +35,7 @@ public class FormattedMessageArgumentTes
     {
         String messageText = this.messageContext.config().use().messageInterpolator(new NumberedArgumentAwareMessageInterpolator())
                 .localeResolver(new TestGermanLocaleResolver())
-                .addFormatterConfig(Number.class, new TestGermanNumberConfig()).create()
+                .create()
                 .message().text("{formatted_number}").argument(new BigDecimal("7654.3210")).toText();
 
         assertEquals("value: 7.654,321", messageText);
@@ -46,11 +46,21 @@ public class FormattedMessageArgumentTes
     {
         String messageText = this.messageContext.config().use().messageInterpolator(new NumberedArgumentAwareMessageInterpolator())
                 .localeResolver(new TestEnglishLocaleResolver())
-                .addFormatterConfig(Number.class, new TestGermanNumberConfig(), Locale.GERMAN)
-                .addFormatterConfig(Number.class, new TestEnglishNumberConfig(), Locale.ENGLISH)
                 .create()
                 .message().text("{formatted_number}").argument(new BigDecimal("7654.3210")).toText();
 
         assertEquals("value: 7,654.321", messageText);
     }
+
+    @Test
+    public void createCustomFormattedMessageEnglishTest()
+    {
+        String messageText = this.messageContext.config().use().messageInterpolator(new NumberedArgumentAwareMessageInterpolator())
+                .localeResolver(new TestEnglishLocaleResolver())
+                .addFormatterConfig(Number.class, new TestCustomNumberConfig(), Locale.ENGLISH)
+                .create()
+                .message().text("{formatted_number}").argument(new BigDecimal("7654.3210")).toText();
+
+        assertEquals("value: 7'654,321", messageText);
+    }
 }
\ No newline at end of file

Copied: myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestCustomNumberConfig.java
(from r1059626, myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestGermanNumberConfig.java)
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestCustomNumberConfig.java?p2=myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestCustomNumberConfig.java&p1=myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestGermanNumberConfig.java&r1=1059626&r2=1067484&rev=1067484&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestGermanNumberConfig.java
(original)
+++ myfaces/extensions/cdi/trunk/jse-modules/message-module/impl/src/test/java/org/apache/myfaces/extensions/cdi/message/test/TestCustomNumberConfig.java
Sat Feb  5 19:03:06 2011
@@ -28,16 +28,15 @@ import java.util.Map;
 /**
  * @author Gerhard Petracek
  */
-class TestGermanNumberConfig implements GenericConfig
+class TestCustomNumberConfig implements GenericConfig
 {
     private static final long serialVersionUID = 1581606533032801390L;
     private Map<String, Serializable> properties = new HashMap<String, Serializable>();
 
+    TestCustomNumberConfig()
     {
-        this.properties.put(NumberFormatterConfigKeys.GROUPING_SEPARATOR_KEY, ".");
+        this.properties.put(NumberFormatterConfigKeys.GROUPING_SEPARATOR_KEY, "'");
         this.properties.put(NumberFormatterConfigKeys.DECIMAL_SEPARATOR_KEY, ",");
-        this.properties.put(NumberFormatterConfigKeys.MINIMUM_FRACTION_DIGITS_KEY, 2);
-        this.properties.put(NumberFormatterConfigKeys.MINIMUM_INTEGER_DIGITS_KEY, 1);
     }
 
     public GenericConfig addProperty(String key, Serializable value)



Mime
View raw message