Collection::contains
). More specifically:
the checks will not execute a preliminary null check on the argument
before proceeding with the actual check. If the argument might be null
,
always perform a notNull()
check first. Otherwise, a raw, unprocessed
NullPointerException
can and will be thrown from the code
underlying Klojang Check.
Check.notNull(file).is(readable());
For ease of reading, the documentation for the checks will mostly use the term "argument" for the value being tested. Constantly repeating "argument, field, variable, program argument, system property, environment variable, etc." would not improve the quality and clarity of the documentation.
- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> ComposablePredicate
<T> array()
Verifies that the argument is an array or an array type.static <T> ComposablePredicate
<Result<T>> Verifies that a result is available.static ComposablePredicate
<String> blank()
Verifies that the argument isnull
or contains whitespace only.static <E,
C0 extends Collection<E>, C1 extends Collection<? super E>>
Relation<C0, C1> Verifies that aCollection
argument is a subset or sublist of anotherCollection
.static <O,
S extends Collection<? super O>>
Relation<S, O> contains()
Verifies that a collection contains a particular value.static <E,
C0 extends Collection<? super E>, C1 extends Collection<E>>
Relation<C0, C1> Verifies that aCollection
argument contains all the elements of the specified collection.Verifies that a map contains a particular key.Verifies that the argument contains the specified pattern (that is, the pattern can be found somewhere in the string).Verifies that the argument contains the specified pattern (that is, the pattern can be found somewhere in the string).Verifies that a map contains a particular value.static <T> ComposablePredicate
<T> Verifies that the argument is recursively non-empty.static <T> ComposablePredicate
<T> Verifies that the argument is notnull
and, if it is an array, collection or map, that it does not contain anynull
values.static ComposablePredicate
<File> Verifies that the argument is an existing directory.static <S,
O extends Collection<? super S>>
Relation<S, O> Alias forin()
.static <T> ComposablePredicate
<T> empty()
Verifies that the argument is empty.static ComposablePredicate
<String> Verifies that the argument is either null or an empty string.endsWith()
Verifies that the argument ends with the specified substring.Verifies that a string value starts with, ignoring case, the specified string.static IntRelation
eq()
Verifies that the argument equals the specifiedint
value.static <S,
O> Relation <S, O> EQ()
Verifies that the argument equals the provided value.equalsIC()
Verifies that a string value equals, ignoring case, the specified string.static <T> Relation
<T, T> equalTo()
Verifies that the argument equals some value.static ComposableIntPredicate
even()
Verifies that the argument is an even integer.static ComposablePredicate
<File> Verifies that the specified file is present on the file system.static IntRelation
gt()
Verifies that the argument is greater than the specifiedint
value.static <T extends Comparable<T>>
Relation<T, T> GT()
Verifies that the argument is greater than another value.static IntRelation
gte()
Verifies that the argument is greater than or equal to the specifiedint
value.static <T extends Comparable<T>>
Relation<T, T> GTE()
Verifies that the argument is greater than or equal to another value.Verifies that the argument matches the specified pattern (that is, the pattern fully describes the string).static Relation
<String, CharSequence> Verifies that the argument contains the specified substring.Verifies that a string value contains, ignoring case, the specified string.static <S,
O extends Collection<? super S>>
Relation<S, O> in()
Verifies that the argument is an element of a collection.static <O,
S extends O>
Relation<S, O[]> inArray()
Verifies that the argument is an element of an array.static <T> IntObjRelation
<T> Alias forindexOf()
.static <T> IntObjRelation
<T> Verifies that a value can be used as a "from" or "to" index in operations likeArrays.copyOfRange()
,String.substring()
andList.subList()
.static <T> IntObjRelation
<T> indexOf()
Verifies that the argument is a valid index into the specified array,List
orString
.static IntObjRelation
<int[]> Verifies that the argument is present in the specifiedint
array.Verifies that the argument is an instance of a particular class or interface.keyIn()
Verifies the presence of a key within a map.static IntRelation
lt()
Verifies that the argument is less than the specifiedint
value.static <T extends Comparable<T>>
Relation<T, T> LT()
Verifies that the argument is less than another value.static IntRelation
lte()
Verifies that the argument is less than or equal to the specifiedint
value.static <T extends Comparable<T>>
Relation<T, T> LTE()
Verifies that the argument is less than or equal to another value.matches()
Verifies that the argument matches the specified pattern (that is, the pattern fully describes the string).static IntRelation
Verifies that the argument is a multiple of the specifiedint
value.static IntRelation
ne()
Verifies that the argument does not equal the specifiedint
value.static ComposableIntPredicate
negative()
Verifies that the argument is a negative integer.static ComposablePredicate
<Boolean> no()
Verifies that a condition evaluates tofalse
.static <T> ComposablePredicate
<T> notEmpty()
Verifies that the argument is not empty.static <T> ComposablePredicate
<T> notNull()
Verifies that the argument is not null.static <T> ComposablePredicate
<T> NULL()
Verifies that the argument is null.static <T> Relation
<T, T> nullOr()
Verifies that the argument is either null or equals a particular value.Verifies that a string can be parsed into a number of the specified type without loss of information.static ComposableIntPredicate
odd()
Verifies that the argument is an odd integer.Verifies that a string can be parsed into aNumber
of the specified type without loss of information.static ComposablePredicate
<String> plainInt()
Verifies that a string consists of digits only (without '+' or '-' sign), no leading zeros, and can be parsed into an integer (by implication non-negative).static ComposablePredicate
<String> Verifies that a string consists of digits only (without '+' or '-' sign), no leading zeros, and can be parsed into a half-precision integer (by implication non-negative).static ComposableIntPredicate
positive()
Verifies that the argument is a positive integer.static <T> ComposablePredicate
<Optional<T>> present()
Verifies that the argument is a non-emptyOptional
.static ComposablePredicate
<File> readable()
Verifies that a file is readable.static ComposablePredicate
<File> Verifies that the argument is an existing, regular file.static <S,
O> Relation <S, O> sameAs()
Verifies that a value references the same object as another value.Verifies that the argument starts with the specified substring.Verifies that a string value starts with, ignoring case, the specified string.Verifies that the argument is a substring of the specified string.Verifies that the argument is a subtype of the provided type.Verifies that the argument is a supertype of the provided type.static ComposablePredicate
<File> symlink()
Verifies that the argument is a symbolic link.valueIn()
Verifies the presence of a value within a map.static ComposablePredicate
<File> writable()
Verifies that a file is writable.static ComposablePredicate
<Boolean> yes()
Verifies that a condition evaluates totrue
.static ComposableIntPredicate
zero()
Verifies that the argument is zero (0).
-
Method Details
-
NULL
Verifies that the argument is null. Equivalent toObjects::isNull
.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
-
notNull
Verifies that the argument is not null. Equivalent toObjects::nonNull
. Note thatNULL()
,yes()
andempty()
are the only checks that come with their negation:notNull()
,no()
andnotEmpty()
. The other checks need to be inverted using theisNot(...)
andnotHas(...)
methods ofObjectCheck
andIntCheck
.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
-
yes
Verifies that a condition evaluates totrue
.Check.that(connection.isOpen()).is(yes());
- Returns:
- a function implementing the test described above
-
no
Verifies that a condition evaluates tofalse
.- Returns:
- a function implementing the test described above
-
empty
Verifies that the argument is empty.Check.that(list).isNot(empty());
A value is defined to be empty if any of the following applies:
- it is
null
- it is an empty
CharSequence
- it is an empty
Collection
- it is an empty
Map
- it is an empty
Emptyable
- it is an empty
File
- it is a zero-length array
- it is an empty
Optional
or anOptional
containing an empty value
This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
- it is
-
emptyString
Verifies that the argument is either null or an empty string.This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Returns:
- a function implementing the test described above
-
notEmpty
Verifies that the argument is not empty. More precisely: it verifies the negation of theempty()
test.This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
-
deepNotNull
Verifies that the argument is notnull
and, if it is an array, collection or map, that it does not contain anynull
values. It could still be a zero-length array or zero-size collection or map, however. For maps, both keys and values are tested fornull
.This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
-
deepNotEmpty
Verifies that the argument is recursively non-empty. A value is defined to be deep-not-empty if any of the following applies:- it is a non-empty
CharSequence
- it is a non-empty
Collection
containing only deep-not-empty elements - it is a non-empty
Map
containing only deep-not-empty keys and values - it is a deep-not-empty
Emptyable
- it is a non-zero-length
Object[]
containing only deep-not-empty elements - it is a non-zero-length array of primitive values
- it is a non-empty
Optional
containing a deep-not-empty value - it is a
File
containing at least one non-whitespace character. Consequently, this check could be expensive if the argument is a largeFile
. Also note that this check will not verify that the file exists in the first place. If in doubt, execute theregularFile()
check first. - it is a non-null object of any other type
This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
- it is a non-empty
-
blank
Verifies that the argument isnull
or contains whitespace only. Probably more useful when called from anisNot
method.This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Returns:
- a function implementing the test described above
-
plainInt
Verifies that a string consists of digits only (without '+' or '-' sign), no leading zeros, and can be parsed into an integer (by implication non-negative).- Returns:
- a function implementing the test described above
-
plainShort
Verifies that a string consists of digits only (without '+' or '-' sign), no leading zeros, and can be parsed into a half-precision integer (by implication non-negative). Useful, for example, for validating TCP port numbers.- Returns:
- a function implementing the test described above
-
array
Verifies that the argument is an array or an array type.Object obj = new int[] {1, 2, 3, 4, 5}; Check.that(obj).is(array()); // OK Check.that(obj.getClass()).is(array()); // OK Check.that("foo").is(array()); // IllegalArgumentException
- Type Parameters:
T
- the type of the argument- Returns:
- a function implementing the test described above
-
regularFile
Verifies that the argument is an existing, regular file. NB To verify that a path string is valid, execute:Check.that(path).has(File::new, regularFile());
- Returns:
- a function implementing the test described above
-
directory
Verifies that the argument is an existing directory.- Returns:
- a function implementing the test described above
-
symlink
Verifies that the argument is a symbolic link.- Returns:
- a function implementing the test described above
-
fileExists
Verifies that the specified file is present on the file system. Equivalent toFile::exists
.// import static org.klojang.CommonChecks.fileExists; // import static org.klojang.CommonExceptions.fileNotFound; Check.that(file).is(fileExists(), fileNotFound(file));
- Returns:
- a function implementing the test described above
- See Also:
-
readable
Verifies that a file is readable. Implies that the file exists. Equivalent toFile::canRead
.- Returns:
- a function implementing the test described above
-
writable
Verifies that a file is writable. Implies that the file exists. Equivalent toFile::canWrite
.- Returns:
- a function implementing the test described above
-
present
Verifies that the argument is a non-emptyOptional
. Note that this check differs from theempty()
check in that it only verifies that theOptional
contains a value. Theempty()
check (in its negation) additionally requires that the value is itself non-empty.- Type Parameters:
T
- the type of the value contained in theOptional
- Returns:
- a function implementing the test described above
-
available
Verifies that a result is available. Note that this check differs from theempty()
check in that it only verifies thatResult
contains a value. Theempty()
check (in its negation) additionally requires that the value it contains is itself non-empty.- Type Parameters:
T
- the type of the value contained in theResult
- Returns:
- a function implementing the test described above
-
even
Verifies that the argument is an even integer.- Returns:
- a function implementing the test described above
-
odd
Verifies that the argument is an odd integer.- Returns:
- a function implementing the test described above
-
positive
Verifies that the argument is a positive integer.- Returns:
- a function implementing the test described above
-
negative
Verifies that the argument is a negative integer.- Returns:
- a function implementing the test described above
-
zero
Verifies that the argument is zero (0).- Returns:
- a function implementing the test described above
-
eq
Verifies that the argument equals the specifiedint
value.- Returns:
- a function implementing the test described above
-
ne
Verifies that the argument does not equal the specifiedint
value.- Returns:
- a function implementing the test described above
-
gt
Verifies that the argument is greater than the specifiedint
value.- Returns:
- a function implementing the test described above
-
gte
Verifies that the argument is greater than or equal to the specifiedint
value.- Returns:
- a function implementing the test described above
-
lt
Verifies that the argument is less than the specifiedint
value.- Returns:
- a function implementing the test described above
-
lte
Verifies that the argument is less than or equal to the specifiedint
value.- Returns:
- a function implementing the test described above
-
multipleOf
Verifies that the argument is a multiple of the specifiedint
value.- Returns:
- a function implementing the test described above
-
EQ
Verifies that the argument equals the provided value. Equivalent toObject::equals
. Note that this method is not equivalent toObjects::equals
and is therefore not null-safe. Execute a null check first, if necessary.- Type Parameters:
S
- the type of the subject of the relationship (which is the value being tested)O
- the type of the object of the relationship- Returns:
- a function implementing the test described above
-
equalTo
Verifies that the argument equals some value. Equivalent toObject::equals
. Use this check instead ofEQ()
if you want the compiler to enforce type equality between subject and object.- Type Parameters:
T
- the type of the objects being compared- Returns:
- a function implementing the test described above
-
GT
Verifies that the argument is greater than another value.- Type Parameters:
T
- the type of the values being compared- Returns:
- a function implementing the test described above
- See Also:
-
LT
Verifies that the argument is less than another value.- Type Parameters:
T
- the type of the values being compared- Returns:
- a function implementing the test described above
- See Also:
-
GTE
Verifies that the argument is greater than or equal to another value.- Type Parameters:
T
- the type of the values being compared- Returns:
- a function implementing the test described above
- See Also:
-
LTE
Verifies that the argument is less than or equal to another value.- Type Parameters:
T
- the type of the values being compared- Returns:
- a function implementing the test described above
- See Also:
-
sameAs
Verifies that a value references the same object as another value.- Type Parameters:
S
- the type of the subject of the relationship (which is the value being tested) (the subject of theRelation
)O
- the type of the value to compare it with (the object of theRelation
)- Returns:
- a function implementing the test described above
-
nullOr
Verifies that the argument is either null or equals a particular value.This check (implicitly) performs a null check and can be safely executed without or instead of executing the
notNull()
check first.- Type Parameters:
T
- the type of the subject of the relationship (which is the value being tested)- Returns:
- a function implementing the test described above
-
instanceOf
Verifies that the argument is an instance of a particular class or interface.- Type Parameters:
S
- the type of the subject of the relation (which is the value being tested)- Returns:
- a function implementing the test described above
-
supertypeOf
Verifies that the argument is a supertype of the provided type. In other words, the provided type should extend, implement or equal the argument. Equivalent toClass::isAssignableFrom
.- Type Parameters:
S
- the type of the subject's classO
- the type of the object's class- Returns:
- a function that implements the test described above
-
subtypeOf
Verifies that the argument is a subtype of the provided type. In other words, the argument should extend, implement or equal the provided type.- Type Parameters:
S
- the type of the subject's classO
- the type of the object's class- Returns:
- a function that implements the test described above
-
contains
Verifies that a collection contains a particular value. Equivalent toCollection::contains
.- Type Parameters:
O
- the type of the elements in theCollection
S
- the type of the collection- Returns:
- a function implementing the test described above
-
containsKey
Verifies that a map contains a particular key. Equivalent toMap::containsKey
.- Type Parameters:
O
- the type of the keys within the mapS
- the Type of theMap
- Returns:
- a function implementing the test described above
-
containsValue
Verifies that a map contains a particular value. Equivalent toMap::containsValue
.- Type Parameters:
O
- the type of the values within the mapS
- the Type of theMap
- Returns:
- a function implementing the test described above
-
in
Verifies that the argument is an element of a collection.- Type Parameters:
S
- the type of the argumentO
- the type of theCollection
- Returns:
- a function implementing the test described above
-
elementOf
Alias forin()
. Note that this method will even report itself to be the "in" check.- Type Parameters:
S
- the type of the argumentO
- the type of theCollection
- Returns:
- a function implementing the test described above
-
keyIn
Verifies the presence of a key within a map.- Type Parameters:
S
- the type of the keys within the mapO
- the Type of theMap
- Returns:
- a function implementing the test described above
-
valueIn
Verifies the presence of a value within a map.- Type Parameters:
S
- the type of the keys within the mapO
- the Type of theMap
- Returns:
- a function implementing the test described above
-
inArray
Verifies that the argument is an element of an array.- Type Parameters:
O
- the component type of the arrayS
- the type of the subject of the relationship (which is the value being tested)- Returns:
- a function implementing the test described above
-
containsAll
public static <E,C0 extends Collection<? super E>, Relation<C0,C1 extends Collection<E>> C1> containsAll()Verifies that aCollection
argument contains all the elements of the specified collection. Equivalent toCollection::containsAll
.Check.that(List.of(1, 2, 3)).is(enclosing(), Set.of(1, 2)); // true Check.that(List.of(1, 2)).is(enclosing(), Set.of(1, 2, 3)); // false
- Type Parameters:
E
- The type of the elements in theCollection
C0
- The type of the argument (the subject of theRelation
)C1
- The type of the object of theRelation
- Returns:
- a function implementing the test described above
-
containedIn
public static <E,C0 extends Collection<E>, Relation<C0,C1 extends Collection<? super E>> C1> containedIn()Verifies that aCollection
argument is a subset or sublist of anotherCollection
.Check.that(List.of(1, 2, 3)).is(enclosedBy(), Set.of(1, 2)); // false Check.that(List.of(1, 2)).is(enclosedBy(), Set.of(1, 2, 3)); // true
- Type Parameters:
E
- The type of the elements in theCollection
C0
- The type of the argument (the subject of theRelation
)C1
- The type of the object of theRelation
- Returns:
- a function implementing the test described above
-
hasSubstring
Verifies that the argument contains the specified substring. Equivalent toString::contains
.- Returns:
- a function implementing the test described above
-
substringOf
Verifies that the argument is a substring of the specified string.- Returns:
- a function implementing the test described above
-
startsWith
Verifies that the argument starts with the specified substring. Equivalent toString::startsWith
.- Returns:
- a function implementing the test described above
-
endsWith
Verifies that the argument ends with the specified substring. Equivalent toString::endsWith
.- Returns:
- a function implementing the test described above
-
hasPattern
Verifies that the argument matches the specified pattern (that is, the pattern fully describes the string).- Returns:
- a function implementing the test described above
- See Also:
-
containsPattern
Verifies that the argument contains the specified pattern (that is, the pattern can be found somewhere in the string).- Returns:
- a function implementing the test described above
- See Also:
-
matches
Verifies that the argument matches the specified pattern (that is, the pattern fully describes the string). The subject of the returnedRelation
is the string to match; the object of theRelation
is a regular expression to be compiled into aPattern
.Check.that("abcd123").is(matches(), "^\\w{4}\\d{3}$"); // yes Check.that("abcd123").is(matches(), "\\d{3}"); // no
- Returns:
- a function implementing the test described above
-
containsMatch
Verifies that the argument contains the specified pattern (that is, the pattern can be found somewhere in the string). The subject of the returnedRelation
is the string to match; the object of theRelation
is a regular expression to be compiled into aPattern
.Check.that("abcd123").is(containsMatch(), "\\d{3}"); // yes Check.that("abcd123").is(containsMatch(), "\\d{4}"); // no
- Returns:
- a function implementing the test described above
-
numerical
Verifies that a string can be parsed into a number of the specified type without loss of information. The provided type must be one of the primitive number types:long
,int
,short
,byte
,double
orfloat
. Specifying a wrapper type (e.g.Integer
) will result in aCorruptCheckException
.Check.that("123").is(numerical(), int.class); // yes Check.that("123.0").is(numerical(), int.class); // no
- Type Parameters:
T
- the type of the number into which to parse the string- Returns:
- a function implementing the test described above
- See Also:
-
parsableAs
Verifies that a string can be parsed into aNumber
of the specified type without loss of information. The provided type must be one of the primitive number types:long
,int
,short
,byte
,double
orfloat
. Specifying a wrapper type (e.g.Integer
) will result in aCorruptCheckException
. Contrary to thenumerical()
check, this check allows the string to contain a fractional part even if the target type is an integral type (likebyte
), as long as it consists of zeros only. Scientific notation is allowed, too, as long as the effective fractional part consists of zeros only. ForDouble
andFloat
there is no difference between the two checks.Check.that("123").is(parsableAs(), int.class); // yes Check.that("123.0").is(parsableAs(), int.class); // yes
- Type Parameters:
T
- the type of the number into which to parse the string- Returns:
- a function implementing the test described above
- See Also:
-
equalsIC
Verifies that a string value equals, ignoring case, the specified string. Equivalent toString::equalsIgnoreCase
.- Returns:
- a function implementing the test described above
-
startsWithIC
Verifies that a string value starts with, ignoring case, the specified string.- Returns:
- a function implementing the test described above
-
endsWithIC
Verifies that a string value starts with, ignoring case, the specified string.- Returns:
- a function implementing the test described above
-
hasSubstringIC
Verifies that a string value contains, ignoring case, the specified string.- Returns:
- a function implementing the test described above
-
indexOf
Verifies that the argument is a valid index into the specified array,List
orString
. No preliminary check is done to ensure the provided object actually is an array,List
orString
. ACorruptCheckException
is thrown if it is not. Execute theinstanceOf()
orarray()
check first, if necessary.- Type Parameters:
T
- the type of the object of theIntObjRelation
- must be aString
,List
or array- Returns:
- a function implementing the test described above
-
indexExclusiveOf
Alias forindexOf()
. Can be used if the class you are working in already contains anindexOf()
method. Note that this will report itself to be theindexOf()
check:Check.that(42, "foo").is(indexExclusiveOf(), new int[10], "${tag} did not pass the ${test}() test"); // "foo did not pass the indexOf() test"
- Type Parameters:
T
- the type of the object of theIntObjRelation
- must be aString
,List
or array- Returns:
- a function implementing the test described above
-
indexInclusiveOf
Verifies that a value can be used as a "from" or "to" index in operations likeArrays.copyOfRange()
,String.substring()
andList.subList()
. These operations allow both the "from" index and the "to" index to be equal to the length of the array, string or list. No preliminary check is done to ensure the provided object actually is an array,List
orString
. ACorruptCheckException
is thrown if it is not. Execute theinstanceOf()
orarray()
check first, if necessary.- Type Parameters:
T
- the type of the object of theIntObjRelation
- must be aString
,List
or array- Returns:
- a function implementing the test described above
- See Also:
-
inIntArray
Verifies that the argument is present in the specifiedint
array.- Returns:
- a function implementing the test described above
-