1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of 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 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.base; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.ArrayList; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List; 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Static utility methods pertaining to {@code Predicate} instances. 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>All methods returns serializable predicates as long as they're given 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * serializable parameters. 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Predicates { 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Predicates() {} 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): considering having these implement a VisitablePredicate 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // interface which specifies an accept(PredicateVisitor) method. 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that always evaluates to {@code true}. 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @GwtCompatible(serializable = true) 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> alwaysTrue() { 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ObjectPredicate.ALWAYS_TRUE.withNarrowedType(); 53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that always evaluates to {@code false}. 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @GwtCompatible(serializable = true) 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> alwaysFalse() { 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ObjectPredicate.ALWAYS_FALSE.withNarrowedType(); 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if the object reference 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * being tested is null. 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtCompatible(serializable = true) 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> isNull() { 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ObjectPredicate.IS_NULL.withNarrowedType(); 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if the object reference 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * being tested is not null. 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtCompatible(serializable = true) 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> notNull() { 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ObjectPredicate.NOT_NULL.withNarrowedType(); 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if the given predicate 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * evaluates to {@code false}. 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> not(Predicate<T> predicate) { 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new NotPredicate<T>(predicate); 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if each of its 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluates to {@code true}. The components are evaluated in 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * order, and evaluation will be "short-circuited" as soon as a false 93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate is found. It defensively copies the iterable passed in, so future 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * changes to it won't alter the behavior of this predicate. If {@code 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components} is empty, the returned predicate will always evaluate to {@code 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * true}. 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> and( 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends Predicate<? super T>> components) { 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new AndPredicate<T>(defensiveCopy(components)); 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if each of its 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluates to {@code true}. The components are evaluated in 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * order, and evaluation will be "short-circuited" as soon as a false 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate is found. It defensively copies the array passed in, so future 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * changes to it won't alter the behavior of this predicate. If {@code 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components} is empty, the returned predicate will always evaluate to {@code 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * true}. 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> and(Predicate<? super T>... components) { 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new AndPredicate<T>(defensiveCopy(components)); 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if both of its 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluate to {@code true}. The components are evaluated in 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * order, and evaluation will be "short-circuited" as soon as a false 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate is found. 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> and(Predicate<? super T> first, 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Predicate<? super T> second) { 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new AndPredicate<T>(Predicates.<T>asList( 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(first), checkNotNull(second))); 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if any one of its 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluates to {@code true}. The components are evaluated in 1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * order, and evaluation will be "short-circuited" as soon as a 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * true predicate is found. It defensively copies the iterable passed in, so 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * future changes to it won't alter the behavior of this predicate. If {@code 134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components} is empty, the returned predicate will always evaluate to {@code 135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * false}. 136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> or( 138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends Predicate<? super T>> components) { 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new OrPredicate<T>(defensiveCopy(components)); 140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if any one of its 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluates to {@code true}. The components are evaluated in 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * order, and evaluation will be "short-circuited" as soon as a 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * true predicate is found. It defensively copies the array passed in, so 147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * future changes to it won't alter the behavior of this predicate. If {@code 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components} is empty, the returned predicate will always evaluate to {@code 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * false}. 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> or(Predicate<? super T>... components) { 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new OrPredicate<T>(defensiveCopy(components)); 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if either of its 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * components evaluates to {@code true}. The components are evaluated in 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * order, and evaluation will be "short-circuited" as soon as a 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * true predicate is found. 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Predicate<T> or( 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> first, Predicate<? super T> second) { 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new OrPredicate<T>(Predicates.<T>asList( 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(first), checkNotNull(second))); 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if the object being 169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * tested {@code equals()} the given target or both are null. 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> equalTo(@Nullable T target) { 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (target == null) 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? Predicates.<T>isNull() 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : new IsEqualToPredicate<T>(target); 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a predicate that evaluates to {@code true} if the object reference 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * being tested is a member of the given collection. It does not defensively 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * copy the collection passed in, so future changes to it will alter the 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * behavior of the predicate. 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This method can technically accept any {@code Collection<?>}, but using 1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * a typed collection helps prevent bugs. This approach doesn't block any 1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * potential users since it is always possible to use {@code 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Predicates.<Object>in()}. 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param target the collection that may contain the function input 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Predicate<T> in(Collection<? extends T> target) { 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new InPredicate<T>(target); 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the composition of a function and a predicate. For every {@code x}, 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the generated predicate returns {@code predicate(function(x))}. 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the composition of the provided function and predicate 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <A, B> Predicate<A> compose( 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Predicate<B> predicate, Function<A, ? extends B> function) { 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new CompositionPredicate<A, B>(predicate, function); 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // End public API, begin private implementation classes. 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Package private for GWT serialization. 2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert enum ObjectPredicate implements Predicate<Object> { 2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ALWAYS_TRUE { 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean apply(@Nullable Object o) { 2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return true; 2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }, 2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ALWAYS_FALSE { 2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean apply(@Nullable Object o) { 2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return false; 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }, 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert IS_NULL { 2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean apply(@Nullable Object o) { 2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return o == null; 2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }, 2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert NOT_NULL { 2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean apply(@Nullable Object o) { 2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return o != null; 2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @SuppressWarnings("unchecked") // these Object predicates work for any T 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert <T> Predicate<T> withNarrowedType() { 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (Predicate<T>) this; 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#not(Predicate) */ 2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class NotPredicate<T> implements Predicate<T>, Serializable { 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Predicate<T> predicate; 239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert NotPredicate(Predicate<T> predicate) { 241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.predicate = checkNotNull(predicate); 242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(T t) { 245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return !predicate.apply(t); 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ~predicate.hashCode(); 249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof NotPredicate) { 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson NotPredicate<?> that = (NotPredicate<?>) obj; 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.equals(that.predicate); 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "Not(" + predicate.toString() + ")"; 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final Joiner COMMA_JOINER = Joiner.on(","); 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#and(Iterable) */ 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class AndPredicate<T> implements Predicate<T>, Serializable { 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final List<? extends Predicate<? super T>> components; 268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private AndPredicate(List<? extends Predicate<? super T>> components) { 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.components = components; 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(T t) { 2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int i = 0; i < components.size(); i++) { 2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!components.get(i).apply(t)) { 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // 0x12472c2c is a random number to help avoid collisions with OrPredicate 2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return components.hashCode() + 0x12472c2c; 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof AndPredicate) { 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson AndPredicate<?> that = (AndPredicate<?>) obj; 2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return components.equals(that.components); 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return "And(" + COMMA_JOINER.join(components) + ")"; 294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#or(Iterable) */ 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class OrPredicate<T> implements Predicate<T>, Serializable { 3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final List<? extends Predicate<? super T>> components; 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private OrPredicate(List<? extends Predicate<? super T>> components) { 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.components = components; 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(T t) { 3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int i = 0; i < components.size(); i++) { 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (components.get(i).apply(t)) { 309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // 0x053c91cf is a random number to help avoid collisions with AndPredicate 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return components.hashCode() + 0x053c91cf; 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof OrPredicate) { 320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson OrPredicate<?> that = (OrPredicate<?>) obj; 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return components.equals(that.components); 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return "Or(" + COMMA_JOINER.join(components) + ")"; 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#equalTo(Object) */ 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class IsEqualToPredicate<T> 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson implements Predicate<T>, Serializable { 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final T target; 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private IsEqualToPredicate(T target) { 337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.target = target; 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(T t) { 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.equals(t); 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.hashCode(); 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof IsEqualToPredicate) { 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson IsEqualToPredicate<?> that = (IsEqualToPredicate<?>) obj; 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.equals(that.target); 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "IsEqualTo(" + target + ")"; 355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#in(Collection) */ 3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class InPredicate<T> implements Predicate<T>, Serializable { 361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Collection<?> target; 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private InPredicate(Collection<?> target) { 364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.target = checkNotNull(target); 365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(T t) { 369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson try { 370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.contains(t); 371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (NullPointerException e) { 372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (ClassCastException e) { 374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 3781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof InPredicate) { 380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson InPredicate<?> that = (InPredicate<?>) obj; 381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.equals(that.target); 382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return target.hashCode(); 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "In(" + target + ")"; 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Predicates#compose(Predicate, Function) */ 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class CompositionPredicate<A, B> 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson implements Predicate<A>, Serializable { 399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Predicate<B> p; 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Function<A, ? extends B> f; 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private CompositionPredicate(Predicate<B> p, Function<A, ? extends B> f) { 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.p = checkNotNull(p); 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.f = checkNotNull(f); 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean apply(A a) { 409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return p.apply(f.apply(a)); 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 4131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof CompositionPredicate) { 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson CompositionPredicate<?, ?> that = (CompositionPredicate<?, ?>) obj; 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.equals(that.f) && p.equals(that.p); 416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.hashCode() ^ p.hashCode(); 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return p.toString() + "(" + f.toString() + ")"; 426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static <T> List<Predicate<? super T>> asList( 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Predicate<? super T> first, Predicate<? super T> second) { 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Arrays.<Predicate<? super T>>asList(first, second); 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static <T> List<T> defensiveCopy(T... array) { 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return defensiveCopy(Arrays.asList(array)); 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static <T> List<T> defensiveCopy(Iterable<T> iterable) { 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ArrayList<T> list = new ArrayList<T>(); 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (T element : iterable) { 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson list.add(checkNotNull(element)); 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return list; 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 4491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 450