Class If<I>
- java.lang.Object
-
- uk.gov.gchq.koryphe.predicate.KoryphePredicate<I>
-
- uk.gov.gchq.koryphe.impl.predicate.If<I>
-
- Type Parameters:
I- the type of input to be validated
- All Implemented Interfaces:
Predicate<I>
public class If<I> extends KoryphePredicate<I>
AnIfis aPredicatethat conditionally applies one of two predicates to a provided input.Note that the
Ifhas both a number of constructors as well as aBuilder. The use case for constructors would generally be for testing a single input.The use case for the Builder allows greater flexibility, mainly for allowing multiple inputs such as an Array of objects, and control over which of these objects is tested by each predicate.
For example, Given an input array of 3 objects, one may wish to test the first object in the array against the initial predicate, then pass both the second and third objects to the resulting predicate, based on the outcome of the initial test. This would require use of the
This would look something like:Builder, passing a selection of 0 along with the first predicate, and a selection of 1, 2 with the other predicates.final If ifPredicate = new If.SelectedBuilder() .predicate(firstPredicate, 0) .then(thenPredicate, 1, 2) .otherwise(otherwisePredicate, 1, 2) .build();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIf.SelectedBuilder
-
Constructor Summary
Constructors Constructor Description If()If(boolean condition, Predicate<? super I> then)Constructs a newIfobject, with a boolean condition, and one predicate that will test the input, if the boolean condition resolves to true.If(boolean condition, Predicate<? super I> then, Predicate<? super I> otherwise)Constructs a newIfobject, with a boolean condition, one predicate to test the input if the condition is true, and another predicate to test the input if the condition is false.If(Predicate<? super I> predicate, Predicate<? super I> then)Constructs a newIfobject, with an initialpredicate, and another predicate to test the input if the initial predicate resolves to true.If(Predicate<? super I> predicate, Predicate<? super I> then, Predicate<? super I> otherwise)Constructs a newIfobject, with an initialpredicate, a second predicate to test the input if the initial predicate resolves to true, and a third predicate to test the input if the initial predicate resolves to false.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)BooleangetCondition()Predicate<? super I>getOtherwise()Predicate<? super I>getPredicate()Predicate<? super I>getThen()inthashCode()voidsetCondition(boolean condition)voidsetOtherwise(Predicate<? super I> otherwise)voidsetPredicate(Predicate<? super I> predicate)voidsetThen(Predicate<? super I> then)booleantest(I input)If the condition is not being used or has not been set, then the provided predicate will test the input (assuming it is also not null).StringtoString()
-
-
-
Constructor Detail
-
If
public If()
-
If
public If(boolean condition, Predicate<? super I> then)Constructs a newIfobject, with a boolean condition, and one predicate that will test the input, if the boolean condition resolves to true.- Parameters:
condition- a boolean conditionthen- the predicate to apply if true
-
If
public If(boolean condition, Predicate<? super I> then, Predicate<? super I> otherwise)Constructs a newIfobject, with a boolean condition, one predicate to test the input if the condition is true, and another predicate to test the input if the condition is false.- Parameters:
condition- a boolean conditionthen- the predicate to apply if trueotherwise- the predicate to apply if false
-
If
public If(Predicate<? super I> predicate, Predicate<? super I> then)
Constructs a newIfobject, with an initialpredicate, and another predicate to test the input if the initial predicate resolves to true.- Parameters:
predicate- the initial predicate applied to the inputthen- the predicate to apply if true
-
If
public If(Predicate<? super I> predicate, Predicate<? super I> then, Predicate<? super I> otherwise)
Constructs a newIfobject, with an initialpredicate, a second predicate to test the input if the initial predicate resolves to true, and a third predicate to test the input if the initial predicate resolves to false.- Parameters:
predicate- the initial predicate applied to the inputthen- the predicate to apply if trueotherwise- the predicate to apply if false
-
-
Method Detail
-
test
public boolean test(I input)
If the condition is not being used or has not been set, then the provided predicate will test the input (assuming it is also not null). If this resolves to true, thethenpredicate will test the input, else theotherwisepredicate will be used. The result of either of these being applied to the input is finally returned.- Parameters:
input- the input to be tested- Returns:
- true if the input passes the predicate, otherwise false
-
getCondition
public Boolean getCondition()
-
setCondition
public void setCondition(boolean condition)
-
equals
public boolean equals(Object obj)
- Overrides:
equalsin classKoryphePredicate<I>
-
hashCode
public int hashCode()
- Overrides:
hashCodein classKoryphePredicate<I>
-
-