1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2007 Google Inc.
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * use this file except in compliance with the License. You may obtain a copy of
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * License for the specific language governing permissions and limitations under
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.base;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtIncompatible;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.ArrayList;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Contains static factory methods for creating {@code Predicate} instances.
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>All methods returns serializable predicates as long as they're given
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * serializable parameters.
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
39bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library)
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Predicates {
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Predicates() {}
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // TODO: considering having these implement a VisitablePredicate interface
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // which specifies an accept(PredicateVisitor) method.
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that always evaluates to {@code true}.
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @GwtCompatible(serializable = true)
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> alwaysTrue() {
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Predicate<T>) AlwaysTruePredicate.INSTANCE;
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that always evaluates to {@code false}.
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @GwtCompatible(serializable = true)
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> alwaysFalse() {
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Predicate<T>) AlwaysFalsePredicate.INSTANCE;
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the object reference
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * being tested is null.
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> isNull() {
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Predicate<T>) IsNullPredicate.INSTANCE;
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the object reference
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * being tested is not null.
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> notNull() {
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Predicate<T>) NotNullPredicate.INSTANCE;
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the given predicate
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * evaluates to {@code false}.
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> not(Predicate<T> predicate) {
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new NotPredicate<T>(predicate);
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if each of its
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluates to {@code true}. The components are evaluated in
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as a false
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * predicate is found. It defensively copies the iterable passed in, so future
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * changes to it won't alter the behavior of this predicate. If {@code
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components} is empty, the returned predicate will always evaluate to {@code
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * true}.
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> and(
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterable<? extends Predicate<? super T>> components) {
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AndPredicate<T>(defensiveCopy(components));
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if each of its
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluates to {@code true}. The components are evaluated in
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as a false
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * predicate is found. It defensively copies the array passed in, so future
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * changes to it won't alter the behavior of this predicate. If {@code
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components} is empty, the returned predicate will always evaluate to {@code
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * true}.
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> and(Predicate<? super T>... components) {
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AndPredicate<T>(defensiveCopy(components));
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if both of its
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluate to {@code true}. The components are evaluated in
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as a false
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * predicate is found.
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> and(Predicate<? super T> first,
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<? super T> second) {
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AndPredicate<T>(Predicates.<T>asList(
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkNotNull(first), checkNotNull(second)));
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if any one of its
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluates to {@code true}. The components are evaluated in
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as as soon as a
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * true predicate is found. It defensively copies the iterable passed in, so
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * future changes to it won't alter the behavior of this predicate. If {@code
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components} is empty, the returned predicate will always evaluate to {@code
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * false}.
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> or(
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterable<? extends Predicate<? super T>> components) {
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new OrPredicate<T>(defensiveCopy(components));
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if any one of its
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluates to {@code true}. The components are evaluated in
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as as soon as a
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * true predicate is found. It defensively copies the array passed in, so
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * future changes to it won't alter the behavior of this predicate. If {@code
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components} is empty, the returned predicate will always evaluate to {@code
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * false}.
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> or(Predicate<? super T>... components) {
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new OrPredicate<T>(defensiveCopy(components));
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if either of its
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * components evaluates to {@code true}. The components are evaluated in
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * order, and evaluation will be "short-circuited" as soon as as soon as a
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * true predicate is found.
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> or(Predicate<? super T> first,
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<? super T> second) {
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new OrPredicate<T>(Predicates.<T>asList(
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkNotNull(first), checkNotNull(second)));
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the object being
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * tested {@code equals()} the given target or both are null.
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> equalTo(@Nullable T target) {
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // TODO: Change signature to return Predicate<Object>.
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (target == null)
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        ? Predicates.<T>isNull()
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        : new IsEqualToPredicate<T>(target);
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the object being
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * tested is an instance of the given class. If the object being tested
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * is {@code null} this predicate evaluates to {@code false}.
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>If you want to filter an {@code Iterable} to narrow its type, consider
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * using {@link com.google.common.collect.Iterables#filter(Iterable, Class)}
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in preference.
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @GwtIncompatible("Class.isInstance")
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static Predicate<Object> instanceOf(Class<?> clazz) {
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new InstanceOfPredicate(clazz);
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a predicate that evaluates to {@code true} if the object reference
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * being tested is a member of the given collection. It does not defensively
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * copy the collection passed in, so future changes to it will alter the
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * behavior of the predicate.
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * This method can technically accept any Collection<?>, but using a typed
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * collection helps prevent bugs. This approach doesn't block any potential
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * users since it is always possible to use {@code Predicates.<Object>in()}.
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param target the collection that may contain the function input
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Predicate<T> in(Collection<? extends T> target) {
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new InPredicate<T>(target);
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the composition of a function and a predicate. For every {@code x},
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * the generated predicate returns {@code predicate(function(x))}.
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the composition of the provided function and predicate
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <A, B> Predicate<A> compose(
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<B> predicate, Function<A, ? extends B> function) {
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new CompositionPredicate<A, B>(predicate, function);
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#alwaysTrue() */
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Package private for GWT serialization.
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  enum AlwaysTruePredicate implements Predicate<Object> {
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(Object o) {
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return true;
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "AlwaysTrue";
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#alwaysFalse() */
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Package private for GWT serialization.
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  enum AlwaysFalsePredicate implements Predicate<Object> {
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(Object o) {
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "AlwaysFalse";
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#not(Predicate) */
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class NotPredicate<T>
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<T>, Serializable {
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Predicate<T> predicate;
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private NotPredicate(Predicate<T> predicate) {
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.predicate = checkNotNull(predicate);
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(T t) {
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return !predicate.apply(t);
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return ~predicate.hashCode(); /* Invert all bits. */
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof NotPredicate<?>) {
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        NotPredicate<?> that = (NotPredicate<?>) obj;
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return predicate.equals(that.predicate);
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "Not(" + predicate.toString() + ")";
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final Joiner commaJoiner = Joiner.on(",");
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#and(Iterable) */
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class AndPredicate<T>
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<T>, Serializable {
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Iterable<? extends Predicate<? super T>> components;
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private AndPredicate(Iterable<? extends Predicate<? super T>> components) {
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.components = components;
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(T t) {
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (Predicate<? super T> predicate : components) {
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!predicate.apply(t)) {
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return false;
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return true;
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      int result = -1; /* Start with all bits on. */
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (Predicate<? super T> predicate : components) {
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        result &= predicate.hashCode();
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof AndPredicate<?>) {
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        AndPredicate<?> that = (AndPredicate<?>) obj;
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterableElementsEqual(components, that.components);
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "And(" + commaJoiner.join(components) + ")";
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#or(Iterable) */
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class OrPredicate<T>
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<T>, Serializable {
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Iterable<? extends Predicate<? super T>> components;
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private OrPredicate(Iterable<? extends Predicate<? super T>> components) {
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.components = components;
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(T t) {
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (Predicate<? super T> predicate : components) {
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (predicate.apply(t)) {
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return true;
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      int result = 0; /* Start with all bits off. */
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (Predicate<? super T> predicate : components) {
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        result |= predicate.hashCode();
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof OrPredicate<?>) {
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        OrPredicate<?> that = (OrPredicate<?>) obj;
339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterableElementsEqual(components, that.components);
340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "Or(" + commaJoiner.join(components) + ")";
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#equalTo(Object) */
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class IsEqualToPredicate<T>
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<T>, Serializable {
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final T target;
353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private IsEqualToPredicate(T target) {
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.target = target;
356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(T t) {
358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return target.equals(t);
359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return target.hashCode();
362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof IsEqualToPredicate) {
365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        IsEqualToPredicate<?> that = (IsEqualToPredicate<?>) obj;
366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return target.equals(that.target);
367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "IsEqualTo(" + target + ")";
372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#instanceOf(Class) */
377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class InstanceOfPredicate
378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<Object>, Serializable {
379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Class<?> clazz;
380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private InstanceOfPredicate(Class<?> clazz) {
382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.clazz = checkNotNull(clazz);
383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(Object o) {
385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Platform.isInstance(clazz, o);
386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return clazz.hashCode();
389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof InstanceOfPredicate) {
392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        InstanceOfPredicate that = (InstanceOfPredicate) obj;
393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return clazz == that.clazz;
394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "IsInstanceOf(" + clazz.getName() + ")";
399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#isNull() */
404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // enum singleton pattern
405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private enum IsNullPredicate implements Predicate<Object> {
406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(Object o) {
409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return o == null;
410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "IsNull";
413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#notNull() */
417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // enum singleton pattern
418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private enum NotNullPredicate implements Predicate<Object> {
419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(Object o) {
422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return o != null;
423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "NotNull";
426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#in(Collection) */
430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class InPredicate<T>
431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<T>, Serializable {
432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Collection<?> target;
433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private InPredicate(Collection<?> target) {
435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.target = checkNotNull(target);
436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(T t) {
439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      try {
440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return target.contains(t);
441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (NullPointerException e) {
442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (ClassCastException e) {
444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof InPredicate<?>) {
450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        InPredicate<?> that = (InPredicate<?>) obj;
451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return target.equals(that.target);
452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return target.hashCode();
458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "In(" + target + ")";
462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Predicates#compose(Predicate, Function) */
467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class CompositionPredicate<A, B>
468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Predicate<A>, Serializable {
469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Predicate<B> p;
470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Function<A, ? extends B> f;
471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private CompositionPredicate(Predicate<B> p, Function<A, ? extends B> f) {
473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.p = checkNotNull(p);
474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.f = checkNotNull(f);
475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean apply(A a) {
478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return p.apply(f.apply(a));
479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof CompositionPredicate<?, ?>) {
483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        CompositionPredicate<?, ?> that = (CompositionPredicate<?, ?>) obj;
484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return f.equals(that.f) && p.equals(that.p);
485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
487090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
489090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
490090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      /*
491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * TODO:  To leave the door open for future enhancement, this
492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * calculation should be coordinated with the hashCode() method of the
493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * corresponding composition method in Functions.  To construct the
494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * composition:
495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       *    predicate(function2(function1(x)))
496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       *
497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * There are two different ways of composing it:
498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       *    compose(predicate, compose(function2, function1))
499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       *    compose(compose(predicate, function2), function1)
500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       *
501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       * It would be nice if these could be equal.
502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson       */
503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return f.hashCode() ^ p.hashCode();
504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
505090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return p.toString() + "(" + f.toString() + ")";
508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Determines whether the two Iterables contain equal elements. More
515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * specifically, this method returns {@code true} if {@code iterable1} and
516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code iterable2} contain the same number of elements and every element of
517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code iterable1} is equal to the corresponding element of {@code
518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterable2}.
519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>This is not a general-purpose method; it assumes that the iterations
521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contain no {@code null} elements.
522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static boolean iterableElementsEqual(
524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterable<?> iterable1, Iterable<?> iterable2) {
525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Iterator<?> iterator1 = iterable1.iterator();
526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Iterator<?> iterator2 = iterable2.iterator();
527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator1.hasNext()) {
528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator2.hasNext()) {
529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator1.next().equals(iterator2.next())) {
532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return !iterator2.hasNext();
536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
538090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
539090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static <T> List<Predicate<? super T>> asList(
540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<? super T> first, Predicate<? super T> second) {
541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Arrays.<Predicate<? super T>>asList(first, second);
542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static <T> List<T> defensiveCopy(T... array) {
545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return defensiveCopy(Arrays.asList(array));
546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> List<T> defensiveCopy(Iterable<T> iterable) {
549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ArrayList<T> list = new ArrayList<T>();
550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (T element : iterable) {
551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      list.add(checkNotNull(element));
552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return list;
554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
556