diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9b4744ec3..ee73fca8a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,10 @@ If the output is not quite correct, check for invisible trailing spaces! + + "NullArgumentException" extends now "java.lang.NullPointerException" + instead of "MathIllegalArgumentException". + Renamed "cumulativeProbability(double, double)" to "probability(double, double)" in "IntegerDistribution" and "RealDistribution". diff --git a/src/main/java/org/apache/commons/math4/exception/NullArgumentException.java b/src/main/java/org/apache/commons/math4/exception/NullArgumentException.java index 7b73be71b..5577042a7 100644 --- a/src/main/java/org/apache/commons/math4/exception/NullArgumentException.java +++ b/src/main/java/org/apache/commons/math4/exception/NullArgumentException.java @@ -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. + *

+ * 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(); + } + } diff --git a/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java b/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java index fba50e33b..f2471b7c7 100644 --- a/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/solvers/UnivariateSolverUtilsTest.java @@ -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); } diff --git a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java b/src/test/java/org/apache/commons/math4/fraction/FractionTest.java index 174fa090b..aca7d05a4 100644 --- a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java +++ b/src/test/java/org/apache/commons/math4/fraction/FractionTest.java @@ -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. diff --git a/src/test/java/org/apache/commons/math4/stat/StatUtilsTest.java b/src/test/java/org/apache/commons/math4/stat/StatUtilsTest.java index 9837b8539..2528f3c79 100644 --- a/src/test/java/org/apache/commons/math4/stat/StatUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/stat/StatUtilsTest.java @@ -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 } } diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatisticTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatisticTest.java index dca0ece27..b455f5b7b 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatisticTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/AbstractUnivariateStatisticTest.java @@ -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 { diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/moment/SemiVarianceTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/moment/SemiVarianceTest.java index e9dc85a36..c964a78b7 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/moment/SemiVarianceTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/moment/SemiVarianceTest.java @@ -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))); diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java index 78969fb4a..8751b00b9 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java @@ -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; diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java index bd67c5ae9..8b2107b49 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java @@ -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))); diff --git a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java index 29dbe07c3..a2f5f62bc 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java @@ -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); } diff --git a/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java b/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java index 8e05600ab..1fc839bc2 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/MultipleLinearRegressionAbstractTest.java @@ -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); } diff --git a/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java index f5025b0cb..d383d0fcd 100644 --- a/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java +++ b/src/test/java/org/apache/commons/math4/stat/regression/OLSMultipleLinearRegressionTest.java @@ -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); } diff --git a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java index 71d75b9b6..16e6a529f 100644 --- a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java +++ b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java @@ -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 {