Discussion:
[collections] Question on the signature of the Transformed Predicate Transformer
(too old to reply)
Steven Geens
2018-05-04 07:39:15 UTC
Permalink
Hi,

I was trying to use the transformed predicate:
Javadoc: Creates a predicate that transforms the input object before
passing it to the predicate.
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/PredicateUtils.html#transformedPredicate(org.apache.commons.collections4.Transformer,%20org.apache.commons.collections4.Predicate)

I noticed the signature:
public static <T> Predicate<T> transformedPredicate(Transformer<? super T,?
extends T> transformer, Predicate<? super T> predicate)

I was actually expecting to see the following:
public static <T,O> Predicate<T> transformedPredicate(Transformer<? super
T,? extends O> transformer, Predicate<? super O> predicate)

I do not see why the type of the output of the transformer should be in any
way related to T, the type of input for the transformer.
It seems to artificially limit the possible types of the transformer and
the given predicate to types related with T.

I've added an example java file with my expected behaviour.
Most important excerpt:
public Collection<Seat> seatsToAvoid(Collection<Seat> seats,
Transformer<Seat, Passenger> seatingArrangement,
Predicate<Passenger> hasEatenBeans) {
Predicate<Seat> badSeat1 =
PredicateUtils.transformedPredicate(seatingArrangement, hasEatenBeans);
Predicate<Seat> badSeat2 =
expectedTransformedPredicate(seatingArrangement, hasEatenBeans);
return CollectionUtils.select(seats, badSeat2);
}
The line with badseat1 does not compile.

Could you clarify why this predicate should have this signature and why my
expected behaviour is incorrect?

Steven

Loading...