public final class Requirements extends Object
This code licensed under Mozilla Public License Version 2.0.
The primary place to start is here: require(Object, Predicate)
| Modifier and Type | Method and Description |
|---|---|
static <T> Predicate<T> |
and(Predicate<T> p1,
Predicate<T> p2)
Returns a predicate that is the "and" of
p1 and p2. |
static <T,U> Predicate<T> |
chain(Function<T,U> f,
Predicate<U> p)
Returns a predicate for the test of
f(o). |
static <E,T extends Collection<? extends E>> |
contains(E val)
Returns a predicate that checks for its collection to contain
val. |
static <K,V,T extends Map<? extends K,? extends V>> |
containsKey(K key)
Returns a predicate that checks for its map to contain
key. |
static <T extends Comparable<T>> |
eq(T val)
Returns a predicate that checks for its object to be
== val (via numerical sense). |
static Predicate<boolean[]> |
equalTo(boolean[] arr)
Returns a predicate that checks for its boolean[] object to be
== arr (using Arrays.equals). |
static Predicate<byte[]> |
equalTo(byte[] arr)
Returns a predicate that checks for its byte[] object to be
== arr (using Arrays.equals). |
static Predicate<char[]> |
equalTo(char[] arr)
Returns a predicate that checks for its char[] object to be
== arr (using Arrays.equals). |
static Predicate<double[]> |
equalTo(double[] arr)
Returns a predicate that checks for its double[] object to be
== arr (using Arrays.equals). |
static <E> Predicate<E[]> |
equalTo(E[] arr)
Returns a predicate that checks for its Object[] object to be
== arr (using Arrays.deepEquals). |
static Predicate<float[]> |
equalTo(float[] arr)
Returns a predicate that checks for its float[] object to be
== arr (using Arrays.equals). |
static Predicate<int[]> |
equalTo(int[] arr)
Returns a predicate that checks for its int[] object to be
== arr (using Arrays.equals). |
static Predicate<long[]> |
equalTo(long[] arr)
Returns a predicate that checks for its long[] object to be
== arr (using Arrays.equals). |
static Predicate<short[]> |
equalTo(short[] arr)
Returns a predicate that checks for its short[] object to be
== arr (using Arrays.equals). |
static <T> Predicate<T> |
equalTo(T val)
Returns a predicate that checks for its Object object to be
== arr (using equals). |
static <T extends Comparable<T>> |
ge(T val)
Returns a predicate that checks for its object to be
>= val. |
static <T extends Comparable<T>> |
gt(T val)
Returns a predicate that checks for its object to be
> val. |
static <T> Function<T,Integer> |
hash()
Returns the Object::hashCode function named "hash".
|
static <K,V,T extends Map<? extends K,? extends V>> |
keySet()
Returns the Map::keySet function named "keys".
|
static <T extends Comparable<T>> |
le(T val)
Returns a predicate that checks for its object to be
<= val. |
static Function<String,Integer> |
length()
Returns the String::length function named "length".
|
static <T extends Comparable<T>> |
lt(T val)
Returns a predicate that checks for its object to be
< val. |
static <K,V,T extends Map<? extends K,? extends V>> |
mapNotEmpty()
Returns a predicate that checks for its map to be not empty.
|
static <K,V,T extends Map<? extends K,? extends V>> |
mapSize()
Returns the Map::size function named "size".
|
static Predicate<String> |
matches(Pattern p)
Returns a predicate that checks for its string argument to match
p. |
static Predicate<String> |
matches(String p)
Returns a predicate that checks for its string argument to match
p. |
static Predicate<String> |
maxLength(int n)
Returns a predicate that checks for its string argument to have a length
<= n. |
static <E,T extends Collection<? extends E>> |
maxSize(int n)
Returns a predicate that checks for its collection to have a size
<= n. |
static <T> Predicate<T> |
memberOf(Collection<? extends T> coll)
Returns a predicate that checks for its object to be a member of
coll. |
static <T> Predicate<T> |
memberOf(T... arr)
Returns a predicate that checks for its object to be a member of
arr. |
static Predicate<String> |
minLength(int n)
Returns a predicate that checks for its string argument to have a length
>= n. |
static <E,T extends Collection<? extends E>> |
minSize(int n)
Returns a predicate that checks for its collection to have a size
>= n. |
static <T> Predicate<T> |
name(Predicate<T> pred,
Supplier<String> msg)
Add a toString to
pred. |
static <T,U> Function<T,U> |
nameF(Function<T,U> f,
Supplier<String> msg)
Adds a toString to a predicate.
|
static <T extends Comparable<T>> |
ne(T val)
Returns a predicate that checks for its object to be
!= val (via numerical sense). |
static <T> Predicate<T> |
negate(Predicate<T> p)
Returns a predicate that is the "not" of
p. |
static Predicate<String> |
notBlank()
Returns a predicate that checks for its string argument to be not blank.
|
static <E,T extends Collection<? extends E>> |
notEmpty()
Returns a predicate that checks for its collection based argument to be not empty.
|
static <T> Predicate<T> |
notNull()
Returns a predicate that checks for its argument to be not null.
|
static <T> Predicate<T> |
or(Predicate<T> p1,
Predicate<T> p2)
Returns a predicate that is the "or" of
p1 and p2. |
static <T> T |
require(T obj,
Predicate<T> validator)
Performs a test on the argument, returning that argument if it succeeds, throwing an
IllegalArgumentException if it fails. |
static <T,EX extends Exception> |
require(T obj,
Predicate<T> validator,
BiFunction<T,Predicate<T>,EX> ex)
Performs a test on the argument, returning that argument if it succeeds, but throwing a user supplied exception if it fails.
|
static <T> T |
require(T obj,
Predicate<T> validator,
Supplier<String> msg)
Performs a test on the argument, returning that argument if it succeeds, but throwing an
IllegalArgumentException if it fails. |
static <T> Predicate<T> |
same(T val)
Returns a predicate that checks for its object to be
== val (using pointer equality). |
static <E,T extends Collection<? extends E>> |
size()
Returns the Collection::size function named "size".
|
static <T> Function<T,String> |
stringify()
Returns the Object::toString function named "toString".
|
static <E,T extends Collection<? extends E>> |
subSetOf(Collection<? extends E> coll)
Returns a predicate that checks for its collection to be a subset of
coll. |
static <E,T extends Collection<? extends E>> |
subSetOf(E... arr)
Returns a predicate that checks for its collection to be a subset of
arr. |
static <E,T extends Collection<? extends E>> |
superSetOf(Collection<? extends E> coll)
Returns a predicate that checks for its collection to be a superset of
coll. |
static <E,T extends Collection<? extends E>> |
superSetOf(E... arr)
Returns a predicate that checks for its collection to be a superset of
arr. |
static <K,V,T extends Map<? extends K,? extends V>> |
values()
Returns the Map::values function named "values".
|
public static <T,EX extends Exception> T require(T obj, Predicate<T> validator, BiFunction<T,Predicate<T>,EX> ex) throws EX extends Exception
Example:
Require.require(null, Require.notNull(), (o, v) -> new MyException()) # throws MyException
Require.require("abc", Require.notNull(), (o, v) -> new MyException()) == "abc"
T - The type of the object being testedEX - The type of the exception that could be thrown.obj - The object to verify - Can be nullvalidator - The predicate that must succeed - Can't be nullex - The Exception supplier - Can't be nullEX - if the test failsEX extends Exceptionpublic static <T> T require(T obj,
Predicate<T> validator,
Supplier<String> msg)
IllegalArgumentException if it fails.
A specified prefix is prepended to the beginning of the message
in that exception.
Example:
Require.require(null, Require.notNull(), () -> "someobj") # throws IllegalArgumentException("someobj: Must not be null")
Require.require("abc", Require.notNull(), () -> "someobj") == "abc"
T - The type of the object being testedobj - The object to verify - Can be nullvalidator - The predicate that must succeed - Can't be null - Note that "toString" on this
predicate will be called. To make this predicate have a toString, use name(Predicate)msg - The supplier of the additional message to prepend - Can't be nullIllegalArgumentException - if the test failspublic static <T> T require(T obj,
Predicate<T> validator)
IllegalArgumentException if it fails.
Example:
Require.require(null, Require.notNull()) # throws IllegalArgumentException("Must not be null")
Require.require("abc", Require.notNull()) == "abc"
T - The type of the object being testedobj - The object to verify - Can be nullvalidator - The predicate that must succeed - Can't be null - Note that "toString" on this
predicate will be called. To make this predicate have a toString, use name(Predicate)IllegalArgumentException - if the test failspublic static <T> Predicate<T> notNull()
Example:
Require.require(null, Require.notNull()) # throws IllegalArgumentException("Must not be null")
Require.require("abc", Require.notNull()) == "abc"
T - The type of the object being testedpublic static Predicate<String> notBlank()
Example:
Require.require(null, Require.notBlank()) # throws IllegalArgumentException("Must not be blank")
Require.require("", Require.notBlank()) # throws IllegalArgumentException("Must not be blank")
Require.require("abc", Require.notBlank()) == "abc"
public static Predicate<String> minLength(int n)
>= n.
A string must be non-null and have a length of at least "n" to satisfy the condition.
Example:
Require.require(null, Require.minLength(2)) # throws IllegalArgumentException("Must have a length of at least 2")
Require.require("", Require.minLength(2)) # throws IllegalArgumentException("Must have a length of at least 2")
Require.require("ab", Require.minLength(2)) == "ab"
Require.require("abc", Require.minLength(2)) == "abc"
n - The minimum length - Must be >= 0>= npublic static Predicate<String> maxLength(int n)
<= n.
A string must be non-null and have a length of at most "n" to satisfy the condition.
Example:
Require.require(null, Require.maxLength(2)) # throws IllegalArgumentException("Must have a length of at most 2")
Require.require("", Require.maxLength(2)) == ""
Require.require("ab", Require.maxLength(2)) == "ab"
Require.require("abc", Require.maxLength(2)) # throws IllegalArgumentException("Must have a length of at most 2")
n - The maximum length - Must be >= 0<= npublic static Predicate<String> matches(Pattern p)
p.
A string must be non-null and match the pattern to satisfy the condition.
Example:
Require.require(null, Require.matches(Pattern.compile("\\d+"))) # throws IllegalArgumentException("Must match \d+")
Require.require("", Require.matches(Pattern.compile("\\d+"))) # throws IllegalArgumentException("Must match \d+")
Require.require("1", Require.matches(Pattern.compile("\\d+"))) == "1"
Require.require("123", Require.matches(Pattern.compile("\\d+"))) == "123"
Require.require("abc123", Require.matches(Pattern.compile("\\d+"))) # throws IllegalArgumentException("Must match \d+")
p - The pattern - Can't be nullppublic static Predicate<String> matches(String p)
p.
A string must be non-null and match the pattern to satisfy the condition.
Example:
Require.require(null, Require.matches("\\d+")) # throws IllegalArgumentException("Must match \d+")
Require.require("", Require.matches("\\d+")) # throws IllegalArgumentException("Must match \d+")
Require.require("1", Require.matches("\\d+")) == "1"
Require.require("123", Require.matches("\\d+")) == "123"
Require.require("abc123", Require.matches("\\d+")) # throws IllegalArgumentException("Must match \d+")
p - The pattern - Can't be nullppublic static <E,T extends Collection<? extends E>> Predicate<T> notEmpty()
Example:
Require.require(null, Require.notEmpty()) # throws IllegalArgumentException("Must not be empty")
Require.require([], Require.notEmpty()) # throws IllegalArgumentException("Must not be empty")
Require.require([1, 2], Require.notEmpty()) == [1, 2]
E - The type of the elements of the collectionT - The Collection type of the object being testedpublic static <E,T extends Collection<? extends E>> Predicate<T> contains(E val)
val.
A collection must be non-null and contain the given object to satisfy the condition.
Example:
Require.require(null, Require.contains(1)) # throws IllegalArgumentException("Must contain 1")
Require.require([], Require.contains(1)) # throws IllegalArgumentException("Must contain 1")
Require.require([2, 3], Require.contains(1)) # throws IllegalArgumentException("Must contain 1")
Require.require([1, 2], Require.contains(1)) == [1]
E - The type of the elements of the collection.T - The Collection type of the object being testedval - The object the collection must contain - Can be nullvalpublic static <K,V,T extends Map<? extends K,? extends V>> Predicate<T> containsKey(K key)
key.
A map must be non-null and contain the given key to satisfy the condition.
Example:
Require.require(null, Require.containsKey(1)) # throws IllegalArgumentException("Must contain key 1")
Require.require({}, Require.containsKey(1)) # throws IllegalArgumentException("Must contain key 1")
Require.require({a => 1, b => 2}, Require.containsKey(c)) # throws IllegalArgumentException("Must contain 1")
Require.require({a => 1, b => 2}, Require.containsKey(a)) == {a => 1, b => 2}
K - The type of the keys of the map.V - The type of the values of the map.T - The Map type of the object being testedkey - The key the map must contain - Can be nullkeypublic static <K,V,T extends Map<? extends K,? extends V>> Predicate<T> mapNotEmpty()
Example:
Require.require(null, Require.mapNotEmpty()) # throws IllegalArgumentException("Must not be empty")
Require.require({}, Require.mapNotEmpty()) # throws IllegalArgumentException("Must not be empty")
Require.require({a => 1, b => 2}, Require.mapNotEmpty()) == {a => 1, b => 2}
K - The type of the keys of the mapV - The type of the values of the mapT - The Map type of the object being testedpublic static <E,T extends Collection<? extends E>> Predicate<T> minSize(int n)
>= n.
A collection must be non-null and have a size of at least "n" to satisfy the condition.
Example:
Require.require(null, Require.minSize(2)) # throws IllegalArgumentException("Must have a size of at least 2")
Require.require([], Require.minSize(2)) # throws IllegalArgumentException("Must have a size of at least 2")
Require.require([1, 2], Require.minSize(2)) == [1, 2]
Require.require([1, 2, 3], Require.minSize(2)) == [1, 2, 3]
E - The element type of the collectionT - The type of the collectionn - The minimum size - Must be >= 0>= npublic static <E,T extends Collection<? extends E>> Predicate<T> maxSize(int n)
<= n.
A collection must be non-null and have a size of at most "n" to satisfy the condition.
Example:
Require.require(null, Require.maxSize(2)) # throws IllegalArgumentException("Must have a size of at most 2")
Require.require([], Require.maxSize(2)) == []
Require.require([1, 2], Require.maxSize(2)) == [1, 2]
Require.require([1, 2, 3], Require.maxSize(2)) # throws IllegalArgumentException("Must have a size of at most 2")
E - The element type of the collectionT - The type of the collectionn - The maximum size - Must be >= 0<= npublic static <E,T extends Collection<? extends E>> Predicate<T> superSetOf(Collection<? extends E> coll)
coll.
A collection must be non-null and be a superset of the given collection to satisfy the condition.
Example:
Require.require(null, Require.superSetOf([1, 2])) # throws IllegalArgumentException("Must be superset of [1, 2]")
Require.require([], Require.superSetOf([1, 2])) # throws IllegalArgumentException("Must be superset of [1, 2]")
Require.require([1], Require.superSetOf([1, 2])) # throws IllegalArgumentException("Must be superset of [1, 2]")
Require.require([1, 2], Require.superSetOf([1, 2])) == [1, 2]
Require.require([1, 2, 3], Require.superSetOf([1, 2])) == [1, 2, 3]
E - The type of the elements of either collection.T - The Collection type of the object being testedcoll - The collection to test against - Can't be nullcoll@SafeVarargs public static <E,T extends Collection<? extends E>> Predicate<T> superSetOf(E... arr)
arr.
A collection must be non-null and be a superset of the given collection to satisfy the condition.
Example:
Require.require(null, Require.superSetOf(1, 2)) # throws IllegalArgumentException("Must be superset of [1, 2]")
Require.require([], Require.superSetOf(1, 2)) # throws IllegalArgumentException("Must be superset of [1, 2]")
Require.require([1], Require.superSetOf(1, 2)) # throws IllegalArgumentException("Must be superset of [1, ]}")
Require.require([1, 2], Require.superSetOf(1, 2)) == [1, 2]
Require.require([1, 2, 3], Require.superSetOf(1, 2)) == [1, 2, 3]
E - The type of the elements of either collection.T - The Collection type of the object being testedarr - The collection to test against - Can't be nullarrpublic static <E,T extends Collection<? extends E>> Predicate<T> subSetOf(Collection<? extends E> coll)
coll.
A collection must be non-null and be a subset of the given collection to satisfy the condition.
Example:
Require.require(null, Require.subSetOf([1, 2])) # throws IllegalArgumentException("Must be subset of [1, 2]")
Require.require([], Require.subSetOf([1, 2])) == []
Require.require([1], Require.subSetOf([1, 2])) == [1]
Require.require([1, 2], Require.subSetOf([1, 2])) == [1, 2]
Require.require([1, 2, 3], Require.subSetOf([1, 2])) # throws IllegalArgumentException("Must be subset of [1, 2]")
E - The type of the elements of either collection.T - The Collection type of the object being testedcoll - The collection to test against - Can't be nullcoll@SafeVarargs public static <E,T extends Collection<? extends E>> Predicate<T> subSetOf(E... arr)
arr.
A collection must be non-null and be a subset of the given collection to satisfy the condition.
Example:
Require.require(null, Require.subSetOf(1, 2)) # throws IllegalArgumentException("Must be subset of [1, 2]")
Require.require([], Require.subSetOf(1, 2)) == []
Require.require([1], Require.subSetOf(1, 2)) == [1]
Require.require([1, 2], Require.subSetOf(1, 2)) == [1, 2]
Require.require([1, 2, 3], Require.subSetOf(1, 2)) # throws IllegalArgumentException("Must be subset of [1, 2]")
E - The type of the elements of either collection.T - The Collection type of the object being testedarr - The collection to test against - Can't be nullarr@SafeVarargs public static <T> Predicate<T> memberOf(T... arr)
arr.
An object must be a member of the given collection to satisfy the condition.
Example:
Require.require(null, Require.memberOf([]) ) # throws IllegalArgumentException("Must be a member of {}")
Require.require(null, Require.memberOf([null])) == null
Require.require(null, Require.memberOf([1, 2])) # throws IllegalArgumentException("Must be a member of [1, 2]")
Require.require(1, Require.memberOf([1, 2])) == 1
Require.require(3, Require.memberOf([1, 2])) # throws IllegalArgumentException("Must be a member of [1, 2]")
T - The type of the object being testedarr - The collection that must contain the object - Can't be nullarrpublic static <T> Predicate<T> memberOf(Collection<? extends T> coll)
coll.
An object must be a member of the given collection to satisfy the condition.
Example:
Require.require(null, Require.memberOf([]) ) # throws IllegalArgumentException("Must be a member of []")
Require.require(null, Require.memberOf([null])) == null
Require.require(null, Require.memberOf([1, 2])) # throws IllegalArgumentException("Must be a member of [1, 2]")
Require.require(1, Require.memberOf([1, 2])) == 1
Require.require(3, Require.memberOf([1, 2])) # throws IllegalArgumentException("Must be a member of [1, 2]")
T - The type of the object being testedcoll - The collection that must contain the object - Can't be nullcollpublic static <T extends Comparable<T>> Predicate<T> lt(T val)
< val.
An object must be less than another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.lt(null)) # throws IllegalArgumentException("Must be less than null")
Require.require(null, Require.lt(2) ) == null
Require.require(1, Require.lt(2) ) == 1
Require.require(2, Require.lt(2) ) # throws IllegalArgumentException("Must be less than 2")
Require.require(3, Require.lt(2) ) # throws IllegalArgumentException("Must be less than 2")
T - The type of the object being testedval - The object to compare to - Can be null< valpublic static <T extends Comparable<T>> Predicate<T> gt(T val)
> val.
An object must be greater than another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.gt(null)) # throws IllegalArgumentException("Must be greater than null")
Require.require(null, Require.gt(2) ) # throws IllegalArgumentException("Must be greater than 2")
Require.require(1, Require.gt(2) ) # throws IllegalArgumentException("Must be greater than 2")
Require.require(2, Require.gt(2) ) # throws IllegalArgumentException("Must be greater than 2")
Require.require(3, Require.gt(2) ) == 3
T - The type of the object being testedval - The object to compare to - Can be null> val.public static <T extends Comparable<T>> Predicate<T> le(T val)
<= val.
An object must be less than or equal to another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.le(null)) == null
Require.require(null, Require.le(2) ) == null
Require.require(1, Require.le(2) ) == 1
Require.require(2, Require.le(2) ) == 2
Require.require(3, Require.le(2) ) # throws IllegalArgumentException("Must be less than or equal to 2")
T - The type of the object being testedval - The object to compare to - Can be null<= valpublic static <T extends Comparable<T>> Predicate<T> ge(T val)
>= val.
An object must be greater than or equal to another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.ge(null)) == null
Require.require(null, Require.ge(2) ) # throws IllegalArgumentException("Must be greater than or equal to 2")
Require.require(1, Require.ge(2) ) # throws IllegalArgumentException("Must be greater than or equal to 2")
Require.require(2, Require.ge(2) ) == 2
Require.require(3, Require.ge(2) ) == 3
T - The type of the object being testedval - The object to compare to - Can be null>= valpublic static <T extends Comparable<T>> Predicate<T> eq(T val)
== val (via numerical sense).
An object must be equal to another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.eq(null)) == null
Require.require(null, Require.eq(2) ) # throws IllegalArgumentException("Must be equal to 2")
Require.require(1, Require.eq(2) ) # throws IllegalArgumentException("Must be equal to 2")
Require.require(2, Require.eq(2) ) == 2
Require.require(3, Require.eq(2) ) # throws IllegalArgumentException("Must be equal to 2")
T - The type of the object being testedval - The object to compare to - Can be null== valpublic static <T extends Comparable<T>> Predicate<T> ne(T val)
!= val (via numerical sense).
An object must not be equal to another object using compareTo to satisfy the condition.
NULL comparison follows the rule null always less than non-null.
Example:
Require.require(null, Require.ne(null)) # throws IllegalArgumentException("Must be not equal to null")
Require.require(null, Require.ne(2) ) == null
Require.require(1, Require.ne(2) ) == 1
Require.require(2, Require.ne(2) ) # throws IllegalArgumentException("Must be not equal to 2")
Require.require(3, Require.ne(2) ) == 3
T - The type of the object being testedval - The object to compare to - Can be null!= valpublic static Predicate<byte[]> equalTo(byte[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1], Require.equalTo([1, 2])) # throws IllegalArgumentException("Must be equal to [1, 2]")
Require.require([1, 2], Require.equalTo([1, 2])) == [1, 2]
Require.require([2, 1], Require.equalTo([1, 2])) # throws IllegalArgumentException("Must be equal to [1, 2]")
Require.require([1, 2, 3], Require.equalTo([1, 2])) # throws IllegalArgumentException("Must be equal to [1, 2]")
arr - The object to compare to - Can be null== arrpublic static Predicate<short[]> equalTo(short[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2], Require.equalTo([1, 2])) == [1, 2]
Require.require([2, 1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2, 3], Require.equalTo([1, 2])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<int[]> equalTo(int[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2], Require.equalTo([1, 2])) == [1, 2]
Require.require([2, 1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2, 3], Require.equalTo([1, 2])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<long[]> equalTo(long[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2], Require.equalTo([1, 2])) == [1, 2]
Require.require([2, 1], Require.equalTo([1, 2])) # throws IllegalArgumentException
Require.require([1, 2, 3], Require.equalTo([1, 2])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<char[]> equalTo(char[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require(['1'], Require.equalTo(['1', '2'])) # throws IllegalArgumentException
Require.require(['1', '2'], Require.equalTo(['1', '2'])) == ['1', '2']
Require.require(['2', '1'], Require.equalTo(['1', '2'])) # throws IllegalArgumentException
Require.require(['1', '2', '3'], Require.equalTo(['1', '2'])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<boolean[]> equalTo(boolean[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([true], Require.equalTo([true, false])) # throws IllegalArgumentException
Require.require([true, false], Require.equalTo([true, false])) == [true, false]
Require.require([false, true], Require.equalTo([true, false])) # throws IllegalArgumentException
Require.require([true, false, false], Require.equalTo([true, false])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<float[]> equalTo(float[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
Require.require([1.0, 2.0], Require.equalTo([1.0, 2.0])) == [1.0, 2.0]
Require.require([2.0, 1.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
Require.require([1.0, 2.0, 3.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static Predicate<double[]> equalTo(double[] arr)
== arr (using Arrays.equals).
An object must not be equal to another object using Arrays.equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require([1.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
Require.require([1.0, 2.0], Require.equalTo([1.0, 2.0])) == [1.0, 2.0]
Require.require([2.0, 1.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
Require.require([1.0, 2.0, 3.0], Require.equalTo([1.0, 2.0])) # throws IllegalArgumentException
arr - The object to compare to - Can be null== arrpublic static <E> Predicate<E[]> equalTo(E[] arr)
== arr (using Arrays.deepEquals).
An object must not be equal to another object using Arrays.deepEquals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo(null) ) == null
Require.require([], Require.equalTo(null) ) # throws IllegalArgumentException("Must be equal to null")
Require.require([], Require.equalTo([]) ) == []
Require.require(["1"], Require.equalTo(["1", "2"])) # throws IllegalArgumentException
Require.require(["1", "2"], Require.equalTo(["1", "2"])) == ["1", "2"]
Require.require(["2", "1"], Require.equalTo(["1", "2"])) # throws IllegalArgumentException
Require.require(["1", "2", "3"], Require.equalTo(["1", "2"])) # throws IllegalArgumentException
E - the element type of the array being testsarr - The object to compare to - Can be null== arrpublic static <T> Predicate<T> equalTo(T val)
== arr (using equals).
An object must be equal to another object using equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.equalTo("1") ) # throws IllegalArgumentException
Require.require(null, Require.equalTo(null)) == null
Require.require("", Require.equalTo(null)) # throws IllegalArgumentException
Require.require("1", Require.equalTo("1") ) == ""
Require.require("2", Require.equalTo("1") ) # throws IllegalArgumentException
T - The type of the object being testedval - The object to compare to - Can be null== arrpublic static <T> Predicate<T> same(T val)
== val (using pointer equality).
An object must be equal to another object using equals to satisfy the condition.
NULL's are allowed.
Example:
Require.require(null, Require.same("1") ) # throws IllegalArgumentException("Must be same object as 1")
Require.require(null, Require.same(null)) == null
Require.require("", Require.same(null)) # throws IllegalArgumentException("Must be same object as null")
Require.require("1", Require.same("1") ) == ""
Require.require("2", Require.same("1") ) # throws IllegalArgumentException("Must be same object as 1")
Foo foo = new Foo();
Foo bar = new Foo();
Require.require(foo, Require.same(bar) # throws IllegalArgumentException("Must be same object as ...")
T - The type of the object being testedval - The object to compare to - Can be null== valpublic static <T> Predicate<T> and(Predicate<T> p1, Predicate<T> p2)
p1 and p2.
An object must match 2 predicates.
NULL's are allowed.
Example:
Require.require(1, Require.and(Equal.gt(0), Equal.lt(2))) == 1
Require.require(1, Require.and(Equal.gt(1), Equal.lt(2))) # throws IllegalArgumentException("(Must be greater than 1) and (Must be less than 2)")
Require.require(1, Require.and(Equal.gt(0), Equal.lt(0))) # throws IllegalArgumentException("(Must be greater than 0) and (Must be less than 0)")
T - the type of the object being testedp1 - The first predicate - Can't be null - always evaluatedp2 - The second predicate - Can't be null - conditionally evaluatedp1 and p2public static <T> Predicate<T> or(Predicate<T> p1, Predicate<T> p2)
p1 and p2.
An object must match one of two predicates.
NULL's are allowed.
Example:
Require.require(1, Require.or(Equal.gt(0), Equal.lt(2))) == 1
Require.require(1, Require.or(Equal.gt(1), Equal.lt(2))) == 1
Require.require(1, Require.or(Equal.gt(0), Equal.lt(0))) == 1
Require.require(1, Require.or(Equal.gt(1), Equal.gt(3))) # throws IllegalArgumentException("(Must be greater than 1) or (Must be less than 3)")
T - the type of the object being testedp1 - The first predicate - Can't be null - always evaluatedp2 - The second predicate - Can't be null - conditionally evaluatedp1 and p2public static <T> Predicate<T> negate(Predicate<T> p)
p.
An object must match not match a sub predicate.
NULL's are allowed.
Example:
Require.require(1, Require.not(Equal.gt(0))) # throws IllegalArgumentException("not (Must be greater than 0)")
Require.require(1, Require.not(Equal.gt(1))) == 1
T - the type of the object being testedp - The predicate to negate - Can't be nullp.public static <T> Predicate<T> name(Predicate<T> pred, Supplier<String> msg)
pred.
This is the mechanism by which we can associate strings
with predicates, giving nice messages when they fail.
Example:
Require.name(someobj -> true, () -> "True").toString() == "True"
T - The type of the object being testedpred - The predicate to decorate with a message - Can't be nullmsg - The supplier of the message - Can't be nullpublic static <K,V,T extends Map<? extends K,? extends V>> Function<T,Collection<? extends V>> values()
Example:
Require.values().apply({a => 1, b => 2}) == [1, 2]
Require.values().toString() == "values"
K - The Map key typeV - The Map value typeT - The Map typepublic static <K,V,T extends Map<? extends K,? extends V>> Function<T,Set<? extends K>> keySet()
Example:
Require.values().apply({a => 1, b =>2}) == [1, 2]
Require.values().toString() == "keys"
K - The Map key typeV - The Map value typeT - The Map typepublic static <K,V,T extends Map<? extends K,? extends V>> Function<T,Integer> mapSize()
Example:
Require.mapSize().apply({a => 1, b => 2}) == 2
Require.mapSize().toString() == "size"
K - The Map key typeV - The Map value typeT - The Map typepublic static <T> Function<T,String> stringify()
Example:
Require.stringify().apply(3) == "3"
Require.stringify().toString() == "toString"
T - the type of the object being operated onpublic static <T> Function<T,Integer> hash()
Example:
Require.hash().apply(o) == o.hashCode()
Require.hash().toString() == "hash"
T - the type of the object being operated onpublic static Function<String,Integer> length()
Example:
Require.length().apply("abc") ==length3
Require.length().toString() == "hash"
public static <E,T extends Collection<? extends E>> Function<T,Integer> size()
Example:
Require.size().apply([4,5]) == 2
Require.size().toString() == "size"
E - the type for the element of the collectionT - the type of the object being operated onpublic static <T,U> Function<T,U> nameF(Function<T,U> f, Supplier<String> msg)
Example:
Require.nameF(String::length, () -> "Length").toString() == "Length"
T - The type being operated on by the functionU - The return type of the functionf - The function to decorate with a message - Can't be nullmsg - The supplier of the message - Can't be nullpublic static <T,U> Predicate<T> chain(Function<T,U> f, Predicate<U> p)
f(o).
A function of an object must match match a sub predicate.
NULL's are allowed, but only if the function allows null, so care needs to
be taken here.
Example:
Require.require("abc", Require.chain(Require.length(), Equal.gt(2)) == "abc"
Require.require("ab", Require.chain(Require.length(), Equal.gt(2)) # throws IllegalArgumentException("length: Must be greater than 0")
If f throws an exception, it will be passed on to the caller.T - The type of the object being testedU - The type of the object returned by ff - The function - Can't be nullp - The predicate of the return value - Can't be nullf(o)Copyright © 2020. All rights reserved.