Closed GoogleCodeExporter closed 9 years ago
It does seem like a gap that we don't have that.
And do we not have a method that composes a Function and a Predicate into a
Predicate? I'm surprised, I thought we had that.
Original comment by kevin...@gmail.com
on 23 Oct 2007 at 7:15
Original comment by kevin...@gmail.com
on 27 May 2008 at 6:29
I built an implementation that mirrors the Functions.forPredicate method:
public static <T> Predicate<T> forFunction(Function<T, Boolean> function) {
return new FunctionPredicate<T>(function);
}
private static class FunctionPredicate<T> implements Predicate<T>, Serializable {
private final Function<T, Boolean> function;
private FunctionPredicate(Function<T, Boolean> function) {
this.function = checkNotNull(function);
}
@Override
public boolean apply(T t) {
return function.apply(t);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FunctionPredicate) {
FunctionPredicate<?> that = (FunctionPredicate<?>) obj;
return function.equals(that.function);
}
return false;
}
@Override
public int hashCode() {
return function.hashCode();
}
@Override
public String toString() {
return "forFunction(" + function + ")";
}
private static final long serialVersionUID = 0;
}
Original comment by seantparsons
on 12 Mar 2009 at 10:24
Original comment by kevin...@gmail.com
on 18 Mar 2009 at 2:20
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 6:02
Does anyone actually need this, and why?
Original comment by kevinb@google.com
on 23 Apr 2010 at 8:16
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:53
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:56
Predicates.compose(Predicates.equalTo(true), TtoBooleanFunction);
Voila. It's also guaranteed not to NPE like the impl in comment #3 could if the
Function returned null.
Original comment by ray.j.gr...@gmail.com
on 5 Nov 2010 at 11:47
Original comment by fry@google.com
on 26 Jan 2011 at 8:47
Why is Predicate<T> not a subtype of Function<T, Boolean>? Also, for the next
big release, it would be more intuitive to rename Function to Functor.
In my own little Collections library (which is soon going to be replaced with
Guava), I created a UnaryFunctor which applies to the type of the argument and
a BinaryFunctor which applies to a UnaryFunctor. Thus, it allows a sort of
currying in Java.
Original comment by g.e.dej...@gmail.com
on 13 Mar 2011 at 10:44
Predicate returns a "boolean" primitive type, not a Boolean. That's why it
cannot subclass Function<T, Boolean>.
You may then ask "why not have Predicate return a Boolean instead?". I see
three reasons for this:
1) it would force everyone to handle null return values
2) it would force auto-boxing, which might be bad for performance
3) doing this would break backward compatibility
Finally, I think Predicate and Function are different notions, so it makes
sense to have separate interfaces for them. I very rarely need to use a
Predicate as a Function: I mainly use predicates when filtering, not when
transforming.
Original comment by nev...@gmail.com
on 13 Mar 2011 at 2:08
see issue #493 : Predicate<T> should extend Function<T, Boolean>
http://code.google.com/p/guava-libraries/issues/detail?id=493
Original comment by amer...@gmail.com
on 13 Mar 2011 at 8:04
I don't think I ever saw an answer to "Does anyone actually need this, and why?"
Original comment by kevinb@google.com
on 5 Apr 2011 at 2:42
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:16
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:10
Original issue reported on code.google.com by
jonhnnyw...@gmail.com
on 23 Oct 2007 at 7:06