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

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@822850 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-10-07 18:56:42 +00:00
parent 2ed606cfc0
commit f5398ad3cf
4 changed files with 94 additions and 36 deletions

View File

@ -53,7 +53,6 @@ public class MathException extends Exception {
* detail message.
*/
public MathException() {
super();
this.pattern = null;
this.arguments = new Object[0];
}
@ -66,7 +65,6 @@ public class MathException extends Exception {
* @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 @@ public class MathException extends Exception {
* @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 @@ public class MathException extends Exception {
}
/**
* 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 @@ public class MathException extends Exception {
* @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} */

View File

@ -57,9 +57,8 @@ public class MathRuntimeException extends RuntimeException {
* @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 class MathRuntimeException extends RuntimeException {
*/
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();
}
/**
@ -155,6 +154,12 @@ public class MathRuntimeException extends RuntimeException {
return buildMessage(locale, pattern, arguments);
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return getMessage(Locale.US);
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
@ -193,11 +198,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -216,11 +227,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -239,11 +256,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -279,11 +302,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -315,11 +344,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -338,11 +373,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -361,11 +402,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -384,11 +431,17 @@ public class MathRuntimeException extends RuntimeException {
*/
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() {
@ -410,11 +463,17 @@ public class MathRuntimeException extends RuntimeException {
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() {
@ -433,11 +492,17 @@ public class MathRuntimeException extends RuntimeException {
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() {

View File

@ -29,8 +29,8 @@ public class MathConfigurationExceptionTest extends TestCase {
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(){

View File

@ -32,8 +32,8 @@ public class MathExceptionTest extends TestCase {
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(){