Author: luc Date: Mon Nov 15 19:40:38 2010 New Revision: 1035421 URL: http://svn.apache.org/viewvc?rev=1035421&view=rev Log: added new constructors to MathUserException to provide more control to user added the general message for MathUserException
Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/MathUserException.java commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/MathUserException.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/MathUserException.java?rev=1035421&r1=1035420&r2=1035421&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/MathUserException.java (original) +++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/MathUserException.java Mon Nov 15 19:40:38 2010 @@ -16,6 +16,13 @@ */ package org.apache.commons.math.exception; +import java.util.Locale; + +import org.apache.commons.math.exception.util.ArgUtils; +import org.apache.commons.math.exception.util.Localizable; +import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.exception.util.MessageFactory; + /** * This class is intended as a sort of communication channel between * layers of <em>user</em> code separated from each other by calls to @@ -26,31 +33,108 @@ package org.apache.commons.math.exceptio * @version $Revision$ $Date$ */ public class MathUserException extends RuntimeException { + /** Serializable version Id. */ + private static final long serialVersionUID = -6024911025449780478L; + /** + * Pattern used to build the specific part of the message (problem description). + */ + private final Localizable specific; + /** + * Pattern used to build the general part of the message (problem description). + */ + private final Localizable general; + /** + * Arguments used to build the message. + */ + private final Object[] arguments; + + /** + * Build an exception with a default message. + */ + public MathUserException() { + this((Throwable) null); + } + /** - * Default constructor. + * Build an exception with a default message. + * @param cause Cause of the error (may be null). */ - public MathUserException() {} + public MathUserException(final Throwable cause) { + this(cause, LocalizedFormats.USER_EXCEPTION); + } /** - * @param msg Error message. + * Build an exception with a localizable message. + * @param pattern Format specifier. + * @param arguments Format arguments. */ - public MathUserException(String msg) { - super(msg); + public MathUserException(final Localizable pattern, final Object ... arguments) { + this((Throwable) null, pattern, arguments); } /** - * @param msg Error message. - * @param cause Cause of the error. + * Build an exception with a localizable message. + * @param cause Cause of the error (may be null). + * @param pattern Format specifier. + * @param arguments Format arguments. */ - public MathUserException(String msg, - Throwable cause) { - super(msg, cause); + public MathUserException(final Throwable cause, + final Localizable pattern, final Object ... arguments) { + this(cause, (Localizable) null, pattern, arguments); } /** - * @param cause Cause of the error. + * Builds an exception from two patterns (specific and general) and + * an argument list. + * + * @param specific Format specifier for the specific part (may be null). + * @param general Format specifier for the general part (may be null). + * @param arguments Format arguments. They will be substituted in + * <em>both</em> the {...@code general} and {...@code specific} format specifiers. */ - public MathUserException(Throwable cause) { + public MathUserException(final Localizable specific, final Localizable general, + final Object ... arguments) { + this((Throwable) null, specific, general, arguments); + } + + /** + * Builds an exception from two patterns (specific and general) and + * an argument list. + * + * @param cause Cause of the error (may be null). + * @param specific Format specifier for the specific part (may be null). + * @param general Format specifier for the general part (may be null). + * @param arguments Format arguments. They will be substituted in + * <em>both</em> the {...@code general} and {...@code specific} format specifiers. + */ + public MathUserException(final Throwable cause, + final Localizable specific, final Localizable general, + final Object ... arguments) { super(cause); + this.specific = specific; + this.general = general; + this.arguments = ArgUtils.flatten(arguments); + } + + /** + * Get the message in a specified locale. + * + * @param locale Locale in which the message should be translated. + * @return the localized message. + */ + public String getMessage(final Locale locale) { + return MessageFactory.buildMessage(locale, specific, general, arguments); + } + + /** {...@inheritdoc} */ + @Override + public String getMessage() { + return getMessage(Locale.US); + } + + /** {...@inheritdoc} */ + @Override + public String getLocalizedMessage() { + return getMessage(Locale.getDefault()); } } Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java?rev=1035421&r1=1035420&r2=1035421&view=diff ============================================================================== --- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java (original) +++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java Mon Nov 15 19:40:38 2010 @@ -284,6 +284,7 @@ public enum LocalizedFormats implements UNPARSEABLE_REAL_VECTOR("unparseable real vector: \"{0}\""), UNSUPPORTED_EXPANSION_MODE("unsupported expansion mode {0}, supported modes are {1} ({2}) and {3} ({4})"), UNSUPPORTED_OPERATION("unsupported operation"), /* keep */ + USER_EXCEPTION("exception generated in user code"), /* keep */ URL_CONTAINS_NO_DATA("URL {0} contains no data"), VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC("{0} values have been added before statistic is configured"), VECTOR_LENGTH_MISMATCH("vector length mismatch: got {0} but expected {1}"),