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,floatandbytecan be used as input arguments. If input arguments violate the assertion, anIllegalArgumentExceptionwith 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 voidassertFalse(boolean condition, String errorMessage, Object... args)Check if an argumentconditionis 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 ifNumberxis negative.static <T extends Number & Comparable<? super T>>
voidassertNonNegative(T x, String variableName)Test ifNumberxis non-negative.static <T extends Number & Comparable<? super T>>
voidassertNonPositive(T x, String variableName)Test ifNumberxis non-positive.static voidassertNormalDouble(double d, String variableName)Check if an argument is a normaldoublevalue (that is, NOTDouble.NaNnor infinity).static voidassertNormalFloat(float f, String variableName)Check if an argument is a normalfloatvalue (that is, NOTFloat.NaNnor infinity).static <T extends Number & Comparable<? super T>>
voidassertNotGreaterThan(T x, T bound, String variableName)static voidassertNotInfinity(double d, String variableName)Check if an argument is NOT aDouble.POSITIVE_INFINITYnorDouble.NEGATIVE_INFINITY.static voidassertNotInfinity(float f, String variableName)Check if an argument is NOT aFloat.POSITIVE_INFINITYnorFloat.NEGATIVE_INFINITY.static <T extends Number & Comparable<? super T>>
voidassertNotLessThan(T x, T bound, String variableName)static voidassertNotNaN(double d, String variableName)Check if an argument is NOT aDouble.NaN.static voidassertNotNaN(float f, String variableName)Check if an argument is NOT aFloat.NaN.static voidassertNotNull(Object obj, String variableName)Check ifobjis notnull.static voidassertNull(Object obj, String variableName)Check ifobjisnull.static <T extends Number & Comparable<? super T>>
voidassertPositive(T x, String variableName)Test ifNumberxis 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 specifiedNumberoccurs 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 specifiedNumberoccurs within the range [low,high) (left inclusive, right exclusive).static voidassertTrue(boolean condition, String errorMessage, Object... args)Check if an argumentconditionis 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 specifiedNumberoccurs within the range [low,high] (both inclusive). ThrowIllegalArgumentExceptionif not.- Type Parameters:
T- subclass ofNumberwhich 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 specifiedNumberoccurs within the range (low,high) (both exclusive). ThrowIllegalArgumentExceptionif not.- Type Parameters:
T- subclass ofNumberwhich 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 specifiedNumberoccurs within the range (low,high] (left exclusive, right inclusive). ThrowIllegalArgumentExceptionif not.- Type Parameters:
T- subclass ofNumberwhich 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 specifiedNumberoccurs within the range [low,high) (left inclusive, right exclusive). ThrowIllegalArgumentExceptionif not.- Type Parameters:
T- subclass ofNumberwhich 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 ofNumberwhich implementsComparable- Parameters:
x- the number to testbound- the number to see ifxis 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 ofNumberwhich implementsComparable- Parameters:
x- the number to testbound- the number to see ifxis 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 ofNumberwhich implementsComparable- Parameters:
x- the number to testbound- the number to see ifxis 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 ofNumberwhich implementsComparable- Parameters:
x- the number to testbound- the number to see ifxis 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 ofNumberwhich 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 ofNumberwhich 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 ofNumberwhich 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 ofNumberwhich 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 ofNumberwhich implementsComparable- Parameters:
x- the number to testbound- the number to see ifxis 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 ofNumberwhich implementsComparable- Parameters:
x1- the numberx1x2- the numberx2variableName1- the display name of variablex1variableName2- the display name of variablex2
-
assertTrue
public static void assertTrue(boolean condition, String errorMessage, Object... args)Check if an argumentconditionis true. ThrowIllegalArgumentExceptionif 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 argumentconditionis false. ThrowIllegalArgumentExceptionif 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- thedoublevariablevariableName- the display name of the variable
-
assertNotInfinity
public static void assertNotInfinity(double d, String variableName)Check if an argument is NOT aDouble.POSITIVE_INFINITYnorDouble.NEGATIVE_INFINITY. ThrowIllegalArgumentExceptionif it holds an infinite value.- Parameters:
d- thedoublevariablevariableName- the display name of the variable
-
assertNormalDouble
public static void assertNormalDouble(double d, String variableName)Check if an argument is a normaldoublevalue (that is, NOTDouble.NaNnor infinity). ThrowIllegalArgumentExceptionif it is not a normaldoublevalue.- Parameters:
d- thedoublevariablevariableName- the display name of the variable
-
assertNotNaN
public static void assertNotNaN(float f, String variableName)- Parameters:
f- thefloatvariablevariableName- the display name of the variable
-
assertNotInfinity
public static void assertNotInfinity(float f, String variableName)Check if an argument is NOT aFloat.POSITIVE_INFINITYnorFloat.NEGATIVE_INFINITY. ThrowIllegalArgumentExceptionif it holds an infinite value.- Parameters:
f- thefloatvariablevariableName- the display name of the variable
-
assertNormalFloat
public static void assertNormalFloat(float f, String variableName)Check if an argument is a normalfloatvalue (that is, NOTFloat.NaNnor infinity). ThrowIllegalArgumentExceptionif it is not a normalfloatvalue.- Parameters:
f- thefloatvariablevariableName- the display name of the variable
-
-