[MATH-869] NullArgumentException now extends NullPointerException.

This commit is contained in:
Thomas Neidhart 2015-02-25 22:25:47 +01:00
parent c22e7fb6f9
commit 35b688b7ec
13 changed files with 98 additions and 55 deletions

View File

@ -54,6 +54,10 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>
<release version="4.0" date="XXXX-XX-XX" description="">
<action dev="tn" type="update" issue="MATH-869">
"NullArgumentException" extends now "java.lang.NullPointerException"
instead of "MathIllegalArgumentException".
</action>
<action dev="tn" type="update" issue="MATH-839" due-to="Gilles Sadowski">
Renamed "cumulativeProbability(double, double)" to "probability(double, double)"
in "IntegerDistribution" and "RealDistribution".

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.math4.exception;
import org.apache.commons.math4.exception.util.ExceptionContext;
import org.apache.commons.math4.exception.util.ExceptionContextProvider;
import org.apache.commons.math4.exception.util.Localizable;
import org.apache.commons.math4.exception.util.LocalizedFormats;
@ -26,12 +28,20 @@ import org.apache.commons.math4.exception.util.LocalizedFormats;
* argument") and so does not extend the standard {@code NullPointerException}.
* Propagation of {@code NullPointerException} from within Commons-Math is
* construed to be a bug.
* <p>
* Note: from 4.0 onwards, this class extends {@link NullPointerException} instead
* of {@link MathIllegalArgumentException}.
*
* @since 2.2
*/
public class NullArgumentException extends MathIllegalArgumentException {
public class NullArgumentException extends NullPointerException
implements ExceptionContextProvider {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
private static final long serialVersionUID = 20150225L;
/** Context. */
private final ExceptionContext context;
/**
* Default constructor.
@ -46,6 +56,28 @@ public class NullArgumentException extends MathIllegalArgumentException {
*/
public NullArgumentException(Localizable pattern,
Object ... arguments) {
super(pattern, arguments);
context = new ExceptionContext(this);
context.addMessage(pattern, arguments);
}
/**
* {@inheritDoc}
* @since 4.0
*/
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
}
}

View File

