fengshao0907 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Predicates.forFunction(Function<T, Boolean>) #19

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
  /**
   * Returns a predicate that evaluates to the same result as the
   * given function.
   */
  public static <T> Predicate<T> forFunction(
      final Function<T, Boolean> predicate) {
    checkNotNull(predicate);
    return new FunctionPredicate<T>(predicate);
  }

  /** @see Predicates#forFunction(Function) */
  private static class FunctionPredicate<T>
      implements Predicate<T>, Serializable {
    private final Function<T, Boolean> function;

    private FunctionPredicate(final Function<T, Boolean> function) {
      this.function = function;
    }

    public boolean apply(final T t) {
      return Boolean.TRUE.equals(function.apply(t));
    }

    private static final long serialVersionUID = -4940925077935486606L;
  }

Usage examples:
  private static final Predicate<Object> ALWAYS_TRUE =
      forFunction(Functions.constant(Boolean.TRUE));

  private static final Predicate<Object> ALWAYS_FALSE =
      forFunction(Functions.constant(Boolean.FALSE));

Original issue reported on code.google.com by jonhnnyw...@gmail.com on 23 Oct 2007 at 7:06

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 27 May 2008 at 6:29

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 18 Mar 2009 at 2:20

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 6:02

GoogleCodeExporter commented 9 years ago
Does anyone actually need this, and why?

Original comment by kevinb@google.com on 23 Apr 2010 at 8:16

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 Jul 2010 at 3:53

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 Jul 2010 at 3:56

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 26 Jan 2011 at 8:47

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:10