public final class Assert extends Object
Modifier and Type | Method and Description |
---|---|
static void |
doesNotContain(String textToSearch,
String substring)
Assert that the given text does not contain the given substring.
|
static void |
doesNotContain(String textToSearch,
String substring,
String message)
Assert that the given text does not contain the given substring.
|
static <T extends Comparable<T>> |
eq(T value,
T requirement,
String msg)
Asserts that a specified
value is equal to the given requirement , throwing
an IllegalArgumentException with the given message if not. |
static <T extends Comparable<T>> |
gt(T value,
T requirement,
String msg)
Asserts that a specified
value is greater than the given requirement , throwing
an IllegalArgumentException with the given message if not. |
static void |
hasLength(String text)
Assert that the given String is not empty; that is,
it must not be
null and not the empty String. |
static void |
hasLength(String text,
String message)
Assert that the given String is not empty; that is,
it must not be
null and not the empty String. |
static void |
hasText(String text)
Assert that the given String has valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static <T extends CharSequence> |
hasText(T text,
String message)
Assert that the given String has valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static void |
isAssignable(Class superType,
Class subType)
Assert that
superType.isAssignableFrom(subType) is true . |
static void |
isAssignable(Class superType,
Class subType,
String message)
Assert that
superType.isAssignableFrom(subType) is true . |
static <T> T |
isInstanceOf(Class<T> clazz,
Object obj)
Assert that the provided object is an instance of the provided class.
|
static <T> T |
isInstanceOf(Class<T> type,
Object obj,
String message)
Assert that the provided object is an instance of the provided class.
|
static void |
isNull(Object object)
Assert that an object is
null . |
static void |
isNull(Object object,
String message)
Assert that an object is
null . |
static void |
isTrue(boolean expression)
Assert a boolean expression, throwing
IllegalArgumentException
if the test result is false . |
static void |
isTrue(boolean expression,
String message)
Assert a boolean expression, throwing
IllegalArgumentException
if the test result is false . |
static <T extends Comparable<T>> |
lte(T value,
T requirement,
String msg)
Asserts that a specified
value is less than or equal to the given requirement , throwing
an IllegalArgumentException with the given message if not. |
static void |
noNullElements(Object[] array)
Assert that an array has no null elements.
|
static void |
noNullElements(Object[] array,
String message)
Assert that an array has no null elements.
|
static byte[] |
notEmpty(byte[] array,
String msg)
Assert that the specified byte array is not null and has at least one byte element.
|
static char[] |
notEmpty(char[] chars,
String msg)
Assert that the specified character array is not null and has at least one byte element.
|
static void |
notEmpty(Collection<?> collection)
Assert that a collection has elements; that is, it must not be
null and must have at least one element. |
static void |
notEmpty(Map map)
Assert that a Map has entries; that is, it must not be
null
and must have at least one entry. |
static void |
notEmpty(Object[] array)
Assert that an array has elements; that is, it must not be
null and must have at least one element. |
static Object[] |
notEmpty(Object[] array,
String message)
Assert that an array has elements; that is, it must not be
null and must have at least one element. |
static <T extends Collection<?>> |
notEmpty(T collection,
String message)
Assert that a collection has elements; that is, it must not be
null and must have at least one element. |
static <T extends Map<?,?>> |
notEmpty(T map,
String message)
Assert that a Map has entries; that is, it must not be
null
and must have at least one entry. |
static void |
notNull(Object object)
Assert that an object is not
null . |
static <T> T |
notNull(T object,
String message)
Assert that an object is not
null . |
static void |
state(boolean expression)
Assert a boolean expression, throwing
IllegalStateException
if the test result is false . |
static void |
state(boolean expression,
String message)
Assert a boolean expression, throwing
IllegalStateException
if the test result is false . |
static <T> T |
stateIsInstance(Class<T> type,
Object obj,
String message)
Asserts that the provided object is an instance of the provided class, throwing an
IllegalStateException otherwise. |
static <T> T |
stateNotNull(T value,
String msg)
Asserts that the specified
value is not null, otherwise throws an
IllegalStateException with the specified msg . |
public static void isTrue(boolean expression, String message)
IllegalArgumentException
if the test result is false
.
Assert.isTrue(i > 0, "The value must be greater than zero");
expression
- a boolean expressionmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if expression is false
public static void isTrue(boolean expression)
IllegalArgumentException
if the test result is false
.
Assert.isTrue(i > 0);
expression
- a boolean expressionIllegalArgumentException
- if expression is false
public static void isNull(Object object, String message)
null
.
Assert.isNull(value, "The value must be null");
object
- the object to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the object is not null
public static void isNull(Object object)
null
.
Assert.isNull(value);
object
- the object to checkIllegalArgumentException
- if the object is not null
public static <T> T notNull(T object, String message)
null
.
Assert.notNull(clazz, "The class must not be null");
T
- the type of objectobject
- the object to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the object is null
public static void notNull(Object object)
null
.
Assert.notNull(clazz);
object
- the object to checkIllegalArgumentException
- if the object is null
public static void hasLength(String text, String message)
null
and not the empty String.
Assert.hasLength(name, "Name must not be empty");
text
- the String to checkmessage
- the exception message to use if the assertion failsStrings.hasLength(java.lang.CharSequence)
public static void hasLength(String text)
null
and not the empty String.
Assert.hasLength(name);
text
- the String to checkStrings.hasLength(java.lang.CharSequence)
public static <T extends CharSequence> T hasText(T text, String message)
null
and must contain at least one non-whitespace character.
Assert.hasText(name, "'name' must not be empty");
T
- the type of CharSequencetext
- the CharSequence to checkmessage
- the exception message to use if the assertion failsStrings.hasText(java.lang.CharSequence)
public static void hasText(String text)
null
and must contain at least one non-whitespace character.
Assert.hasText(name, "'name' must not be empty");
text
- the String to checkStrings.hasText(java.lang.CharSequence)
public static void doesNotContain(String textToSearch, String substring, String message)
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
textToSearch
- the text to searchsubstring
- the substring to find within the textmessage
- the exception message to use if the assertion failspublic static void doesNotContain(String textToSearch, String substring)
Assert.doesNotContain(name, "rod");
textToSearch
- the text to searchsubstring
- the substring to find within the textpublic static Object[] notEmpty(Object[] array, String message)
null
and must have at least one element.
Assert.notEmpty(array, "The array must have elements");
array
- the array to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the object array is null
or has no elementspublic static void notEmpty(Object[] array)
null
and must have at least one element.
Assert.notEmpty(array);
array
- the array to checkIllegalArgumentException
- if the object array is null
or has no elementspublic static byte[] notEmpty(byte[] array, String msg)
array
- the byte array to checkmsg
- the exception message to use if the assertion failsIllegalArgumentException
- if the byte array is null or emptypublic static char[] notEmpty(char[] chars, String msg)
chars
- the character array to checkmsg
- the exception message to use if the assertion failsIllegalArgumentException
- if the character array is null or emptypublic static void noNullElements(Object[] array, String message)
Assert.noNullElements(array, "The array must have non-null elements");
array
- the array to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the object array contains a null
elementpublic static void noNullElements(Object[] array)
Assert.noNullElements(array);
array
- the array to checkIllegalArgumentException
- if the object array contains a null
elementpublic static <T extends Collection<?>> T notEmpty(T collection, String message)
null
and must have at least one element.
Assert.notEmpty(collection, "Collection must have elements");
T
- the type of collectioncollection
- the collection to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the collection is null
or has no elementspublic static void notEmpty(Collection<?> collection)
null
and must have at least one element.
Assert.notEmpty(collection, "Collection must have elements");
collection
- the collection to checkIllegalArgumentException
- if the collection is null
or has no elementspublic static <T extends Map<?,?>> T notEmpty(T map, String message)
null
and must have at least one entry.
Assert.notEmpty(map, "Map must have entries");
T
- the type of Map to checkmap
- the map to checkmessage
- the exception message to use if the assertion failsIllegalArgumentException
- if the map is null
or has no entriespublic static void notEmpty(Map map)
null
and must have at least one entry.
Assert.notEmpty(map);
map
- the map to checkIllegalArgumentException
- if the map is null
or has no entriespublic static <T> T isInstanceOf(Class<T> clazz, Object obj)
Assert.instanceOf(Foo.class, foo);
T
- the type of instance expectedclazz
- the required classobj
- the object to checkT
IllegalArgumentException
- if the object is not an instance of clazzClass.isInstance(java.lang.Object)
public static <T> T isInstanceOf(Class<T> type, Object obj, String message)
Assert.instanceOf(Foo.class, foo);
T
- the object's expected typetype
- the type to check againstobj
- the object to checkmessage
- a message which will be prepended to the message produced by
the function itself, and which may be used to provide context. It should
normally end in a ": " or ". " so that the function generate message looks
ok when prepended to it.type
.IllegalArgumentException
- if the object is not an instance of clazzClass.isInstance(java.lang.Object)
public static <T> T stateIsInstance(Class<T> type, Object obj, String message)
IllegalStateException
otherwise.
Assert.stateIsInstance(Foo.class, foo);
T
- the object's expected typetype
- the type to check againstobj
- the object to checkmessage
- a message which will be prepended to the message produced by
the function itself, and which may be used to provide context. It should
normally end in a ": " or ". " so that the function generate message looks
ok when prepended to it.type
.IllegalStateException
- if the object is not an instance of clazzClass.isInstance(java.lang.Object)
public static void isAssignable(Class superType, Class subType)
superType.isAssignableFrom(subType)
is true
.
Assert.isAssignable(Number.class, myClass);
superType
- the super type to checksubType
- the sub type to checkIllegalArgumentException
- if the classes are not assignablepublic static void isAssignable(Class superType, Class subType, String message)
superType.isAssignableFrom(subType)
is true
.
Assert.isAssignable(Number.class, myClass);
superType
- the super type to check againstsubType
- the sub type to checkmessage
- a message which will be prepended to the message produced by
the function itself, and which may be used to provide context. It should
normally end in a ": " or ". " so that the function generate message looks
ok when prepended to it.IllegalArgumentException
- if the classes are not assignablepublic static <T extends Comparable<T>> T eq(T value, T requirement, String msg)
value
is equal to the given requirement
, throwing
an IllegalArgumentException
with the given message if not.T
- the type of argumentvalue
- the value to checkrequirement
- the requirement that value
must be greater thanmsg
- the message to use for the IllegalArgumentException
if thrown.value
if greater than the specified requirement
.public static <T extends Comparable<T>> T gt(T value, T requirement, String msg)
value
is greater than the given requirement
, throwing
an IllegalArgumentException
with the given message if not.T
- the type of value to check and return if the requirement is metvalue
- the value to checkrequirement
- the requirement that value
must be greater thanmsg
- the message to use for the IllegalArgumentException
if thrown.value
if greater than the specified requirement
.public static <T extends Comparable<T>> T lte(T value, T requirement, String msg)
value
is less than or equal to the given requirement
, throwing
an IllegalArgumentException
with the given message if not.T
- the type of value to check and return if the requirement is metvalue
- the value to checkrequirement
- the requirement that value
must be greater thanmsg
- the message to use for the IllegalArgumentException
if thrown.value
if greater than the specified requirement
.public static void state(boolean expression, String message)
IllegalStateException
if the test result is false
. Call isTrue if you wish to
throw IllegalArgumentException on an assertion failure.
Assert.state(id == null, "The id property must not already be initialized");
expression
- a boolean expressionmessage
- the exception message to use if the assertion failsIllegalStateException
- if expression is false
public static void state(boolean expression)
IllegalStateException
if the test result is false
.
Call isTrue(boolean)
if you wish to
throw IllegalArgumentException
on an assertion failure.
Assert.state(id == null);
expression
- a boolean expressionIllegalStateException
- if the supplied expression is false
public static <T> T stateNotNull(T value, String msg) throws IllegalStateException
value
is not null, otherwise throws an
IllegalStateException
with the specified msg
. Intended to be used with
code invariants (as opposed to method arguments, like notNull(Object)
).T
- value typevalue
- value to assert is not nullmsg
- exception message to use if value
is nullIllegalStateException
- with the specified msg
if value
is null.Copyright © 2014–2024 jsonwebtoken.io. All rights reserved.