Created "NullArgumentException" (subclass of "MathIllegalArgumentException").
Changed all occurrences of "createIllegalArgumentException" (in "MathRuntime")
to throw the new exception whenever a "null" is not allowed. Added a few
checks for null in "BigFraction.java".
Simplified "LocalizedFormats" enum.
Changed "ZeroNotAllowedException" to "ZeroException".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@982950 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-08-06 13:29:01 +00:00
parent d402f6b5e1
commit c15e80eba9
29 changed files with 229 additions and 168 deletions

View File

@ -20,6 +20,7 @@ import org.apache.commons.math.ConvergingAlgorithmImpl;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Provide a default implementation for several generic functions.
@ -67,7 +68,7 @@ public abstract class UnivariateRealIntegratorImpl
throws IllegalArgumentException {
super(defaultMaximalIterationCount, 1.0e-15);
if (f == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FUNCTION);
throw new NullArgumentException(LocalizedFormats.FUNCTION);
}
this.f = f;

View File

@ -22,6 +22,7 @@ import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Provide a default implementation for several functions useful to generic
@ -74,7 +75,7 @@ public abstract class UnivariateRealSolverImpl
final double defaultAbsoluteAccuracy) {
super(defaultMaximalIterationCount, defaultAbsoluteAccuracy);
if (f == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FUNCTION);
throw new NullArgumentException(LocalizedFormats.FUNCTION);
}
this.f = f;
this.defaultFunctionValueAccuracy = 1.0e-15;

View File

