added new constructors to MathUserException to provide more control to user

added the general message for MathUserException

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1035421 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2010-11-15 19:40:38 +00:00
parent e5c461784a
commit 2fb069185a
2 changed files with 97 additions and 12 deletions

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.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.exception;
* @version $Revision$ $Date$
*/
public class MathUserException extends RuntimeException {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/**
* Default constructor.
* Pattern used to build the specific part of the message (problem description).
*/
public MathUserException() {}
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;
/**
* @param msg Error message.
* Build an exception with a default message.
*/
public MathUserException(String msg) {
super(msg);
public MathUserException() {
this((Throwable) null);
}
/**
* @param msg Error message.
* @param cause Cause of the error.
* Build an exception with a default message.
* @param cause Cause of the error (may be null).
*/
public MathUserException(String msg,
Throwable cause) {
super(msg, cause);
public MathUserException(final Throwable cause) {
this(cause, LocalizedFormats.USER_EXCEPTION);
}
/**
* @param cause Cause of the error.
* Build an exception with a localizable message.
* @param pattern Format specifier.
* @param arguments Format arguments.
*/
public MathUserException(Throwable cause) {
public MathUserException(final Localizable pattern, final Object ... arguments) {
this((Throwable) null, pattern, arguments);
}
/**
* 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(final Throwable cause,
final Localizable pattern, final Object ... arguments) {
this(cause, (Localizable) null, pattern, arguments);
}
/**
* 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(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());
}
}

View File

@ -284,6 +284,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}"),