@ -23,6 +23,7 @@ import org.apache.commons.math4.analysis.function.Sin;
import org.apache.commons.math4.analysis.solvers.UnivariateSolverUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NoBracketingException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -33,7 +34,7 @@ public class UnivariateSolverUtilsTest {
protected UnivariateFunction sin = new Sin();
@Test(expected=MathIllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testSolveNull() {
UnivariateSolverUtils.solve(null, 0.0, 4.0);
}
@ -60,7 +61,7 @@ public class UnivariateSolverUtilsTest {
Assert.assertEquals(FastMath.PI, x, 1.0e-4);
}
@Test(expected=MathIllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testSolveAccuracyNull() {
double accuracy = 1.0e-6;
UnivariateSolverUtils.solve(null, 0.0, 4.0, accuracy);
@ -144,7 +145,7 @@ public class UnivariateSolverUtilsTest {
Assert.assertTrue(sin.value(result[1]) > 0);
}
@Test(expected=MathIllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testNullFunction() {
UnivariateSolverUtils.bracket(null, 1.5, 0, 2.0);
}

View File

@ -19,7 +19,7 @@ package org.apache.commons.math4.fraction;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.ConvergenceException;
import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.fraction.Fraction;
import org.apache.commons.math4.fraction.FractionConversionException;
import org.apache.commons.math4.util.FastMath;
@ -348,8 +348,8 @@ public class FractionTest {
try {
f.add(null);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {}
Assert.fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {}
// if this fraction is added naively, it will overflow.
// check that it doesn't.
@ -445,8 +445,8 @@ public class FractionTest {
try {
f.divide(null);
Assert.fail("MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {}
Assert.fail("NullArgumentException");
} catch (NullArgumentException ex) {}
try {
f1 = new Fraction(1, Integer.MAX_VALUE);
@ -484,8 +484,8 @@ public class FractionTest {
try {
f.multiply(null);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {}
Assert.fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {}
f1 = new Fraction(6, 35);
f = f1.multiply(15);
@ -506,8 +506,8 @@ public class FractionTest {
Fraction f = new Fraction(1,1);
try {
f.subtract(null);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {}
Assert.fail("expecting NullArgumentException");
} catch (NullArgumentException ex) {}
// if this fraction is subtracted naively, it will overflow.
// check that it doesn't.

View File

@ -19,6 +19,7 @@ package org.apache.commons.math4.stat;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.stat.StatUtils;
import org.apache.commons.math4.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math4.util.FastMath;
@ -122,14 +123,14 @@ public final class StatUtilsTest {
try {
StatUtils.sumSq(x);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
try {
StatUtils.sumSq(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -157,14 +158,14 @@ public final class StatUtilsTest {
try {
StatUtils.product(x);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
try {
StatUtils.product(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -192,14 +193,14 @@ public final class StatUtilsTest {
try {
StatUtils.sumLog(x);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
try {
StatUtils.sumLog(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -226,7 +227,7 @@ public final class StatUtilsTest {
try {
StatUtils.mean(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -250,7 +251,7 @@ public final class StatUtilsTest {
try {
StatUtils.variance(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -278,7 +279,7 @@ public final class StatUtilsTest {
try {
StatUtils.variance(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -307,7 +308,7 @@ public final class StatUtilsTest {
try {
StatUtils.max(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -347,7 +348,7 @@ public final class StatUtilsTest {
try {
StatUtils.min(x, 0, 4);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -388,14 +389,14 @@ public final class StatUtilsTest {
try {
StatUtils.percentile(x, .25);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
try {
StatUtils.percentile(x, 0, 4, 0.25);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException ex) {
} catch (NullArgumentException ex) {
// success
}
@ -452,8 +453,8 @@ public final class StatUtilsTest {
double[] test = null;
try {
StatUtils.geometricMean(test);
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
test = new double[] {2, 4, 6, 8};
@ -547,8 +548,8 @@ public final class StatUtilsTest {
final double[] nullArray = null;
try {
StatUtils.mode(nullArray);
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// Expected
}
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.math4.stat.descriptive;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.stat.descriptive.moment.Mean;
import org.junit.Assert;
import org.junit.Test;
@ -76,14 +77,14 @@ public class AbstractUnivariateStatisticTest {
}
try {
testStatistic.test(nullArray, 0, 1); // null array
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
try {
testStatistic.test(testArray, nullArray, 0, 1); // null weights array
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
try {

View File

@ -18,7 +18,7 @@
package org.apache.commons.math4.stat.descriptive.moment;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.stat.StatUtils;
import org.apache.commons.math4.stat.descriptive.moment.SemiVariance;
import org.junit.Assert;
@ -34,14 +34,14 @@ public class SemiVarianceTest {
try {
sv.evaluate(nothing);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException iae) {
} catch (NullArgumentException nae) {
}
try {
sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
sv.evaluate(nothing);
Assert.fail("null is not a valid data array.");
} catch (MathIllegalArgumentException iae) {
} catch (NullArgumentException nae) {
}
nothing = new double[] {};
Assert.assertTrue(Double.isNaN(sv.evaluate(nothing)));

View File

@ -28,6 +28,7 @@ import org.apache.commons.math4.distribution.LogNormalDistribution;
import org.apache.commons.math4.distribution.NormalDistribution;
import org.apache.commons.math4.distribution.RealDistribution;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.OutOfRangeException;
import org.apache.commons.math4.random.RandomGenerator;
import org.apache.commons.math4.random.Well19937c;
@ -439,7 +440,7 @@ public class PSquarePercentileTest extends
1.0);// changed the accuracy to 1 instead of tolerance
}
@Test(expected = MathIllegalArgumentException.class)
@Test(expected = NullArgumentException.class)
public void testNull() {
PSquarePercentile percentile = new PSquarePercentile(50d);
double[] nullArray = null;

View File

@ -171,8 +171,8 @@ public class PercentileTest extends UnivariateStatisticAbstractTest{
final double[] emptyArray = new double[] {};
try {
percentile.evaluate(nullArray);
Assert.fail("Expecting MathIllegalArgumentException for null array");
} catch (final MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException for null array");
} catch (final NullArgumentException ex) {
// expected
}
Assert.assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));
@ -364,9 +364,9 @@ public class PercentileTest extends UnivariateStatisticAbstractTest{
final UnivariateStatistic percentile = getUnivariateStatistic();
try {
percentile.evaluate(nullArray);
Assert.fail("Expecting MathIllegalArgumentException "
Assert.fail("Expecting NullArgumentException "
+ "for null array");
} catch (final MathIllegalArgumentException ex) {
} catch (final NullArgumentException ex) {
// expected
}
Assert.assertTrue(Double.isNaN(percentile.evaluate(emptyArray)));

View File

@ -20,6 +20,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.linear.MatrixUtils;
import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.linear.RealVector;
@ -77,12 +78,12 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
super.setUp();
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void cannotAddXSampleData() {
createRegression().newSampleData(new double[]{}, null, null);
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void cannotAddNullYSampleData() {
createRegression().newSampleData(null, new double[][]{}, null);
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math4.stat.regression;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.linear.RealMatrix;
import org.apache.commons.math4.linear.RealVector;
import org.apache.commons.math4.stat.regression.AbstractMultipleLinearRegression;
@ -104,7 +105,7 @@ public abstract class MultipleLinearRegressionAbstractTest {
Assert.assertEquals(flatY, regression.getY());
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testNewSampleNullData() {
double[] data = null;
createRegression().newSampleData(data, 2, 3);
@ -122,12 +123,12 @@ public abstract class MultipleLinearRegressionAbstractTest {
createRegression().newSampleData(data, 1, 3);
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testXSampleDataNull() {
createRegression().newXSampleData(null);
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testYSampleDataNull() {
createRegression().newYSampleData(null);
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.math4.stat.regression;
import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.linear.Array2DRowRealMatrix;
import org.apache.commons.math4.linear.DefaultRealMatrixChangingVisitor;
import org.apache.commons.math4.linear.MatrixUtils;
@ -500,12 +501,12 @@ public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
Assert.assertEquals(combinedY, regression.getY());
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testNewSampleDataYNull() {
createRegression().newSampleData(null, new double[][] {});
}
@Test(expected=IllegalArgumentException.class)
@Test(expected=NullArgumentException.class)
public void testNewSampleDataXNull() {
createRegression().newSampleData(new double[] {}, null);
}

View File

@ -1128,14 +1128,14 @@ public class MathArraysTest {
}
try {
MathArrays.verifyValues(nullArray, 0, 1); // null array
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
try {
MathArrays.verifyValues(testArray, nullArray, 0, 1); // null weights array
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
try {