java.lang.Object
org.klojang.check.relation.Compose
Utility methods that assist in the creation of new checks by combining multiple
individual checks. Note that while the predicates in the
CommonChecks
class are, in fact, already either a ComposablePredicate
or a
ComposableIntPredicate
, the relational checks obviously are not.
Handwritten lambdas and method references (for example: i -> i % 3 == 0
)
neither are a ComposablePredicate
or ComposableIntPredicate
in
and of themselves. The utility methods defined in this class make sure a
composition can start with a Relation
, lambda or method reference.- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> ComposablePredicate
<T> invalid()
Returns aComposablePredicate
that always evaluates tofalse
.static <T> ComposablePredicate
<T> invalidIf
(T value) Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.static ComposableIntPredicate
Returns aComposableIntPredicate
that always evaluates tofalse
.static ComposableIntPredicate
invalidIntIf
(int value) Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.static <T> ComposablePredicate
<T> valid()
Returns aComposablePredicate
that always evaluates totrue
.static <T> ComposablePredicate
<T> validIf
(T value) Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.static ComposableIntPredicate
validInt()
Returns aComposableIntPredicate
that always evaluates totrue
.static ComposableIntPredicate
validIntIf
(int value) Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.static ComposableIntPredicate
validIntWhen
(IntPredicate test) Converts anIntPredicate
to aComposableIntPredicate
.static <O> ComposableIntPredicate
validIntWhen
(IntObjRelation<O> relation, O object) Converts anIntObjRelation
to aComposableIntPredicate
.static ComposableIntPredicate
validIntWhen
(IntRelation relation, int object) Converts anRelation
to aComposableIntPredicate
.static <T> ComposablePredicate
<T> Converts aPredicate
to aComposablePredicate
.static <S,
O> ComposablePredicate <S> Converts aRelation
to aComposablePredicate
.
-
Method Details
-
valid
Returns aComposablePredicate
that always evaluates totrue
. Can be used as the first of a series of AND-joined checks if there is no need for an initial notNull() null check.Check.that(color).is(valid().and(equalTo(), noneOf(), GREEN, BLUE, YELLOW));
- Type Parameters:
T
- the type of the value being tested (which is ignored by the returnedComposablePredicate
)- Returns:
- a
ComposablePredicate
that always evaluates totrue
-
validInt
Returns aComposableIntPredicate
that always evaluates totrue
. Can be used as the first of a series of AND-joined checks.- Returns:
- a
ComposableIntPredicate
that always evaluates totrue
-
invalid
Returns aComposablePredicate
that always evaluates tofalse
. Can be used as the first of a series of OR-joined checks if there is no need for an initial notNull() null check.Check.that(color).is(invalid().or(equalTo(), anyOf(), GREEN, BLUE, YELLOW));
- Type Parameters:
T
- the type of the value being tested (which is ignored by the returnedComposablePredicate
)- Returns:
- a
ComposablePredicate
that always evaluates tofalse
-
invalidInt
Returns aComposableIntPredicate
that always evaluates tofalse
. Can be used as the first of a series of OR-joined checks.- Returns:
- a
ComposableIntPredicate
that always evaluates tofalse
-
validIf
Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value. The two values are compared usingObjects.equals()
.- Type Parameters:
T
- the type of the value being tested- Parameters:
value
- the value to compare the value to be tested with- Returns:
- a
ComposablePredicate
that evaluates totrue
if the value to be tested has the specified value
-
validIntIf
Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.- Parameters:
value
- the value to compare the value to be tested with- Returns:
- a
ComposablePredicate
that evaluates totrue
if the value to be tested has the specified value
-
invalidIf
Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value. The two values are compared usingObjects.equals()
.- Type Parameters:
T
- the type of the value being tested- Parameters:
value
- the value to compare the value to be tested with- Returns:
- a
ComposablePredicate
that evaluates totrue
if the value to be tested has the specified value
-
invalidIntIf
Returns aComposablePredicate
that evaluates totrue
if the value to be tested has the specified value.- Parameters:
value
- the value to compare the value to be tested with- Returns:
- a
ComposablePredicate
that evaluates totrue
if the value to be tested has the specified value
-
validWhen
Converts aPredicate
to aComposablePredicate
. This method can be used to convert a predefinedPredicate
constant from outside Klojang Check to aComposablePredicate
, or to hard-cast a lambda or method reference to aComposablePredicate
. This method is only needed if thePredicate
, lambda or method reference must be the first test of the composition.Check.that(sentence).is(validIf((String s) -> s.contains("to")) .orElse((String s) -> s.contains("be")); .orElse((String s) -> s.contains("or")); .orElse((String s) -> s.contains("not")));
- Type Parameters:
T
- the type of the value being tested- Parameters:
test
- thePredicate
to convert- Returns:
- the equivalent
ComposablePredicate
-
validIntWhen
Converts anIntPredicate
to aComposableIntPredicate
. This method can be used to convert a predefinedIntPredicate
constant from outside Klojang Check to aComposableIntPredicate
, or to hard-cast a lambda or method reference. This method is only needed if theIntPredicate
, lambda or method reference must be the first test of the composition.- Parameters:
test
- theIntPredicate
to convert- Returns:
- the equivalent
ComposableIntPredicate
-
validWhen
Converts aRelation
to aComposablePredicate
. More precisely: this method returns aComposablePredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value. This method is only needed if theRelation
must be the first test of the composition.Check.that(Year.now()).is(validIf(GT(), Year.of(2000)) .andAlso(LT(), Year.of(3000));
- Type Parameters:
S
- the type of the subject of the relationO
- the type of the object of the relation- Parameters:
relation
- the relationship test to executeobject
- the object of the relation- Returns:
- a
ComposablePredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value
-
validIntWhen
Converts anIntObjRelation
to aComposableIntPredicate
. More precisely: this method returns aComposableIntPredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value. This method is only needed if theIntObjRelation
must be the first test of the composition.- Type Parameters:
O
- the type of the object of the relation- Parameters:
relation
- the relationship test to executeobject
- the object of the relation- Returns:
- a
ComposableIntPredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value
-
validIntWhen
Converts anRelation
to aComposableIntPredicate
. More precisely: this method returns aComposableIntPredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value. This method is only needed if theIntRelation
must be the first test of the composition.- Parameters:
relation
- the relationship test to executeobject
- the object of the relation- Returns:
- a
ComposableIntPredicate
that evaluates totrue
if the value being tested has the specified relation to the specified value
-