Changed all exception classes to use the new "ExceptionContext".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1101029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-05-09 13:54:34 +00:00
parent ec6dbad43a
commit c7f092e022
11 changed files with 128 additions and 31 deletions

View File

@ -34,7 +34,7 @@ public class ConvergenceException extends MathIllegalStateException {
* Construct the exception. * Construct the exception.
*/ */
public ConvergenceException() { public ConvergenceException() {
addMessage(LocalizedFormats.CONVERGENCE_FAILED); this(LocalizedFormats.CONVERGENCE_FAILED);
} }
/** /**
@ -46,6 +46,6 @@ public class ConvergenceException extends MathIllegalStateException {
*/ */
public ConvergenceException(Localizable pattern, public ConvergenceException(Localizable pattern,
Object ... args) { Object ... args) {
addMessage(pattern, args); getContext().addMessage(pattern, args);
} }
} }

View File

@ -18,25 +18,30 @@ package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/** /**
* Base class for arithmetic exceptions. * Base class for arithmetic exceptions.
* It is used for all the exceptions that share the semantics of the standard * It is used for all the exceptions that have the semantics of the standard
* {@link ArithmeticException}, but must also provide a localized * {@link ArithmeticException}, but must also provide a localized
* message. * message.
* *
* @since 3.0 * @since 3.0
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class MathArithmeticException extends MathRuntimeException { public class MathArithmeticException extends ArithmeticException
implements ExceptionContextProvider {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L; private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
/** /**
* Default constructor. * Default constructor.
*/ */
public MathArithmeticException() { public MathArithmeticException() {
addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION); context.addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION);
} }
/** /**
@ -48,6 +53,23 @@ public class MathArithmeticException extends MathRuntimeException {
*/ */
public MathArithmeticException(Localizable pattern, public MathArithmeticException(Localizable pattern,
Object ... args) { Object ... args) {
addMessage(pattern, args); context.addMessage(pattern, args);
}
/** {@inheritDoc} */
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
} }
} }

View File

@ -50,7 +50,7 @@ public class MathIllegalArgumentException extends IllegalArgumentException
return context; return context;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public String getMessage() { public String getMessage() {
return context.getMessage(); return context.getMessage();

View File

@ -18,6 +18,8 @@ package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/** /**
* Base class for all exceptions that signal a mismatch between the * Base class for all exceptions that signal a mismatch between the
@ -26,9 +28,12 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @since 2.2 * @since 2.2
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class MathIllegalStateException extends MathRuntimeException { public class MathIllegalStateException extends IllegalStateException
implements ExceptionContextProvider {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L; private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
/** /**
* Simple constructor. * Simple constructor.
@ -38,7 +43,7 @@ public class MathIllegalStateException extends MathRuntimeException {
*/ */
public MathIllegalStateException(Localizable pattern, public MathIllegalStateException(Localizable pattern,
Object ... args) { Object ... args) {
addMessage(pattern, args); context.addMessage(pattern, args);
} }
/** /**
@ -52,13 +57,30 @@ public class MathIllegalStateException extends MathRuntimeException {
Localizable pattern, Localizable pattern,
Object ... args) { Object ... args) {
super(cause); super(cause);
addMessage(pattern, args); context.addMessage(pattern, args);
} }
/** /**
* Default constructor. * Default constructor.
*/ */
public MathIllegalStateException() { public MathIllegalStateException() {
addMessage(LocalizedFormats.ILLEGAL_STATE); this(LocalizedFormats.ILLEGAL_STATE);
}
/** {@inheritDoc} */
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
} }
} }

View File

@ -34,7 +34,7 @@ public class MathInternalError extends MathIllegalStateException {
* Simple constructor. * Simple constructor.
*/ */
public MathInternalError() { public MathInternalError() {
addMessage(LocalizedFormats.INTERNAL_ERROR, REPORT_URL); getContext().addMessage(LocalizedFormats.INTERNAL_ERROR, REPORT_URL);
} }
/** /**
@ -44,5 +44,4 @@ public class MathInternalError extends MathIllegalStateException {
public MathInternalError(final Throwable cause) { public MathInternalError(final Throwable cause) {
super(cause, LocalizedFormats.INTERNAL_ERROR, REPORT_URL); super(cause, LocalizedFormats.INTERNAL_ERROR, REPORT_URL);
} }
} }

View File

@ -17,6 +17,8 @@
package org.apache.commons.math.exception; package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/** /**
* Class to signal parse failures. * Class to signal parse failures.
@ -24,7 +26,8 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @since 2.2 * @since 2.2
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class MathParseException extends MathRuntimeException { public class MathParseException extends MathIllegalStateException
implements ExceptionContextProvider {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L; private static final long serialVersionUID = -6024911025449780478L;
@ -38,8 +41,8 @@ public class MathParseException extends MathRuntimeException {
public MathParseException(String wrong, public MathParseException(String wrong,
int position, int position,
Class<?> type) { Class<?> type) {
addMessage(LocalizedFormats.CANNOT_PARSE_AS_TYPE, getContext().addMessage(LocalizedFormats.CANNOT_PARSE_AS_TYPE,
wrong, Integer.valueOf(position), type.getName()); wrong, Integer.valueOf(position), type.getName());
} }
/** /**
@ -49,7 +52,7 @@ public class MathParseException extends MathRuntimeException {
*/ */
public MathParseException(String wrong, public MathParseException(String wrong,
int position) { int position) {
addMessage(LocalizedFormats.CANNOT_PARSE, getContext().addMessage(LocalizedFormats.CANNOT_PARSE,
wrong, Integer.valueOf(position)); wrong, Integer.valueOf(position));
} }
} }

