You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/03/08 21:32:41 UTC
svn commit: r1079519 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/util/MathUtils.java
test/java/org/apache/commons/math/util/MathUtilsTest.java
Author: erans
Date: Tue Mar 8 20:32:40 2011
New Revision: 1079519
URL: http://svn.apache.org/viewvc?rev=1079519&view=rev
Log:
MATH-403
Added "CheckNotNull" methods.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1079519&r1=1079518&r2=1079519&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Tue Mar 8 20:32:40 2011
@@ -2274,4 +2274,32 @@ public final class MathUtils {
System.arraycopy(source, 0, output, 0, FastMath.min(len, source.length));
return output;
}
+
+ /**
+ * Checks that an object is not null.
+ *
+ * @param o Object to be checked.
+ * @param pattern Message pattern.
+ * @param args Arguments to replace the placeholders in {@code pattern}.
+ * @throws NullArgumentException if {@code o} is {@code null}.
+ */
+ public static void checkNotNull(Object o,
+ Localizable pattern,
+ Object ... args) {
+ if (o == null) {
+ throw new NullArgumentException(pattern, args);
+ }
+ }
+
+ /**
+ * Checks that an object is not null.
+ *
+ * @param o Object to be checked.
+ * @throws NullArgumentException if {@code o} is {@code null}.
+ */
+ public static void checkNotNull(Object o) {
+ if (o == null) {
+ throw new NullArgumentException();
+ }
+ }
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1079519&r1=1079518&r2=1079519&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Tue Mar 8 20:32:40 2011
@@ -28,6 +28,8 @@ import org.apache.commons.math.exception
import org.apache.commons.math.exception.MathArithmeticException;
import org.apache.commons.math.exception.MathRuntimeException;
import org.apache.commons.math.exception.NotFiniteNumberException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.random.RandomDataImpl;
/**
@@ -1640,4 +1642,22 @@ public final class MathUtilsTest extends
assertEquals(0, dest[i], 0);
}
}
+
+ public void testCheckNotNull1() {
+ try {
+ Object obj = null;
+ MathUtils.checkNotNull(obj);
+ } catch (NullArgumentException e) {
+ // Expected.
+ }
+ }
+
+ public void testCheckNotNull2() {
+ try {
+ double[] array = null;
+ MathUtils.checkNotNull(array, LocalizedFormats.INPUT_ARRAY, null);
+ } catch (NullArgumentException e) {
+ // Expected.
+ }
+ }
}