TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
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
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09