MATH-403
Added "CheckNotNull" methods. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1079519 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e3fe16402e
commit
9e79920ec5
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ import org.apache.commons.math.exception.MathIllegalArgumentException;
|
|||
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 TestCase {
|
|||
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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue