commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r822850 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/ test/java/org/apache/commons/math/
Date Wed, 07 Oct 2009 18:56:43 GMT
Author: luc
Date: Wed Oct  7 18:56:42 2009
New Revision: 822850

URL: http://svn.apache.org/viewvc?rev=822850&view=rev
Log:
delay message build until really needed
this allows not wasting time dealing with resources, strings and formatting when the exception
is discarded and its message not used
thanks to Gilles for the suggestion

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathException.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathRuntimeException.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathExceptionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathException.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathException.java?rev=822850&r1=822849&r2=822850&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathException.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathException.java Wed
Oct  7 18:56:42 2009
@@ -53,7 +53,6 @@
      * detail message.
      */
     public MathException() {
-        super();
         this.pattern   = null;
         this.arguments = new Object[0];
     }
@@ -66,7 +65,6 @@
      * @param arguments format arguments
      */
     public MathException(String pattern, Object ... arguments) {
-      super(buildMessage(Locale.US, pattern, arguments));
       this.pattern   = pattern;
       this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
     }
@@ -95,7 +93,7 @@
      * @since 1.2
      */
     public MathException(Throwable rootCause, String pattern, Object ... arguments) {
-      super(buildMessage(Locale.US, pattern, arguments), rootCause);
+      super(rootCause);
       this.pattern   = pattern;
       this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
     }
@@ -126,17 +124,6 @@
 
     }
 
-    /**
-     * Builds a message string by from a pattern and its arguments.
-     * @param locale Locale in which the message should be translated
-     * @param pattern format specifier
-     * @param arguments format arguments
-     * @return a message string
-     */
-    private static String buildMessage(Locale locale, String pattern, Object ... arguments)
{
-        return (pattern == null) ? "" : new MessageFormat(translate(pattern, locale), locale).format(arguments);
-    }
-
     /** Gets the pattern used to build the message of this throwable.
      *
      * @return the pattern used to build the message of this throwable
@@ -162,8 +149,14 @@
      * @return localized message
      * @since 1.2
      */
