Package dev.nm.misc
Class ArgumentAssertion
- java.lang.Object
-
- dev.nm.misc.ArgumentAssertion
-
public final class ArgumentAssertion extends Object
Utility class for checking numerical arguments. Arguments of same primitive type ofdouble
,int
,long
,short
,float
andbyte
can be used as input arguments. If input arguments violate the assertion, anIllegalArgumentException
with appropriate error message will be thrown. For example:ArgumentAssertion.assertRangeLeftOpen(1.5, 1.0, 2.0); // double ArgumentAssertion.assertRangeLeftOpen(2, 1, 3); // int ArgumentAssertion.assertRangeLeftOpen(1.5f, 1.0f, 2.0f); // float
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Number & Comparable<? super T>>
voidassertEqual(T x, T bound, String variableName)
static <T extends Number & Comparable<? super T>>
voidassertEqual(T x1, T x2, String variableName1, String variableName2)
static void
assertFalse(boolean condition, String errorMessage, Object... args)
Check if an argumentcondition
is false.static <T extends Number & Comparable<? super T>>
voidassertGreaterThan(T x, T bound, String variableName)
static <T extends Number & Comparable<? super T>>
voidassertLessThan(T x, T bound, String variableName)
static <T extends Number & Comparable<? super T>>
voidassertNegative(T x, String variableName)
Test ifNumber
x
is negative.static <T extends Number & Comparable<? super T>>
voidassertNonNegative(T x, String variableName)
Test ifNumber
x
is non-negative.static <T extends Number & Comparable<? super T>>
voidassertNonPositive(T x, String variableName)
Test ifNumber
x
is non-positive.static void
assertNormalDouble(double d, String variableName)
Check if an argument is a normaldouble
value (that is, NOTDouble.NaN
nor infinity).static void
assertNormalFloat(float f, String variableName)
Check if an argument is a normalfloat
value (that is, NOTFloat.NaN
nor infinity).static <T extends Number & Comparable<? super T>>
voidassertNotGreaterThan(T x, T bound, String variableName)
static void
assertNotInfinity(double d, String variableName)
Check if an argument is NOT aDouble.POSITIVE_INFINITY
norDouble.NEGATIVE_INFINITY
.static void
assertNotInfinity(float f, String variableName)
Check if an argument is NOT aFloat.POSITIVE_INFINITY
norFloat.NEGATIVE_INFINITY
.static <T extends Number & Comparable<? super T>>
voidassertNotLessThan(T x, T bound, String variableName)
static void
assertNotNaN(double d, String variableName)
Check if an argument is NOT aDouble.NaN
.static void
assertNotNaN(float f, String variableName)
Check if an argument is NOT aFloat.NaN
.static void
assertNotNull(Object obj, String variableName)
Check ifobj
is notnull
.static void
assertNull(Object obj, String variableName)
Check ifobj
isnull
.static <T extends Number & Comparable<? super T>>
voidassertPositive(T x, String variableName)
Test ifNumber
x
is positive.static <T extends Number & Comparable<? super T>>
voidassertRange(T x, T low, T high, String variableName)
static <T extends Number & Comparable<? super T>>
voidassertRangeLeftOpen(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range (low
,high
] (left exclusive, right inclusive).static <T extends Number & Comparable<? super T>>
voidassertRangeOpen(T x, T low, T high, String variableName)
static <T extends Number & Comparable<? super T>>
voidassertRangeRightOpen(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range [low
,high
) (left inclusive, right exclusive).static void
assertTrue(boolean condition, String errorMessage, Object... args)
Check if an argumentcondition
is true.
-
-
-
Method Detail
-
assertRange
public static <T extends Number & Comparable<? super T>> void assertRange(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range [low
,high
] (both inclusive). ThrowIllegalArgumentException
if not.- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testlow
- lower bound of the range, inclusivehigh
- upper bound of the range, inclusivevariableName
- the display name of variablex
-
assertRangeOpen
public static <T extends Number & Comparable<? super T>> void assertRangeOpen(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range (low
,high
) (both exclusive). ThrowIllegalArgumentException
if not.- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testlow
- lower bound of the range, exclusivehigh
- upper bound of the range, exclusivevariableName
- the display name of variablex
-
assertRangeLeftOpen
public static <T extends Number & Comparable<? super T>> void assertRangeLeftOpen(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range (low
,high
] (left exclusive, right inclusive). ThrowIllegalArgumentException
if not.- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testlow
- lower bound of the range, exclusivehigh
- upper bound of the range, inclusivevariableName
- the display name of variablex
-
assertRangeRightOpen
public static <T extends Number & Comparable<? super T>> void assertRangeRightOpen(T x, T low, T high, String variableName)
Test whether the specifiedNumber
occurs within the range [low
,high
) (left inclusive, right exclusive). ThrowIllegalArgumentException
if not.- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testlow
- lower bound of the range, inclusivehigh
- upper bound of the range, exclusivevariableName
- the display name of variablex
-
assertGreaterThan
public static <T extends Number & Comparable<? super T>> void assertGreaterThan(T x, T bound, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testbound
- the number to see ifx
is greater thanvariableName
- the display name of variablex
-
assertNotLessThan
public static <T extends Number & Comparable<? super T>> void assertNotLessThan(T x, T bound, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testbound
- the number to see ifx
is not less thanvariableName
- the display name of variablex
-
assertLessThan
public static <T extends Number & Comparable<? super T>> void assertLessThan(T x, T bound, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testbound
- the number to see ifx
is less thanvariableName
- the display name of variablex
-
assertNotGreaterThan
public static <T extends Number & Comparable<? super T>> void assertNotGreaterThan(T x, T bound, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testbound
- the number to see ifx
is not greater thanvariableName
- the display name of variablex
-
assertPositive
public static <T extends Number & Comparable<? super T>> void assertPositive(T x, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testvariableName
- the display name of variablex
-
assertNonNegative
public static <T extends Number & Comparable<? super T>> void assertNonNegative(T x, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testvariableName
- the display name of variablex
-
assertNegative
public static <T extends Number & Comparable<? super T>> void assertNegative(T x, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testvariableName
- the display name of variablex
-
assertNonPositive
public static <T extends Number & Comparable<? super T>> void assertNonPositive(T x, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testvariableName
- the display name of variablex
-
assertEqual
public static <T extends Number & Comparable<? super T>> void assertEqual(T x, T bound, String variableName)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x
- the number to testbound
- the number to see ifx
is equal tovariableName
- the display name of variablex
-
assertEqual
public static <T extends Number & Comparable<? super T>> void assertEqual(T x1, T x2, String variableName1, String variableName2)
- Type Parameters:
T
- subclass ofNumber
which implementsComparable
- Parameters:
x1
- the numberx1
x2
- the numberx2
variableName1
- the display name of variablex1
variableName2
- the display name of variablex2
-
assertTrue
public static void assertTrue(boolean condition, String errorMessage, Object... args)
Check if an argumentcondition
is true. ThrowIllegalArgumentException
if it is false.- Parameters:
condition
- the argument condition to be checkederrorMessage
- the error message if the condition is not true (format string can be used withargs
, seeString.format(java.lang.String, java.lang.Object[])
)args
- the arguments for the error message
-
assertFalse
public static void assertFalse(boolean condition, String errorMessage, Object... args)
Check if an argumentcondition
is false. ThrowIllegalArgumentException
if it is true.- Parameters:
condition
- the argument condition to be checkederrorMessage
- the error message if the condition is not false (format string can be used withargs
, seeString.format(java.lang.String, java.lang.Object[])
)args
- the arguments for the error message
-
assertNotNull
public static void assertNotNull(Object obj, String variableName)
- Parameters:
obj
- variable to be checkedvariableName
- the display name of variableobj
-
assertNull
public static void assertNull(Object obj, String variableName)
- Parameters:
obj
- variable to be checkedvariableName
- the display name of variableobj
-
assertNotNaN
public static void assertNotNaN(double d, String variableName)
- Parameters:
d
- thedouble
variablevariableName
- the display name of the variable
-
assertNotInfinity
public static void assertNotInfinity(double d, String variableName)
Check if an argument is NOT aDouble.POSITIVE_INFINITY
norDouble.NEGATIVE_INFINITY
. ThrowIllegalArgumentException
if it holds an infinite value.- Parameters:
d
- thedouble
variablevariableName
- the display name of the variable
-
assertNormalDouble
public static void assertNormalDouble(double d, String variableName)
Check if an argument is a normaldouble
value (that is, NOTDouble.NaN
nor infinity). ThrowIllegalArgumentException
if it is not a normaldouble
value.- Parameters:
d
- thedouble
variablevariableName
- the display name of the variable
-
assertNotNaN
public static void assertNotNaN(float f, String variableName)
- Parameters:
f
- thefloat
variablevariableName
- the display name of the variable
-
assertNotInfinity
public static void assertNotInfinity(float f, String variableName)
Check if an argument is NOT aFloat.POSITIVE_INFINITY
norFloat.NEGATIVE_INFINITY
. ThrowIllegalArgumentException
if it holds an infinite value.- Parameters:
f
- thefloat
variablevariableName
- the display name of the variable
-
assertNormalFloat
public static void assertNormalFloat(float f, String variableName)
Check if an argument is a normalfloat
value (that is, NOTFloat.NaN
nor infinity). ThrowIllegalArgumentException
if it is not a normalfloat
value.- Parameters:
f
- thefloat
variablevariableName
- the display name of the variable
-
-