Package org.wildfly.common
Class Assert
- java.lang.Object
-
- org.wildfly.common.Assert
-
public final class Assert extends java.lang.Object
A set of assertions and checks.- Author:
- David M. Lloyd
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
assertFalse(boolean expr)
Assert that the given expression is alwaysfalse
.static <T> T
assertHoldsLock(T monitor)
Assert that the given monitor is held by the current thread.static <T> T
assertNotHoldsLock(T monitor)
Assert that the given monitor is not held by the current thread.static <T> T
assertNotNull(T value)
Assert that the value is notnull
.static boolean
assertTrue(boolean expr)
Assert that the given expression is alwaystrue
.static void
checkArrayBounds(byte[] array, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array.static void
checkArrayBounds(char[] array, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array.static void
checkArrayBounds(int[] array, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array.static void
checkArrayBounds(int arrayLength, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array length.static void
checkArrayBounds(long[] array, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array.static void
checkArrayBounds(java.lang.Object[] array, int offs, int len)
Check that the given offset and length fall completely within the bounds of the given array.static void
checkMaximumParameter(java.lang.String name, double max, double actual)
Check that the named parameter is less than or equal tomax
.static void
checkMaximumParameter(java.lang.String name, float max, float actual)
Check that the named parameter is less than or equal tomax
.static void
checkMaximumParameter(java.lang.String name, int max, int actual)
Check that the named parameter is less than or equal tomax
.static void
checkMaximumParameter(java.lang.String name, long max, long actual)
Check that the named parameter is less than or equal tomax
.static void
checkMinimumParameter(java.lang.String name, double min, double actual)
Check that the named parameter is greater than or equal tomin
.static void
checkMinimumParameter(java.lang.String name, float min, float actual)
Check that the named parameter is greater than or equal tomin
.static void
checkMinimumParameter(java.lang.String name, int min, int actual)
Check that the named parameter is greater than or equal tomin
.static void
checkMinimumParameter(java.lang.String name, long min, long actual)
Check that the named parameter is greater than or equal tomin
.static boolean[]
checkNotEmptyParam(java.lang.String name, boolean[] value)
Check that the named parameter is not empty.static byte[]
checkNotEmptyParam(java.lang.String name, byte[] value)
Check that the named parameter is not empty.static double[]
checkNotEmptyParam(java.lang.String name, double[] value)
Check that the named parameter is not empty.static float[]
checkNotEmptyParam(java.lang.String name, float[] value)
Check that the named parameter is not empty.static int[]
checkNotEmptyParam(java.lang.String name, int[] value)
Check that the named parameter is not empty.static long[]
checkNotEmptyParam(java.lang.String name, long[] value)
Check that the named parameter is not empty.static short[]
checkNotEmptyParam(java.lang.String name, short[] value)
Check that the named parameter is not empty.static java.lang.CharSequence
checkNotEmptyParam(java.lang.String name, java.lang.CharSequence value)
Check that the named parameter is not empty.static java.lang.String
checkNotEmptyParam(java.lang.String name, java.lang.String value)
Check that the named parameter is not empty.static <E,T extends java.util.Collection<E>>
TcheckNotEmptyParam(java.lang.String name, T value)
Check that the named parameter is not empty.static <T> T[]
checkNotEmptyParam(java.lang.String name, T[] value)
Check that the named parameter is not empty.static <T> T
checkNotNullArrayParam(java.lang.String name, int index, T value)
Check that a value within the named array parameter is notnull
.static <T> T
checkNotNullParam(java.lang.String name, T value)
Check that the named parameter is notnull
.static <T> T
checkNotNullParamWithNullPointerException(java.lang.String name, T value)
Check that the named parameter is notnull
, using aNullPointerException
as some specifications require.static java.lang.IllegalStateException
impossibleSwitchCase(int val)
Return an exception indicating that the current switch case was intended to be unreachable.static java.lang.IllegalStateException
impossibleSwitchCase(long val)
Return an exception indicating that the current switch case was intended to be unreachable.static java.lang.IllegalStateException
impossibleSwitchCase(java.lang.Object obj)
Return an exception indicating that the current switch case was intended to be unreachable.static java.lang.IllegalStateException
unreachableCode()
Return an exception indicating that the current code was intended to be unreachable.static java.lang.UnsupportedOperationException
unsupported()
Return an exception explaining that the caller's method is not supported.
-
-
-
Method Detail
-
checkNotNullParam
@NotNull public static <T> T checkNotNullParam(java.lang.String name, T value) throws java.lang.IllegalArgumentException
Check that the named parameter is notnull
. Use a standard exception message if it is.- Type Parameters:
T
- the value type- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value isnull
-
checkNotNullParamWithNullPointerException
@NotNull public static <T> T checkNotNullParamWithNullPointerException(java.lang.String name, T value) throws java.lang.NullPointerException
Check that the named parameter is notnull
, using aNullPointerException
as some specifications require. Use a standard exception message if it is.- Type Parameters:
T
- the value type- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.NullPointerException
- if the value isnull
-
checkNotNullArrayParam
@NotNull public static <T> T checkNotNullArrayParam(java.lang.String name, int index, T value) throws java.lang.IllegalArgumentException
Check that a value within the named array parameter is notnull
. Use a standard exception message if it is.- Type Parameters:
T
- the element value type- Parameters:
name
- the parameter nameindex
- the array indexvalue
- the array element value- Returns:
- the array element value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value isnull
-
checkNotEmptyParam
@NotNull public static java.lang.String checkNotEmptyParam(java.lang.String name, java.lang.String value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static java.lang.CharSequence checkNotEmptyParam(java.lang.String name, java.lang.CharSequence value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static <E,T extends java.util.Collection<E>> T checkNotEmptyParam(java.lang.String name, T value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static <K,V,T extends java.util.Map<K,V>> T checkNotEmptyParam(java.lang.String name, T value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static <T> T[] checkNotEmptyParam(java.lang.String name, T[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static boolean[] checkNotEmptyParam(java.lang.String name, boolean[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static byte[] checkNotEmptyParam(java.lang.String name, byte[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static short[] checkNotEmptyParam(java.lang.String name, short[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static int[] checkNotEmptyParam(java.lang.String name, int[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static long[] checkNotEmptyParam(java.lang.String name, long[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static float[] checkNotEmptyParam(java.lang.String name, float[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkNotEmptyParam
@NotNull public static double[] checkNotEmptyParam(java.lang.String name, double[] value)
Check that the named parameter is not empty. Use a standard exception message if it is.- Parameters:
name
- the parameter namevalue
- the parameter value- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the value is empty
-
checkMinimumParameter
public static void checkMinimumParameter(java.lang.String name, int min, int actual) throws java.lang.IllegalArgumentException
Check that the named parameter is greater than or equal tomin
.- Parameters:
name
- the parameter namemin
- the minimum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is less than the minimum value
-
checkMinimumParameter
public static void checkMinimumParameter(java.lang.String name, long min, long actual) throws java.lang.IllegalArgumentException
Check that the named parameter is greater than or equal tomin
.- Parameters:
name
- the parameter namemin
- the minimum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is less than the minimum value
-
checkMinimumParameter
public static void checkMinimumParameter(java.lang.String name, float min, float actual) throws java.lang.IllegalArgumentException
Check that the named parameter is greater than or equal tomin
.- Parameters:
name
- the parameter namemin
- the minimum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is less than the minimum value
-
checkMinimumParameter
public static void checkMinimumParameter(java.lang.String name, double min, double actual) throws java.lang.IllegalArgumentException
Check that the named parameter is greater than or equal tomin
.- Parameters:
name
- the parameter namemin
- the minimum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is less than the minimum value
-
checkMaximumParameter
public static void checkMaximumParameter(java.lang.String name, int max, int actual) throws java.lang.IllegalArgumentException
Check that the named parameter is less than or equal tomax
.- Parameters:
name
- the parameter namemax
- the maximum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is greater than the minimum value
-
checkMaximumParameter
public static void checkMaximumParameter(java.lang.String name, long max, long actual) throws java.lang.IllegalArgumentException
Check that the named parameter is less than or equal tomax
.- Parameters:
name
- the parameter namemax
- the maximum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is greater than the minimum value
-
checkMaximumParameter
public static void checkMaximumParameter(java.lang.String name, float max, float actual) throws java.lang.IllegalArgumentException
Check that the named parameter is less than or equal tomax
.- Parameters:
name
- the parameter namemax
- the maximum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is greater than the minimum value
-
checkMaximumParameter
public static void checkMaximumParameter(java.lang.String name, double max, double actual) throws java.lang.IllegalArgumentException
Check that the named parameter is less than or equal tomax
.- Parameters:
name
- the parameter namemax
- the maximum valueactual
- the actual parameter value- Throws:
java.lang.IllegalArgumentException
- if the actual value is greater than the minimum value
-
checkArrayBounds
public static void checkArrayBounds(java.lang.Object[] array, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array.- Parameters:
array
- the array to checkoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
checkArrayBounds
public static void checkArrayBounds(byte[] array, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array.- Parameters:
array
- the array to checkoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
checkArrayBounds
public static void checkArrayBounds(char[] array, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array.- Parameters:
array
- the array to checkoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
checkArrayBounds
public static void checkArrayBounds(int[] array, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array.- Parameters:
array
- the array to checkoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
checkArrayBounds
public static void checkArrayBounds(long[] array, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array.- Parameters:
array
- the array to checkoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
checkArrayBounds
public static void checkArrayBounds(int arrayLength, int offs, int len) throws java.lang.ArrayIndexOutOfBoundsException
Check that the given offset and length fall completely within the bounds of the given array length.- Parameters:
arrayLength
- the array length to check againstoffs
- the array offsetlen
- the array length- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the range of the offset and length do not fall within the array bounds
-
assertNotNull
@NotNull public static <T> T assertNotNull(T value)
Assert that the value is notnull
. Use a standard assertion failure message if it is. Only runs ifassert
is enabled.- Type Parameters:
T
- the value type- Parameters:
value
- the not-null
value- Returns:
- the value that was passed in
-
assertHoldsLock
@NotNull public static <T> T assertHoldsLock(@NotNull T monitor)
Assert that the given monitor is held by the current thread. Use a standard assertion failure message if it is not. Only runs ifassert
is enabled.- Type Parameters:
T
- the monitor's type- Parameters:
monitor
- the monitor object- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the monitor isnull
-
assertNotHoldsLock
@NotNull public static <T> T assertNotHoldsLock(@NotNull T monitor)
Assert that the given monitor is not held by the current thread. Use a standard assertion failure message if it is. Only runs ifassert
is enabled.- Type Parameters:
T
- the monitor's type- Parameters:
monitor
- the monitor object- Returns:
- the value that was passed in
- Throws:
java.lang.IllegalArgumentException
- if the monitor isnull
-
assertTrue
public static boolean assertTrue(boolean expr)
Assert that the given expression is alwaystrue
.- Parameters:
expr
- the boolean expression- Returns:
- the boolean expression
-
assertFalse
public static boolean assertFalse(boolean expr)
Assert that the given expression is alwaysfalse
.- Parameters:
expr
- the boolean expression- Returns:
- the boolean expression
-
unreachableCode
public static java.lang.IllegalStateException unreachableCode()
Return an exception indicating that the current code was intended to be unreachable.- Returns:
- the exception which may be immediately thrown
-
impossibleSwitchCase
@NotNull public static java.lang.IllegalStateException impossibleSwitchCase(@NotNull java.lang.Object obj)
Return an exception indicating that the current switch case was intended to be unreachable.- Parameters:
obj
- the switch case value- Returns:
- the exception which may be immediately thrown
-
impossibleSwitchCase
@NotNull public static java.lang.IllegalStateException impossibleSwitchCase(int val)
Return an exception indicating that the current switch case was intended to be unreachable.- Parameters:
val
- the switch case value- Returns:
- the exception which may be immediately thrown
-
impossibleSwitchCase
@NotNull public static java.lang.IllegalStateException impossibleSwitchCase(long val)
Return an exception indicating that the current switch case was intended to be unreachable.- Parameters:
val
- the switch case value- Returns:
- the exception which may be immediately thrown
-
unsupported
@NotNull public static java.lang.UnsupportedOperationException unsupported()
Return an exception explaining that the caller's method is not supported.- Returns:
- the exception
-
-