Java Functional Programming : 5. Predicate
This is the multi part series on Java Functional Programming
5. Predicate
The Predicate is used for making a Boolean check. Apart from the abstract method test it has 1 static and 3 default methods.
Category
|
Predicate
|
Interface Definition
|
Predicate<T>
|
Abstract Method
|
boolean test(T t)
|
Static Method
|
<T> Predicate<T> isEqual(Object targetRef)
|
Default Method 1
|
Predicate<T> and(Predicate<? super T> other)
|
Default Method 2
|
Predicate<T> or(Predicate<? super T> other)
|
Default Method 3
|
Predicate<T> negate( )
|
Let us first
see how the body of each method looks. The three default methods – and,
or
and negate
actually represent the logical checks - AND,
OR and NOT. Since these return a Predicate these can be chained too
just like the Consumer we saw earlier. It is not a good idea to chain too
many operators as they may add to confusion. The logical step should be to add
intermediate predicates and evolve the operation step by step by combing two or
three predicates at a time.
default
Predicate<T> and(Predicate<? super T> other){
Objects.requireNonNull(other);
return (t) ->
test(t) && other.test(t);
}
default
Predicate<T> or(Predicate<? super T> other){
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
default
Predicate<T> negate(){
return (t) -> !test(t);
}
static
<T> Predicate<T> isEqual(Object targetRef){
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
|
The method and
does a logical AND operation between
two predicates. The methods negate and or do the logical
operations as their name suggests. The beauty is that they can be joined as a
chain.
Chair chair =
Chair.createChair("My
Brand", 200, true);
Predicate<Chair>
isExpensive = (c) -> c.getPrice()
> 100;
Predicate<Chair>
isCushioned = (c) -> c.isWithCushion();
// is
Expensive AND Cushioned?
boolean isExpAndCushioned = isExpensive.and(isCushioned).test(chair);
System.out.println("isExpAndCushioned=" + isExpAndCushioned);
// is NOT
expensive
boolean isNotExpensive = isExpensive.negate().test(chair);
System.out.println("isNotExpensive=" + isNotExpensive);
// is NOT
expensive OR NOT cushioned
boolean isNotExOrNotCush = isExpensive.negate().or(isCushioned.negate()).test(chair);
System.out.println("isNotExOrNotCush=" + isNotExOrNotCush);
// the
isEqual static method gives back a predicate
// this can
be used to test equality with other Chair
Chair chair2 = Chair.createChair("My Other Brand", 200, true);
Predicate<Chair>
isEqual = Predicate.isEqual(chair2);
boolean isSame2 = isEqual.test(chair);
System.out.println("isSame
Chair2=" + isSame2);
Chair chair3 = Chair.createChair("Brand 3", 200, false);
boolean isSame3 = isEqual.test(chair3);
System.out.println("isSame Chair3=" + isSame3);
|
This is what
gets printed when we execute the above code.
Chair Created
... !!
isExpAndCushioned=true
isNotExpensive=false
isNotExOrNotCush=false
Chair Created
... !!
isSame
Chair2=true
Chair Created
... !!
isSame Chair3=false
Check that
the isEqual
actually invokes equals method defined in the Chair class.
<< Prev (Consumer) | Next (Function) >>
<< Prev (Consumer) | Next (Function) >>
Comments
Post a Comment