-    public String getMessage(Locale locale) {
-        return buildMessage(locale, pattern, arguments);
+    public String getMessage(final Locale locale) {
+        return (pattern == null) ? "" : new MessageFormat(translate(pattern, locale), locale).format(arguments);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getMessage() {
+        return getMessage(Locale.US);
     }
 
     /** {@inheritDoc} */

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathRuntimeException.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathRuntimeException.java?rev=822850&r1=822849&r2=822850&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathRuntimeException.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/MathRuntimeException.java
Wed Oct  7 18:56:42 2009
@@ -57,9 +57,8 @@
      * @param arguments format arguments
      */
     public MathRuntimeException(final String pattern, final Object ... arguments) {
-      super(buildMessage(Locale.US, pattern, arguments));
-      this.pattern   = pattern;
-      this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
+        this.pattern   = pattern;
+        this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
     }
 
     /**
@@ -86,9 +85,9 @@
      */
     public MathRuntimeException(final Throwable rootCause,
                                 final String pattern, final Object ... arguments) {
-      super(buildMessage(Locale.US, pattern, arguments), rootCause);
-      this.pattern   = pattern;
-      this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
+        super(rootCause);
+        this.pattern   = pattern;
+        this.arguments = (arguments == null) ? new Object[0] : arguments.clone();
     }
 
     /**
@@ -157,6 +156,12 @@
 
     /** {@inheritDoc} */
     @Override
+    public String getMessage() {
+        return getMessage(Locale.US);
+    }
+
+    /** {@inheritDoc} */
+    @Override
     public String getLocalizedMessage() {
         return getMessage(Locale.getDefault());
     }
@@ -193,13 +198,19 @@
      */
     public static ArithmeticException createArithmeticException(final String pattern,
                                                                 final Object ... arguments)
{
-        return new ArithmeticException(buildMessage(Locale.US, pattern, arguments)) {
+        return new ArithmeticException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = 7705628723242533939L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -216,13 +227,19 @@
      */
     public static ArrayIndexOutOfBoundsException createArrayIndexOutOfBoundsException(final
String pattern,
                                                                                       final
Object ... arguments) {
-        return new ArrayIndexOutOfBoundsException(buildMessage(Locale.US, pattern, arguments))
{
+        return new ArrayIndexOutOfBoundsException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -3394748305449283486L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -239,13 +256,19 @@
      */
     public static EOFException createEOFException(final String pattern,
                                                   final Object ... arguments) {
-        return new EOFException(buildMessage(Locale.US, pattern, arguments)) {
+        return new EOFException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = 279461544586092584L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -279,13 +302,19 @@
      */
     public static IllegalArgumentException createIllegalArgumentException(final String pattern,
                                                                           final Object ...
arguments) {
-        return new IllegalArgumentException(buildMessage(Locale.US, pattern, arguments))
{
+        return new IllegalArgumentException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -6555453980658317913L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -315,13 +344,19 @@
      */
     public static IllegalStateException createIllegalStateException(final String pattern,
                                                                     final Object ... arguments)
{
-        return new IllegalStateException(buildMessage(Locale.US, pattern, arguments)) {
+        return new IllegalStateException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -95247648156277208L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -338,13 +373,19 @@
      */
     public static ConcurrentModificationException createConcurrentModificationException(final
String pattern,
                                                                                         final
Object ... arguments) {
-        return new ConcurrentModificationException(buildMessage(Locale.US, pattern, arguments))
{
+        return new ConcurrentModificationException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = 6134247282754009421L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -361,13 +402,19 @@
      */
     public static NoSuchElementException createNoSuchElementException(final String pattern,
                                                                       final Object ... arguments)
{
-        return new NoSuchElementException(buildMessage(Locale.US, pattern, arguments)) {
+        return new NoSuchElementException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = 7304273322489425799L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -384,13 +431,19 @@
      */
     public static NullPointerException createNullPointerException(final String pattern,
                                                                   final Object ... arguments)
{
-        return new NullPointerException(buildMessage(Locale.US, pattern, arguments)) {
+        return new NullPointerException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -3075660477939965216L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -410,13 +463,19 @@
     public static ParseException createParseException(final int offset,
                                                       final String pattern,
                                                       final Object ... arguments) {
-        return new ParseException(buildMessage(Locale.US, pattern, arguments), offset) {
+        return new ParseException(null, offset) {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -1103502177342465975L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, arguments);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, arguments);
             }
@@ -433,13 +492,19 @@
         final String pattern  = "internal error, please fill a bug report at {0}";
         final String argument = "https://issues.apache.org/jira/browse/MATH";
 
-        return new RuntimeException(buildMessage(Locale.US, pattern, argument)) {
+        return new RuntimeException() {
 
             /** Serializable version identifier. */
             private static final long serialVersionUID = -201865440834027016L;
 
             /** {@inheritDoc} */
             @Override
+            public String getMessage() {
+                return buildMessage(Locale.US, pattern, argument);
+            }
+
+            /** {@inheritDoc} */
+            @Override
             public String getLocalizedMessage() {
                 return buildMessage(Locale.getDefault(), pattern, argument);
             }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java?rev=822850&r1=822849&r2=822850&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java
Wed Oct  7 18:56:42 2009
@@ -29,8 +29,8 @@
     public void testConstructor(){
         MathConfigurationException ex = new MathConfigurationException();
         assertNull(ex.getCause());
-        assertNull(ex.getMessage());
-        assertEquals(0, ex.getMessage(Locale.FRENCH).length());
+        assertEquals("", ex.getMessage());
+        assertEquals("", ex.getMessage(Locale.FRENCH));
     }
 
     public void testConstructorPatternArguments(){

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathExceptionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathExceptionTest.java?rev=822850&r1=822849&r2=822850&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathExceptionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/MathExceptionTest.java
Wed Oct  7 18:56:42 2009
@@ -32,8 +32,8 @@
     public void testConstructor(){
         MathException ex = new MathException();
         assertNull(ex.getCause());
-        assertNull(ex.getMessage());
-        assertEquals(0, ex.getMessage(Locale.FRENCH).length());
+        assertEquals("", ex.getMessage());
+        assertEquals("", ex.getMessage(Locale.FRENCH));
     }
 
     public void testConstructorPatternArguments(){



Mime
View raw message