View File

@ -18,19 +18,24 @@ package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/** /**
* Base class for all unsupported features. * Base class for all unsupported features.
* It is used for all the exceptions that share the semantics of the standard * It is used for all the exceptions that have the semantics of the standard
* {@link UnsupportedOperationException}, but must also provide a localized * {@link UnsupportedOperationException}, but must also provide a localized
* message. * message.
* *
* @since 2.2 * @since 2.2
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class MathUnsupportedOperationException extends MathRuntimeException { public class MathUnsupportedOperationException extends UnsupportedOperationException
implements ExceptionContextProvider {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L; private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
/** /**
* Default constructor. * Default constructor.
@ -45,6 +50,23 @@ public class MathUnsupportedOperationException extends MathRuntimeException {
*/ */
public MathUnsupportedOperationException(Localizable pattern, public MathUnsupportedOperationException(Localizable pattern,
Object ... args) { Object ... args) {
addMessage(pattern, args); context.addMessage(pattern, args);
}
/** {@inheritDoc} */
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
} }
} }

View File

@ -18,6 +18,8 @@ package org.apache.commons.math.exception;
import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/** /**
* This class is intended as a sort of communication channel between * This class is intended as a sort of communication channel between
@ -28,15 +30,18 @@ import org.apache.commons.math.exception.util.LocalizedFormats;
* @since 2.2 * @since 2.2
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class MathUserException extends MathRuntimeException { public class MathUserException extends RuntimeException
implements ExceptionContextProvider {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L; private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
/** /**
* Build an exception with a default message. * Build an exception with a default message.
*/ */
public MathUserException() { public MathUserException() {
addMessage(LocalizedFormats.USER_EXCEPTION); context.addMessage(LocalizedFormats.USER_EXCEPTION);
} }
/** /**
@ -45,7 +50,7 @@ public class MathUserException extends MathRuntimeException {
*/ */
public MathUserException(final Throwable cause) { public MathUserException(final Throwable cause) {
super(cause); super(cause);
addMessage(LocalizedFormats.USER_EXCEPTION); context.addMessage(LocalizedFormats.USER_EXCEPTION);
} }
/** /**
@ -56,7 +61,7 @@ public class MathUserException extends MathRuntimeException {
*/ */
public MathUserException(final Localizable pattern, public MathUserException(final Localizable pattern,
final Object ... arguments) { final Object ... arguments) {
addMessage(pattern, arguments); context.addMessage(pattern, arguments);
} }
/** /**
@ -70,6 +75,23 @@ public class MathUserException extends MathRuntimeException {
final Localizable pattern, final Localizable pattern,
final Object ... arguments) { final Object ... arguments) {
super(cause); super(cause);
addMessage(pattern, arguments); context.addMessage(pattern, arguments);
}
/** {@inheritDoc} */
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
} }
} }

View File

@ -39,8 +39,7 @@ public class MaxCountExceededException extends MathIllegalStateException {
* @param max Maximum. * @param max Maximum.
*/ */
public MaxCountExceededException(Number max) { public MaxCountExceededException(Number max) {
super(LocalizedFormats.MAX_COUNT_EXCEEDED, max); this(LocalizedFormats.MAX_COUNT_EXCEEDED, max);
this.max = max;
} }
/** /**
* Construct the exception with a specific context. * Construct the exception with a specific context.
@ -52,8 +51,8 @@ public class MaxCountExceededException extends MathIllegalStateException {
public MaxCountExceededException(Localizable specific, public MaxCountExceededException(Localizable specific,
Number max, Number max,
Object ... args) { Object ... args) {
this(max); getContext().addMessage(specific, max, args);
addMessage(specific, max, args); this.max = max;
} }
/** /**

View File

@ -34,6 +34,7 @@ public class TooManyEvaluationsException extends MaxCountExceededException {
* @param max Maximum number of evaluations. * @param max Maximum number of evaluations.
*/ */
public TooManyEvaluationsException(Number max) { public TooManyEvaluationsException(Number max) {
super(LocalizedFormats.EVALUATIONS, max); super(max);
getContext().addMessage(LocalizedFormats.EVALUATIONS);
} }
} }

View File

@ -52,6 +52,13 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces! If the output is not quite correct, check for invisible trailing spaces!
--> -->
<release version="3.0" date="TBD" description="TBD"> <release version="3.0" date="TBD" description="TBD">
<action dev="erans" type="add" issue="MATH-566">
Created an "ExceptionContext" class: It provides the customization feature of
"MathRuntimeException" without imposing a singly rooted hierarchy of the Comons
Math exceptions.
Thus, those exceptions now inherit from their Java standard counterparts (e.g.
"MathIllegalArgumentException" inherits from "IllegalArgumentException").
</action>
<action dev="luc" type="add" issue="MATH-567" due-to="Michel"> <action dev="luc" type="add" issue="MATH-567" due-to="Michel">
Fixed conversion problems to/from 0 in Decimal Floating Point (Dfp) class. Fixed conversion problems to/from 0 in Decimal Floating Point (Dfp) class.
</action> </action>