@ -21,6 +21,7 @@ import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Utility routines for {@link UnivariateRealSolver} objects.
@ -170,7 +171,7 @@ public class UnivariateRealSolverUtils {
FunctionEvaluationException {
if (function == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FUNCTION);
throw new NullArgumentException(LocalizedFormats.FUNCTION);
}
if (maximumIterations <= 0) {
throw MathRuntimeException.createIllegalArgumentException(
@ -225,7 +226,7 @@ public class UnivariateRealSolverUtils {
*/
private static void setup(UnivariateRealFunction f) {
if (f == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FUNCTION);
throw new NullArgumentException(LocalizedFormats.FUNCTION);
}
}

View File

@ -26,6 +26,7 @@ import java.util.Locale;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.util.CompositeFormat;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Formats a Complex number in cartesian format "Re(c) + Im(c)i". 'i' can
@ -358,13 +359,11 @@ public class ComplexFormat extends CompositeFormat {
/**
* Modify the imaginaryFormat.
* @param imaginaryFormat The new imaginaryFormat value.
* @throws IllegalArgumentException if <code>imaginaryFormat</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code imaginaryFormat} is {@code null}.
*/
public void setImaginaryFormat(NumberFormat imaginaryFormat) {
if (imaginaryFormat == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_IMAGINARY_FORMAT);
throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT);
}
this.imaginaryFormat = imaginaryFormat;
}
@ -372,13 +371,11 @@ public class ComplexFormat extends CompositeFormat {
/**
* Modify the realFormat.
* @param realFormat The new realFormat value.
* @throws IllegalArgumentException if <code>realFormat</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code realFormat} is {@code null}.
*/
public void setRealFormat(NumberFormat realFormat) {
if (realFormat == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_REAL_FORMAT);
throw new NullArgumentException(LocalizedFormats.REAL_FORMAT);
}
this.realFormat = realFormat;
}

View File

@ -145,11 +145,10 @@ public enum LocalizedFormats implements Localizable {
NEGATIVE_COMPLEX_MODULE("negative complex module {0}"),
NEGATIVE_ELEMENT_AT_2D_INDEX("element ({0}, {1}) is negative: {2}"),
NEGATIVE_ELEMENT_AT_INDEX("element {0} is negative: {1}"),
NEGATIVE_LENGTH("length cannot be negative ({0})"),
NEGATIVE_NUMBER_OF_SUCCESSES("number of successes must be non-negative ({0})"),
NEGATIVE_NUMBER_OF_TRIALS("number of trials must be non-negative ({0})"),
NEGATIVE_ROBUSTNESS_ITERATIONS("the number of robustness iterations must be non-negative, but got {0}"),
NEGATIVE_START_POSITION("start position cannot be negative ({0})"),
START_POSITION("start position ({0})"), /* keep */
NON_CONVERGENT_CONTINUED_FRACTION("Continued fraction convergents failed to converge for value {0}"),
NON_POSITIVE_MICROSPHERE_ELEMENTS("number of microsphere elements must be positive, but got {0}"),
NON_POSITIVE_POLYNOMIAL_DEGREE("polynomial degree must be positive: degree={0}"),
@ -211,18 +210,19 @@ public enum LocalizedFormats implements Localizable {
NO_OPTIMUM_COMPUTED_YET("no optimum computed yet"),
NO_RESULT_AVAILABLE("no result available"),
NO_SUCH_MATRIX_ENTRY("no entry at indices ({0}, {1}) in a {2}x{3} matrix"),
NULL_COVARIANCE_MATRIX("covariance matrix is null"),
NULL_DENOMINATOR("denominator is null"),
NULL_DENOMINATOR_FORMAT("denominator format can not be null"),
NULL_FRACTION("null fraction"),
NULL_FUNCTION("function is null"),
NULL_IMAGINARY_FORMAT("null imaginary format"),
NULL_INPUT_ARRAY("input array is null"),
NULL_NUMERATOR("numerator is null"),
NULL_NUMERATOR_FORMAT("numerator format can not be null"),
NULL_OBJECT_TRANSFORMATION("Conversion Exception in Transformation, Object is null"),
NULL_REAL_FORMAT("null real format"),
NULL_WHOLE_FORMAT("whole format can not be null"),
NULL_NOT_ALLOWED("null is not allowed"), /* keep */
COVARIANCE_MATRIX("covariance matrix"), /* keep */
DENOMINATOR("denominator"), /* keep */
DENOMINATOR_FORMAT("denominator format"), /* keep */
FRACTION("fraction"), /* keep */
FUNCTION("function"), /* keep */
IMAGINARY_FORMAT("imaginary format"), /* keep */
INPUT_ARRAY("input array"), /* keep */
NUMERATOR("numerator"), /* keep */
NUMERATOR_FORMAT("numerator format"), /* keep */
OBJECT_TRANSFORMATION("conversion exception in transformation"), /* keep */
REAL_FORMAT("real format"), /* keep */
WHOLE_FORMAT("whole format"), /* keep */
NUMBER_TOO_LARGE("{0} is larger than the maximum ({1})"), /* keep */
NUMBER_TOO_SMALL("{0} is smaller than the minimum ({1})"), /* keep */
NUMBER_TOO_LARGE_BOUND_EXCLUDED("{0} is larger than, or equal to, the maximum ({1})"), /* keep */

View File

@ -0,0 +1,47 @@
/*
* 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;
/**
* All conditions checks that fail due to a {@code null} argument must throw
* this exception.
* This class is meant to signal a precondition violation ("null is an illegal
* argument") and so does not extend the standard {@code NullPointerException}.
* Proagation of {@code NullPointerException} from within Commons-Math is
* construed to be a bug.
*
* @since 2.2
* @version $Revision$ $Date$
*/
public class NullArgumentException extends MathIllegalArgumentException {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/**
* Default constructor.
*/
public NullArgumentException() {
super(LocalizedFormats.NULL_NOT_ALLOWED);
}
/**
* @param specific Message pattern providing the specific context of
* the error.
*/
public NullArgumentException(Localizable specific) {
super(specific, LocalizedFormats.NULL_NOT_ALLOWED);
}
}

View File

@ -23,7 +23,7 @@ package org.apache.commons.math.exception;
* @since 2.2
* @version $Revision$ $Date$
*/
public class ZeroNotAllowedException extends MathIllegalNumberException {
public class ZeroException extends MathIllegalNumberException {
/** Serializable version identifier */
private static final long serialVersionUID = -1960874856936000015L;
@ -31,7 +31,7 @@ public class ZeroNotAllowedException extends MathIllegalNumberException {
/**
* Construct the exception.
*/
public ZeroNotAllowedException() {
public ZeroException() {
this(null);
}
@ -40,8 +40,7 @@ public class ZeroNotAllowedException extends MathIllegalNumberException {
*
* @param specific Specific contexte pattern .
*/
public ZeroNotAllowedException(Localizable specific) {
public ZeroException(Localizable specific) {
super(specific, LocalizedFormats.ZERO_NOT_ALLOWED, 0);
}
}

View File

@ -23,7 +23,7 @@ import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.LocalizedFormats;
/**
@ -114,13 +114,11 @@ public abstract class AbstractFormat extends NumberFormat implements Serializabl
/**
* Modify the denominator format.
* @param format the new denominator format value.
* @throws IllegalArgumentException if <code>format</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code format} is {@code null}.
*/
public void setDenominatorFormat(final NumberFormat format) {
if (format == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_DENOMINATOR_FORMAT);
throw new NullArgumentException(LocalizedFormats.DENOMINATOR_FORMAT);
}
this.denominatorFormat = format;
}
@ -128,13 +126,11 @@ public abstract class AbstractFormat extends NumberFormat implements Serializabl
/**
* Modify the numerator format.
* @param format the new numerator format value.
* @throws IllegalArgumentException if <code>format</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code format} is {@code null}.
*/
public void setNumeratorFormat(final NumberFormat format) {
if (format == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_NUMERATOR_FORMAT);
throw new NullArgumentException(LocalizedFormats.NUMERATOR_FORMAT);
}
this.numeratorFormat = format;
}

View File

@ -23,6 +23,7 @@ import java.math.BigInteger;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.util.MathUtils;
/**
@ -104,19 +105,20 @@ public class BigFraction
}
/**
* <p>
* Create a {@link BigFraction} given the numerator and denominator as
* <code>BigInteger</code>. The {@link BigFraction} is reduced to lowest terms.
* </p>
* {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
*
* @param num
* the numerator, must not be <code>null</code>.
* @param den
* the denominator, must not be <code>null</code>.
* @throws ArithmeticException
* if the denominator is <code>zero</code>.
* @param num the numerator, must not be {@code null}.
* @param den the denominator, must not be {@code null}..
* @throws ArithmeticException if the denominator is zero.
*/
public BigFraction(BigInteger num, BigInteger den) {
if (num == null) {
throw new NullArgumentException(LocalizedFormats.NUMERATOR);
}
if (den == null) {
throw new NullArgumentException(LocalizedFormats.DENOMINATOR);
}
if (BigInteger.ZERO.equals(den)) {
throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
@ -498,10 +500,12 @@ public class BigFraction
* @param fraction
* the {@link BigFraction} to add, must not be <code>null</code>.
* @return a {@link BigFraction} instance with the resulting values.
* @throws NullPointerException
* if the {@link BigFraction} is <code>null</code>.
* @throws NullArgumentException if the {@link BigFraction} is {@code null}.
*/
public BigFraction add(final BigFraction fraction) {
if (fraction == null) {
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (ZERO.equals(fraction)) {
return this;
}
@ -601,8 +605,7 @@ public class BigFraction
* the <code>BigInteger</code> to divide by, must not be
* <code>null</code>.
* @return a {@link BigFraction} instance with the resulting values.
* @throws NullPointerException
* if the <code>BigInteger</code> is <code>null</code>.
* @throws NullArgumentException if the {@code BigInteger} is {@code null}.
* @throws ArithmeticException
* if the fraction to divide by is zero.
*/
@ -651,15 +654,15 @@ public class BigFraction
* reduced form.
* </p>
*
* @param fraction
* the fraction to divide by, must not be <code>null</code>.
* @param fraction Fraction to divide by, must not be {@code null}.
* @return a {@link BigFraction} instance with the resulting values.
* @throws NullPointerException
* if the fraction is <code>null</code>.
* @throws ArithmeticException
* if the fraction to divide by is zero.
* @throws NullArgumentException if the {@code fraction} is {@code null}.
* @throws ArithmeticException if the fraction to divide by is zero.
*/
public BigFraction divide(final BigFraction fraction) {
if (fraction == null) {
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (BigInteger.ZERO.equals(fraction.numerator)) {
throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
}
@ -838,13 +841,14 @@ public class BigFraction
* <code>BigInteger</code>, returning the result in reduced form.
* </p>
*
* @param bg
* the <code>BigInteger</code> to multiply by.
* @return a <code>BigFraction</code> instance with the resulting values.
* @throws NullPointerException
* if the bg is <code>null</code>.
* @param bg the {@code BigInteger} to multiply by.
* @return a {@code BigFraction} instance with the resulting values.
* @throws NullArgumentException if {@code bg} is {@code null}.
*/
public BigFraction multiply(final BigInteger bg) {
if (bg == null) {
throw new NullArgumentException();
}
return new BigFraction(bg.multiply(numerator), denominator);
}
@ -882,13 +886,14 @@ public class BigFraction
* reduced form.
* </p>
*
* @param fraction
* the fraction to multiply by, must not be <code>null</code>.
* @param fraction Fraction to multiply by, must not be {@code null}.
* @return a {@link BigFraction} instance with the resulting values.
* @throws NullPointerException
* if the fraction is <code>null</code>.
* @throws NullArgumentException if {@code fraction} is {@code null}.
*/
public BigFraction multiply(final BigFraction fraction) {
if (fraction == null) {
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (numerator.equals(BigInteger.ZERO) ||
fraction.numerator.equals(BigInteger.ZERO)) {
return ZERO;
@ -1023,14 +1028,14 @@ public class BigFraction
* returning the result in reduced form.
* </p>
*
* @param bg
* the {@link BigInteger} to subtract, must'nt be
* <code>null</code>.
* @return a <code>BigFraction</code> instance with the resulting values.
* @throws NullPointerException
* if the {@link BigInteger} is <code>null</code>.
* @param bg the {@link BigInteger} to subtract, cannot be {@code null}.
* @return a {@code BigFraction} instance with the resulting values.
* @throws NullArgumentException if the {@link BigInteger} is {@code null}.
*/
public BigFraction subtract(final BigInteger bg) {
if (bg == null) {
throw new NullArgumentException();
}
return new BigFraction(numerator.subtract(denominator.multiply(bg)), denominator);
}
@ -1069,14 +1074,14 @@ public class BigFraction
* returning the result in reduced form.
* </p>
*
* @param fraction
* the {@link BigFraction} to subtract, must not be
* <code>null</code>.
* @param fraction {@link BigFraction} to subtract, must not be {@code null}.
* @return a {@link BigFraction} instance with the resulting values
* @throws NullPointerException
* if the fraction is <code>null</code>.
* @throws NullArgumentException if the {@code fraction} is {@code null}.
*/
public BigFraction subtract(final BigFraction fraction) {
if (fraction == null) {
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (ZERO.equals(fraction)) {
return this;
}

View File

@ -22,6 +22,7 @@ import java.math.BigInteger;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.util.MathUtils;
/**
@ -473,7 +474,7 @@ public class Fraction
*/
private Fraction addSub(Fraction fraction, boolean isAdd) {
if (fraction == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FRACTION);
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
// zero is identity for addition.
if (numerator == 0) {
@ -530,7 +531,7 @@ public class Fraction
*/
public Fraction multiply(Fraction fraction) {
if (fraction == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FRACTION);
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (numerator == 0 || fraction.numerator == 0) {
return ZERO;
@ -565,7 +566,7 @@ public class Fraction
*/
public Fraction divide(Fraction fraction) {
if (fraction == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_FRACTION);
throw new NullArgumentException(LocalizedFormats.FRACTION);
}
if (fraction.numerator == 0) {
throw MathRuntimeException.createArithmeticException(

View File

@ -21,8 +21,8 @@ import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Formats a BigFraction number in proper format. The number format for each of
@ -228,15 +228,12 @@ public class ProperBigFractionFormat extends BigFractionFormat {
/**
* Modify the whole format.
* @param format The new whole format value.
* @throws IllegalArgumentException if <code>format</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code format} is {@code null}.
*/
public void setWholeFormat(final NumberFormat format) {
if (format == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_WHOLE_FORMAT);
throw new NullArgumentException(LocalizedFormats.WHOLE_FORMAT);
}
this.wholeFormat = format;
}
}

View File

@ -20,8 +20,8 @@ import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.util.MathUtils;
/**
@ -221,13 +221,11 @@ public class ProperFractionFormat extends FractionFormat {
/**
* Modify the whole format.
* @param format The new whole format value.
* @throws IllegalArgumentException if <code>format</code> is
* <code>null</code>.
* @throws NullArgumentException if {@code format} is {@code null}.
*/
public void setWholeFormat(NumberFormat format) {
if (format == null) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NULL_WHOLE_FORMAT);
throw new NullArgumentException(LocalizedFormats.WHOLE_FORMAT);
}
this.wholeFormat = format;
}

View File

@ -24,7 +24,8 @@ import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.ZeroNotAllowedException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.exception.NullArgumentException;
/**
* The derivative of {@link GaussianFunction}. Specifically:
@ -65,7 +66,7 @@ public class GaussianDerivativeFunction implements UnivariateRealFunction, Seria
*/
public GaussianDerivativeFunction(double b, double c, double d) {
if (d == 0.0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
this.b = b;
this.c = c;
@ -83,13 +84,13 @@ public class GaussianDerivativeFunction implements UnivariateRealFunction, Seria
*/
public GaussianDerivativeFunction(double[] parameters) {
if (parameters == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (parameters.length != 3) {
throw new DimensionMismatchException(3, parameters.length);
}
if (parameters[2] == 0.0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
this.b = parameters[0];
this.c = parameters[1];

View File

@ -18,8 +18,7 @@
package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.optimization.
DifferentiableMultivariateVectorialOptimizer;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.fitting.CurveFitter;
import org.apache.commons.math.optimization.fitting.WeightedObservedPoint;

View File

@ -25,7 +25,8 @@ import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.ZeroNotAllowedException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.exception.NullArgumentException;
/**
* A Gaussian function. Specifically:
@ -77,7 +78,7 @@ public class GaussianFunction implements DifferentiableUnivariateRealFunction, S
*/
public GaussianFunction(double a, double b, double c, double d) {
if (d == 0.0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
this.a = a;
this.b = b;
@ -97,13 +98,13 @@ public class GaussianFunction implements DifferentiableUnivariateRealFunction, S
*/
public GaussianFunction(double[] parameters) {
if (parameters == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (parameters.length != 4) {
throw new DimensionMismatchException(4, parameters.length);
}
if (parameters[3] == 0.0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
this.a = parameters[0];
this.b = parameters[1];

View File

@ -20,14 +20,14 @@ package org.apache.commons.math.optimization.fitting;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.ZeroNotAllowedException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.exception.NullArgumentException;
/**
* Guesses the parameters (<tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and <tt>d</tt>)
* Guesses the parameters ({@code a}, {@code b}, {@code c}, and {@code d})
* of a {@link ParametricGaussianFunction} based on the specified observed
* points.
*
@ -49,7 +49,7 @@ public class GaussianParametersGuesser {
*/
public GaussianParametersGuesser(WeightedObservedPoint[] observations) {
if (observations == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (observations.length < 3) {
throw new NumberIsTooSmallException(observations.length, 3, true);
@ -154,7 +154,7 @@ public class GaussianParametersGuesser {
private double interpolateXAtY(WeightedObservedPoint[] points,
int startIdx, int idxStep, double y) throws OutOfRangeException {
if (idxStep == 0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
WeightedObservedPoint[] twoPoints = getInterpolationPointsForY(points, startIdx, idxStep, y);
WeightedObservedPoint pointA = twoPoints[0];
@ -190,7 +190,7 @@ public class GaussianParametersGuesser {
int startIdx, int idxStep, double y)
throws OutOfRangeException {
if (idxStep == 0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
for (int i = startIdx;
(idxStep < 0) ? (i + idxStep >= 0) : (i + idxStep < points.length);

View File

@ -20,10 +20,10 @@ package org.apache.commons.math.optimization.fitting;
import java.io.Serializable;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.ZeroNotAllowedException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
/**
@ -153,13 +153,13 @@ public class ParametricGaussianFunction implements ParametricRealFunction, Seria
*/
private void validateParameters(double[] parameters) throws FunctionEvaluationException {
if (parameters == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (parameters.length != 4) {
throw new DimensionMismatchException(4, parameters.length);
}
if (parameters[3] == 0.0) {
throw new ZeroNotAllowedException();
throw new ZeroException();
}
}

View File

@ -21,6 +21,8 @@ import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.TDistributionImpl;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.BlockRealMatrix;
import org.apache.commons.math.stat.regression.SimpleRegression;
@ -92,7 +94,7 @@ public class PearsonsCorrelation {
public PearsonsCorrelation(Covariance covariance) {
RealMatrix covarianceMatrix = covariance.getCovarianceMatrix();
if (covarianceMatrix == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_COVARIANCE_MATRIX);
throw new NullArgumentException(LocalizedFormats.COVARIANCE_MATRIX);
}
nObs = covariance.getN();
correlationMatrix = covarianceToCorrelation(covarianceMatrix);
@ -225,8 +227,7 @@ public class PearsonsCorrelation {
public double correlation(final double[] xArray, final double[] yArray) throws IllegalArgumentException {
SimpleRegression regression = new SimpleRegression();
if (xArray.length != yArray.length) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, xArray.length, yArray.length);
throw new DimensionMismatchException(xArray.length, yArray.length);
} else if (xArray.length < 2) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);

View File

@ -16,8 +16,8 @@
*/
package org.apache.commons.math.stat.descriptive;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.util.MathUtils;
/**
@ -56,7 +56,7 @@ public abstract class AbstractStorelessUnivariateStatistic
@Override
public double evaluate(final double[] values) {
if (values == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
return evaluate(values, 0, values.length);
}
@ -124,7 +124,7 @@ public abstract class AbstractStorelessUnivariateStatistic
*/
public void incrementAll(double[] values) {
if (values == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
incrementAll(values, 0, values.length);
}

View File

@ -18,6 +18,9 @@ package org.apache.commons.math.stat.descriptive;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.DimensionMismatchException;
/**
* Abstract base class for all implementations of the
@ -78,17 +81,15 @@ public abstract class AbstractUnivariateStatistic
final int length) {
if (values == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (begin < 0) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NEGATIVE_START_POSITION, begin);
throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
}
if (length < 0) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NEGATIVE_LENGTH, length);
throw new NotPositiveException(LocalizedFormats.LENGTH, length);
}
if (begin + length > values.length) {
@ -140,12 +141,11 @@ public abstract class AbstractUnivariateStatistic
final int length) {
if (weights == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
if (weights.length != values.length) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, weights.length, values.length);
if (weights.length != values.length) {
throw new DimensionMismatchException(weights.length, values.length);
}
boolean containsPositiveWeight = false;

View File

@ -18,7 +18,7 @@
package org.apache.commons.math.stat.descriptive.moment;
import java.io.Serializable;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.stat.descriptive.AbstractUnivariateStatistic;
@ -176,7 +176,7 @@ public class SemiVariance extends AbstractUnivariateStatistic implements Seriali
@Override
public double evaluate(final double[] values) {
if (values == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
return evaluate(values, 0, values.length);
}

View File

@ -18,7 +18,7 @@ package org.apache.commons.math.stat.descriptive.moment;
import java.io.Serializable;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.stat.descriptive.WeightedEvaluation;
import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
@ -214,7 +214,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se
@Override
public double evaluate(final double[] values) {
if (values == null) {
throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NULL_INPUT_ARRAY);
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
}
return evaluate(values, 0, values.length);
}
@ -589,7 +589,6 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se
return result;
}
/**
* Copies source to dest.
* <p>Neither source nor dest can be null.</p>
@ -599,9 +598,12 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se
* @throws NullPointerException if either source or dest is null
*/
public static void copy(Variance source, Variance dest) {
if (source == null ||
dest == null) {
throw new NullArgumentException();
}
dest.moment = source.moment.copy();
dest.isBiasCorrected = source.isBiasCorrected;
dest.incMoment = source.incMoment;
}
}

View File

@ -21,6 +21,7 @@ import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.exception.LocalizedFormats;
import org.apache.commons.math.exception.NullArgumentException;
/**
* A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This
@ -38,14 +39,13 @@ public class DefaultTransformer implements NumberTransformer, Serializable {
/**
* @param o the object that gets transformed.
* @return a double primitive representation of the Object o.
* @throws org.apache.commons.math.MathException If it cannot successfully
* be transformed or is null.
* @throws MathException if it cannot successfully be transformed.
* @throws NullArgumentException if is {@code null}.
* @see <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html"/>
*/
public double transform(Object o) throws MathException{
public double transform(Object o) throws MathException {
if (o == null) {
throw new MathException(LocalizedFormats.NULL_OBJECT_TRANSFORMATION);
throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION);
}
if (o instanceof Number) {

View File

@ -117,11 +117,10 @@ NEGATIVE_BRIGHTNESS_EXPONENT = l''exposant de brillance devrait \u00eatre positi
NEGATIVE_COMPLEX_MODULE = module n\u00e9gatif ({0}) pour un nombre complexe
NEGATIVE_ELEMENT_AT_2D_INDEX = l''\u00e9l\u00e9ment ({0}, {1}) est n\u00e9gatif : {2}
NEGATIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} est n\u00e9gatif : {1}
NEGATIVE_LENGTH = la longueur ne peut pas \u00eatre n\u00e9gative
NEGATIVE_NUMBER_OF_SUCCESSES = le nombre de succ\u00e8s ne doit pas \u00eatre n\u00e9gatif ({0})
NEGATIVE_NUMBER_OF_TRIALS = le nombre d''essais ne doit pas \u00eatre n\u00e9gatif ({0})
NEGATIVE_ROBUSTNESS_ITERATIONS = le nombre d''it\u00e9rations robuste ne peut \u00eatre n\u00e9gatif, alors qu''il est de {0}
NEGATIVE_START_POSITION = la position de d\u00e9part ne peut pas \u00eatre n\u00e9gative
START_POSITION = position de d\u00e9part
NON_CONVERGENT_CONTINUED_FRACTION = \u00c9chec de convergence de fraction continue pour la valeur {0}
NON_POSITIVE_MICROSPHERE_ELEMENTS = le nombre d''\u00e9l\u00e9ments de la microsph\u00e8re devrait \u00eatre positif, or n = {0}
NON_POSITIVE_POLYNOMIAL_DEGREE = le polyn\u00f4me doit \u00eatre de degr\u00e9 positif : degr\u00e9 = {0}
@ -189,12 +188,12 @@ DENOMINATOR = d\u00e9nominateur
DENOMINATOR_FORMAT = format du d\u00e9nominateur
FRACTION = fraction
FUNCTION = fonction
IMAGINARY_FORMAT = format imaginaire
IMAGINARY_FORMAT = format de la partie imaginaire
INPUT_ARRAY = tableau d''entr\u00e9e
NUMERATOR = num\u00e9rateur
NUMERATOR_FORMAT = format du num\u00e9rateur
OBJECT_TRANSFORMATION = exception de conversion dans une transformation
REAL_FORMAT = format r\u00e9el
REAL_FORMAT = format de la partie r\u00e9elle
WHOLE_FORMAT = format complet
NUMBER_TOO_LARGE = {0} est plus grand que le maximum ({1})
NUMBER_TOO_SMALL = {0} est plus petit que le minimum ({1})

View File

@ -96,6 +96,8 @@ The <action> type attribute can be add,update,fix,remove.
Created package "exception" to contain the new exceptions hierarchy.
Created package "exception.util": utilities for the exception classes
(e.g. managing the localization of error messages).
Default policy for dealing with invalid null references: raise a
"NullArgumentException" (subclass of "IllegalArgumentException").
</action>
<action dev="erans" type="add" issue="MATH-378" due-to="Matthew Rowles">
Implementation of linear interpolation.

View File

@ -93,7 +93,7 @@
</subsection>
<subsection name="0.4 How interface contracts are specified in commons-math javadoc" href="contracts">
<p>
<p>
You should always read the javadoc class and method comments carefully when using
Commons Math components in your programs. The javadoc provides references to the algorithms
that are used, usage notes about limitations, performance, etc. as well as interface contracts.
@ -101,16 +101,27 @@
for the method to return valid results), special values returned (e.g. Double.NaN)
or exceptions that may be thrown if the preconditions are not met, and definitions for returned
values/objects or state changes.</p>
<p>
When the actual parameters provided to a method or the internal state of an object
make a computation meaningless, an IllegalArgumentException or IllegalStateException may
be thrown. Exact conditions under which runtime exceptions (and any other exceptions) are
thrown are specified in the javadoc method comments. In some cases, to be consistent with
the <a href="http://grouper.ieee.org/groups/754/">IEEE 754 standard</a> for floating point
arithmetic and with java.lang.Math, Commons Math methods return Double.NaN values.
Conditions under which Double.NaN or other special values are returned are fully specified
in the javadoc method comments.
</p>
<p>
When the actual parameters provided to a method or the internal state of an object
make a computation meaningless, an IllegalArgumentException or IllegalStateException may
be thrown. Exact conditions under which runtime exceptions (and any other exceptions) are
thrown are specified in the javadoc method comments. In some cases, to be consistent with
the <a href="http://grouper.ieee.org/groups/754/">IEEE 754 standard</a> for floating point
arithmetic and with java.lang.Math, Commons Math methods return Double.NaN values.
Conditions under which Double.NaN or other special values are returned are fully specified
in the javadoc method comments.
</p>
<p>
As of version 2.2, the policy for dealing with null references is as
follows: When an argument is unexpectedly null, a
<a href="../apidocs/org/apache/commons/math/exception/NullArgumentException.html">
NullArgumentException</a> is raised for signalling the
illegal argument. Note that this class does not inherit from the
standard <code>NullPointerException</code> but is a subclass of
<code>IllegalArgumentException</code>.
No <code>NullPointerException</code> should be propagated from within
Commons-Math.
</p>
</subsection>
<subsection name="0.5 Dependencies" href="dependencies">

View File

@ -21,6 +21,7 @@ import java.math.BigInteger;
import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.NullArgumentException;
import junit.framework.TestCase;
@ -64,12 +65,12 @@ public class BigFractionTest extends TestCase {
assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001));
try {
new BigFraction(null, BigInteger.ONE);
} catch (NullPointerException npe) {
} catch (NullArgumentException npe) {
// expected
}
try {
new BigFraction(BigInteger.ONE, null);
} catch (NullPointerException npe) {
} catch (NullArgumentException npe) {
// expected
}
try {
@ -326,8 +327,8 @@ public class BigFractionTest extends TestCase {
try {
f.add((BigFraction) null);
fail("expecting NullPointerException");
} catch (NullPointerException ex) {
fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {
}
// if this fraction is added naively, it will overflow.
@ -414,8 +415,8 @@ public class BigFractionTest extends TestCase {
try {
f.divide((BigFraction) null);
fail("expecting NullPointerException");
} catch (NullPointerException ex) {
fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {
}
f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
@ -460,8 +461,8 @@ public class BigFractionTest extends TestCase {
try {
f.multiply((BigFraction) null);
fail("expecting NullPointerException");
} catch (NullPointerException ex) {
fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {
}
}
@ -478,8 +479,8 @@ public class BigFractionTest extends TestCase {
BigFraction f = new BigFraction(1, 1);
try {
f.subtract((BigFraction) null);
fail("expecting NullPointerException");
} catch (NullPointerException ex) {
fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {
}
// if this fraction is subtracted naively, it will overflow.

View File

@ -18,7 +18,7 @@
package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.exception.ZeroNotAllowedException;
import org.apache.commons.math.exception.ZeroException;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.fitting.CurveFitter;
import org.apache.commons.math.optimization.general.
@ -133,7 +133,7 @@ public class ParametricGaussianFunctionTest {
*
* @throws FunctionEvaluationException in the event of a test case error
*/
@Test(expected=ZeroNotAllowedException.class)
@Test(expected=ZeroException.class)
public void testValue03() throws FunctionEvaluationException {
ParametricGaussianFunction f = new ParametricGaussianFunction();
f.value(0.0, new double[] {0.0, 1.0, 1.0, 0.0});

View File

@ -23,6 +23,7 @@ import junit.framework.TestCase;
import org.apache.commons.math.MathException;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.NullArgumentException;
/**
* @version $Revision$ $Date$
@ -41,12 +42,12 @@ public class DefaultTransformerTest extends TestCase {
/**
*
*/
public void testTransformNull(){
public void testTransformNull() throws Exception {
DefaultTransformer t = new DefaultTransformer();
try {
t.transform(null);
fail("Expection MathException");
} catch (MathException e) {
fail("Expecting NullArgumentException");
} catch (NullArgumentException e) {
// expected
}
}