added back some features from old exception scheme (accessors for patterns and arguments) to the new scheme

this should ease transition for existing user code

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1035476 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2010-11-15 22:39:56 +00:00
parent e5413fd483
commit f2d9bc8f93
12 changed files with 168 additions and 58 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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.
*

View File

@ -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());
}
}

View File

@ -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();
}

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);