diff --git a/src/main/java/org/apache/commons/math/MathException.java b/src/main/java/org/apache/commons/math/MathException.java index ad9fc2b7e..58e8c0567 100644 --- a/src/main/java/org/apache/commons/math/MathException.java +++ b/src/main/java/org/apache/commons/math/MathException.java @@ -21,6 +21,7 @@ import java.io.PrintWriter; import java.text.MessageFormat; import java.util.Locale; +import org.apache.commons.math.exception.MathThrowable; import org.apache.commons.math.exception.util.DummyLocalizable; import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -35,7 +36,7 @@ import org.apache.commons.math.exception.util.LocalizedFormats; * * @version $Revision$ $Date$ */ -public class MathException extends Exception { +public class MathException extends Exception implements MathThrowable { /** Serializable version identifier. */ private static final long serialVersionUID = 7428019509644517071L; @@ -134,27 +135,24 @@ public class MathException extends Exception { * * @return the pattern used to build the message of this throwable * @since 1.2 - * @deprecated as of 2.2 replaced by {@link #getLocalizablePattern()} + * @deprecated as of 2.2 replaced by {@link #getSpecificPattern()} and {@link #getGeneralPattern()} */ @Deprecated public String getPattern() { return pattern.getSourceString(); } - /** Gets the localizable pattern used to build the message of this throwable. - * - * @return the localizable pattern used to build the message of this throwable - * @since 2.2 - */ - public Localizable getLocalizablePattern() { + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return null; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { return pattern; } - /** Gets the arguments used to build the message of this throwable. - * - * @return the arguments used to build the message of this throwable - * @since 1.2 - */ + /** {@inheritDoc} */ public Object[] getArguments() { return arguments.clone(); } diff --git a/src/main/java/org/apache/commons/math/MathRuntimeException.java b/src/main/java/org/apache/commons/math/MathRuntimeException.java index b7cb614a8..908bab0bd 100644 --- a/src/main/java/org/apache/commons/math/MathRuntimeException.java +++ b/src/main/java/org/apache/commons/math/MathRuntimeException.java @@ -26,6 +26,7 @@ import java.util.ConcurrentModificationException; import java.util.Locale; import java.util.NoSuchElementException; +import org.apache.commons.math.exception.MathThrowable; import org.apache.commons.math.exception.util.DummyLocalizable; import org.apache.commons.math.exception.util.Localizable; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -36,7 +37,7 @@ import org.apache.commons.math.exception.util.LocalizedFormats; * @version $Revision$ $Date$ * @since 2.0 */ -public class MathRuntimeException extends RuntimeException { +public class MathRuntimeException extends RuntimeException implements MathThrowable { /** Serializable version identifier. */ private static final long serialVersionUID = 9058794795027570002L; @@ -139,26 +140,24 @@ public class MathRuntimeException extends RuntimeException { /** Gets the pattern used to build the message of this throwable. * * @return the pattern used to build the message of this throwable - * @deprecated as of 2.2 replaced by {@link #getLocalizablePattern()} + * @deprecated as of 2.2 replaced by {@link #getSpecificPattern()} and {@link #getGeneralPattern()} */ @Deprecated public String getPattern() { return pattern.getSourceString(); } - /** Gets the localizable pattern used to build the message of this throwable. - * - * @return the localizable pattern used to build the message of this throwable - * @since 2.2 - */ - public Localizable getLocalizablePattern() { + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return null; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { return pattern; } - /** Gets the arguments used to build the message of this throwable. - * - * @return the arguments used to build the message of this throwable - */ + /** {@inheritDoc} */ public Object[] getArguments() { return arguments.clone(); } diff --git a/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java b/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java index adbc6c1ba..2f418736c 100644 --- a/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java @@ -19,14 +19,14 @@ package org.apache.commons.math.distribution; import java.io.Serializable; import org.apache.commons.math.ConvergenceException; -import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.MathException; import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.solvers.BrentSolver; import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils; -import org.apache.commons.math.exception.util.LocalizedFormats; -import org.apache.commons.math.exception.OutOfRangeException; +import org.apache.commons.math.exception.MathUserException; import org.apache.commons.math.exception.NotStrictlyPositiveException; +import org.apache.commons.math.exception.OutOfRangeException; +import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.random.RandomDataImpl; import org.apache.commons.math.util.FastMath; @@ -82,17 +82,17 @@ public abstract class AbstractContinuousDistribution // subclasses can override if there is a better method. UnivariateRealFunction rootFindingFunction = new UnivariateRealFunction() { - public double value(double x) throws FunctionEvaluationException { + public double value(double x) throws MathUserException { double ret = Double.NaN; try { ret = cumulativeProbability(x) - p; } catch (MathException ex) { - throw new FunctionEvaluationException(ex, x, ex.getLocalizablePattern(), - ex.getArguments()); + throw new MathUserException(ex, + ex.getSpecificPattern(), ex.getGeneralPattern(), + ex.getArguments()); } if (Double.isNaN(ret)) { - throw new FunctionEvaluationException(x, - LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p); + throw new MathUserException(LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p); } return ret; } diff --git a/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java b/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java index 89b85dfd8..cb9b543bf 100644 --- a/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/AbstractIntegerDistribution.java @@ -18,7 +18,6 @@ package org.apache.commons.math.distribution; import java.io.Serializable; -import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.MathException; import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.NumberIsTooSmallException; @@ -254,28 +253,19 @@ public abstract class AbstractIntegerDistribution extends AbstractDistribution /** * Computes the cumulative probability function and checks for NaN * values returned. - * Throws MathException if the value is NaN. Wraps and rethrows any - * MathException encountered evaluating the cumulative probability - * function in a FunctionEvaluationException. - * Throws FunctionEvaluationException of the cumulative probability - * function returns NaN. + * Throws MathException if the value is NaN. Rethrows any MathException encountered + * evaluating the cumulative probability function. Throws + * MathException of the cumulative probability function returns NaN. * * @param argument Input value. * @return the cumulative probability. - * @throws FunctionEvaluationException if a MathException occurs - * computing the cumulative probability. */ private double checkedCumulativeProbability(int argument) - throws FunctionEvaluationException { + throws MathException { double result = Double.NaN; - try { result = cumulativeProbability(argument); - } catch (MathException ex) { - throw new FunctionEvaluationException(ex, argument, ex.getLocalizablePattern(), ex.getArguments()); - } if (Double.isNaN(result)) { - throw new FunctionEvaluationException(argument, - LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument); + throw new MathException(LocalizedFormats.DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument); } return result; } diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java index 43f8ed34a..d9f23c18f 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java @@ -32,7 +32,7 @@ import org.apache.commons.math.exception.util.Localizable; * @since 2.2 * @version $Revision$ $Date$ */ -public class MathIllegalArgumentException extends IllegalArgumentException { +public class MathIllegalArgumentException extends IllegalArgumentException implements MathThrowable { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; @@ -72,6 +72,21 @@ public class MathIllegalArgumentException extends IllegalArgumentException { this(null, general, args); } + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return specific; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { + return general; + } + + /** {@inheritDoc} */ + public Object[] getArguments() { + return arguments.clone(); + } + /** * Get the message in a specified locale. * diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalStateException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalStateException.java index 12f9bce13..944d45f76 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalStateException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalStateException.java @@ -29,7 +29,7 @@ import org.apache.commons.math.exception.util.Localizable; * @since 2.2 * @version $Revision$ $Date$ */ -public class MathIllegalStateException extends IllegalStateException { +public class MathIllegalStateException extends IllegalStateException implements MathThrowable { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; @@ -69,6 +69,21 @@ public class MathIllegalStateException extends IllegalStateException { this(null, general, args); } + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return specific; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { + return general; + } + + /** {@inheritDoc} */ + public Object[] getArguments() { + return arguments.clone(); + } + /** * Get the message in a specified locale. * @@ -91,4 +106,5 @@ public class MathIllegalStateException extends IllegalStateException { public String getLocalizedMessage() { return getMessage(Locale.getDefault()); } + } diff --git a/src/main/java/org/apache/commons/math/exception/MathThrowable.java b/src/main/java/org/apache/commons/math/exception/MathThrowable.java new file mode 100644 index 000000000..774654fe6 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/MathThrowable.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import java.util.Locale; + +import org.apache.commons.math.exception.util.Localizable; + +/** +* Interface for commons-math throwables. +* +* @version $Revision$ $Date$ +* @since 2.2 +*/ +public interface MathThrowable { + + /** Gets the localizable pattern used to build the specific part of the message of this throwable. + * @return localizable pattern used to build the specific part of the message of this throwable + */ + Localizable getSpecificPattern(); + + /** Gets the localizable pattern used to build the general part of the message of this throwable. + * @return localizable pattern used to build the general part of the message of this throwable + */ + Localizable getGeneralPattern(); + + /** Gets the arguments used to build the message of this throwable. + * @return the arguments used to build the message of this throwable + */ + Object[] getArguments(); + + /** Gets the message in a specified locale. + * @param locale Locale in which the message should be translated + * @return localized message + */ + String getMessage(final Locale locale); + + /** Gets the message in a conventional US locale. + * @return localized message + */ + String getMessage(); + + /** Gets the message in the system default locale. + * @return localized message + */ + String getLocalizedMessage(); + +} diff --git a/src/main/java/org/apache/commons/math/exception/MathUnsupportedOperationException.java b/src/main/java/org/apache/commons/math/exception/MathUnsupportedOperationException.java index 521031d53..592aa3702 100644 --- a/src/main/java/org/apache/commons/math/exception/MathUnsupportedOperationException.java +++ b/src/main/java/org/apache/commons/math/exception/MathUnsupportedOperationException.java @@ -32,7 +32,7 @@ import org.apache.commons.math.exception.util.LocalizedFormats; * @since 2.2 * @version $Revision$ $Date$ */ -public class MathUnsupportedOperationException extends UnsupportedOperationException { +public class MathUnsupportedOperationException extends UnsupportedOperationException implements MathThrowable { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; @@ -63,6 +63,21 @@ public class MathUnsupportedOperationException extends UnsupportedOperationExcep arguments = ArgUtils.flatten(args); } + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return specific; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { + return LocalizedFormats.UNSUPPORTED_OPERATION; + } + + /** {@inheritDoc} */ + public Object[] getArguments() { + return arguments.clone(); + } + /** * Get the message in a specified locale. * diff --git a/src/main/java/org/apache/commons/math/exception/MathUserException.java b/src/main/java/org/apache/commons/math/exception/MathUserException.java index f60293c4c..de1c77e6e 100644 --- a/src/main/java/org/apache/commons/math/exception/MathUserException.java +++ b/src/main/java/org/apache/commons/math/exception/MathUserException.java @@ -32,7 +32,7 @@ import org.apache.commons.math.exception.util.MessageFactory; * @since 2.2 * @version $Revision$ $Date$ */ -public class MathUserException extends RuntimeException { +public class MathUserException extends RuntimeException implements MathThrowable { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** @@ -116,6 +116,21 @@ public class MathUserException extends RuntimeException { this.arguments = ArgUtils.flatten(arguments); } + /** {@inheritDoc} */ + public Localizable getSpecificPattern() { + return specific; + } + + /** {@inheritDoc} */ + public Localizable getGeneralPattern() { + return general; + } + + /** {@inheritDoc} */ + public Object[] getArguments() { + return arguments.clone(); + } + /** * Get the message in a specified locale. * diff --git a/src/test/java/org/apache/commons/math/ConvergenceExceptionTest.java b/src/test/java/org/apache/commons/math/ConvergenceExceptionTest.java index cf3eb95fc..6d6673fe5 100644 --- a/src/test/java/org/apache/commons/math/ConvergenceExceptionTest.java +++ b/src/test/java/org/apache/commons/math/ConvergenceExceptionTest.java @@ -41,7 +41,7 @@ public class ConvergenceExceptionTest extends TestCase { Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) }; ConvergenceException ex = new ConvergenceException(pattern, arguments); assertNull(ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]); @@ -64,7 +64,7 @@ public class ConvergenceExceptionTest extends TestCase { Exception cause = new Exception(inMsg); ConvergenceException ex = new ConvergenceException(cause, pattern, arguments); assertEquals(cause, ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]); diff --git a/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java b/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java index f7b6e4e30..82b354f93 100644 --- a/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java +++ b/src/test/java/org/apache/commons/math/MathConfigurationExceptionTest.java @@ -40,7 +40,7 @@ public class MathConfigurationExceptionTest extends TestCase { Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) }; MathConfigurationException ex = new MathConfigurationException(pattern, arguments); assertNull(ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]); @@ -63,7 +63,7 @@ public class MathConfigurationExceptionTest extends TestCase { Exception cause = new Exception(inMsg); MathConfigurationException ex = new MathConfigurationException(cause, pattern, arguments); assertEquals(cause, ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]); diff --git a/src/test/java/org/apache/commons/math/MathExceptionTest.java b/src/test/java/org/apache/commons/math/MathExceptionTest.java index 0865a964e..d299aee8e 100644 --- a/src/test/java/org/apache/commons/math/MathExceptionTest.java +++ b/src/test/java/org/apache/commons/math/MathExceptionTest.java @@ -45,7 +45,7 @@ public class MathExceptionTest extends TestCase { Object[] arguments = { Integer.valueOf(6), Integer.valueOf(4) }; MathException ex = new MathException(pattern, arguments); assertNull(ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]); @@ -68,7 +68,7 @@ public class MathExceptionTest extends TestCase { Exception cause = new Exception(inMsg); MathException ex = new MathException(cause, pattern, arguments); assertEquals(cause, ex.getCause()); - assertEquals(pattern, ex.getLocalizablePattern()); + assertEquals(pattern, ex.getGeneralPattern()); assertEquals(arguments.length, ex.getArguments().length); for (int i = 0; i < arguments.length; ++i) { assertEquals(arguments[i], ex.getArguments()[i]);