"MathUserException": Using "Localizable" argument in constructor.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1034601 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-11-12 23:41:02 +00:00
parent 7c9c3017bb
commit d60ad04b6d
4 changed files with 63 additions and 19 deletions

View File

@ -76,7 +76,6 @@ public class MathIllegalArgumentException extends IllegalArgumentException {
* 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) {

View File

@ -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.MessageFactory;
import org.apache.commons.math.exception.util.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**
* 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,67 @@ package org.apache.commons.math.exception;
* @version $Revision$ $Date$
*/
public class MathUserException extends RuntimeException {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/**
* Pattern used to build the message (problem description).
*/
private final Localizable pattern;
/**
* Arguments used to build the message.
*/
private final Object[] arguments;
/**
* Default constructor.
*/
public MathUserException() {}
/**
* @param msg Error message.
*/
public MathUserException(String msg) {
super(msg);
}
/**
* @param msg Error message.
* @param cause Cause of the error.
*/
public MathUserException(String msg,
Throwable cause) {
super(msg, cause);
public MathUserException() {
this(null);
}
/**
* @param cause Cause of the error.
* @param args Arguments.
*/
public MathUserException(Throwable cause) {
super(cause);
public MathUserException(Throwable cause,
Object ... args) {
this(null, cause, args);
}
/**
* @param pattern Message pattern explaining the cause of the error.
* @param cause Cause of the error.
* @param args Arguments.
*/
public MathUserException(Localizable pattern,
Throwable cause,
Object ... args) {
this.pattern = pattern;
arguments = ArgUtils.flatten(args);
}
/**
* 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,
pattern,
LocalizedFormats.USER_EXCEPTION,
arguments);
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return getMessage(Locale.US);
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return getMessage(Locale.getDefault());
}
}

View File

@ -303,6 +303,7 @@ public enum LocalizedFormats implements Localizable {
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}"),

View File

@ -275,6 +275,7 @@ UNPARSEABLE_FRACTION_NUMBER = nombre fractionnaire non analysable : "{0}"
UNPARSEABLE_REAL_VECTOR = vecteur r\u00e9el non analysable : "{0}"
UNSUPPORTED_EXPANSION_MODE = mode d''extension {0} non support\u00e9, les modes support\u00e9s sont {1} ({2}) et {3} ({4})
UNSUPPORTED_OPERATION = op\u00e9ration non disponible
USER_EXCEPTION = erreur g\u00e9n\u00e9r\u00e9e par le code utilisateur
URL_CONTAINS_NO_DATA = l''adresse {0} ne contient aucune donn\u00e9e
VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC = {0} valeurs ont \u00e9t\u00e9 ajout\u00e9es
VECTOR_LENGTH_MISMATCH = taille de vecteur invalide : {0} au lieu de {1} attendue