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 aComposablePredicatethat always evaluates tofalse.static <T> ComposablePredicate<T> invalidIf(T value) Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.static ComposableIntPredicateReturns aComposableIntPredicatethat always evaluates tofalse.static ComposableIntPredicateinvalidIntIf(int value) Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.static <T> ComposablePredicate<T> valid()Returns aComposablePredicatethat always evaluates totrue.static <T> ComposablePredicate<T> validIf(T value) Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.static ComposableIntPredicatevalidInt()Returns aComposableIntPredicatethat always evaluates totrue.static ComposableIntPredicatevalidIntIf(int value) Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.static ComposableIntPredicatevalidIntWhen(IntPredicate test) Converts anIntPredicateto aComposableIntPredicate.static <O> ComposableIntPredicatevalidIntWhen(IntObjRelation<O> relation, O object) Converts anIntObjRelationto aComposableIntPredicate.static ComposableIntPredicatevalidIntWhen(IntRelation relation, int object) Converts anRelationto aComposableIntPredicate.static <T> ComposablePredicate<T> Converts aPredicateto aComposablePredicate.static <S,O> ComposablePredicate <S> Converts aRelationto aComposablePredicate.
-
Method Details
-
valid
Returns aComposablePredicatethat 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
ComposablePredicatethat always evaluates totrue
-
validInt
Returns aComposableIntPredicatethat always evaluates totrue. Can be used as the first of a series of AND-joined checks.- Returns:
- a
ComposableIntPredicatethat always evaluates totrue
-
invalid
Returns aComposablePredicatethat 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
ComposablePredicatethat always evaluates tofalse
-
invalidInt
Returns aComposableIntPredicatethat always evaluates tofalse. Can be used as the first of a series of OR-joined checks.- Returns:
- a
ComposableIntPredicatethat always evaluates tofalse
-
validIf
Returns aComposablePredicatethat evaluates totrueif 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
ComposablePredicatethat evaluates totrueif the value to be tested has the specified value
-
validIntIf
Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.- Parameters:
value- the value to compare the value to be tested with- Returns:
- a
ComposablePredicatethat evaluates totrueif the value to be tested has the specified value
-
invalidIf
Returns aComposablePredicatethat evaluates totrueif 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
ComposablePredicatethat evaluates totrueif the value to be tested has the specified value
-
invalidIntIf
Returns aComposablePredicatethat evaluates totrueif the value to be tested has the specified value.- Parameters:
value- the value to compare the value to be tested with- Returns:
- a
ComposablePredicatethat evaluates totrueif the value to be tested has the specified value
-
validWhen
Converts aPredicateto aComposablePredicate. This method can be used to convert a predefinedPredicateconstant 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- thePredicateto convert- Returns:
- the equivalent
ComposablePredicate
-
validIntWhen
Converts anIntPredicateto aComposableIntPredicate. This method can be used to convert a predefinedIntPredicateconstant 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- theIntPredicateto convert- Returns:
- the equivalent
ComposableIntPredicate
-
validWhen
Converts aRelationto aComposablePredicate. More precisely: this method returns aComposablePredicatethat evaluates totrueif the value being tested has the specified relation to the specified value. This method is only needed if theRelationmust 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
ComposablePredicatethat evaluates totrueif the value being tested has the specified relation to the specified value
-
validIntWhen
Converts anIntObjRelationto aComposableIntPredicate. More precisely: this method returns aComposableIntPredicatethat evaluates totrueif the value being tested has the specified relation to the specified value. This method is only needed if theIntObjRelationmust 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
ComposableIntPredicatethat evaluates totrueif the value being tested has the specified relation to the specified value
-
validIntWhen
Converts anRelationto aComposableIntPredicate. More precisely: this method returns aComposableIntPredicatethat evaluates totrueif the value being tested has the specified relation to the specified value. This method is only needed if theIntRelationmust be the first test of the composition.- Parameters:
relation- the relationship test to executeobject- the object of the relation- Returns:
- a
ComposableIntPredicatethat evaluates totrueif the value being tested has the specified relation to the specified value
-