001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.List;
027import java.util.regex.Pattern;
028import org.jspecify.annotations.Nullable;
029
030/**
031 * Static utility methods pertaining to {@code Predicate} instances.
032 *
033 * <p>All methods return serializable predicates as long as they're given serializable parameters.
034 *
035 * <p>See the Guava User Guide article on <a
036 * href="https://github.com/google/guava/wiki/FunctionalExplained">the use of {@code Predicate}</a>.
037 *
038 * @author Kevin Bourrillion
039 * @since 2.0
040 */
041@GwtCompatible(emulated = true)
042public final class Predicates {
043  private Predicates() {}
044
045  // TODO(kevinb): considering having these implement a VisitablePredicate
046  // interface which specifies an accept(PredicateVisitor) method.
047
048  /** Returns a predicate that always evaluates to {@code true}. */
049  @GwtCompatible(serializable = true)
050  public static <T extends @Nullable Object> Predicate<T> alwaysTrue() {
051    return ObjectPredicate.ALWAYS_TRUE.withNarrowedType();
052  }
053
054  /** Returns a predicate that always evaluates to {@code false}. */
055  @GwtCompatible(serializable = true)
056  public static <T extends @Nullable Object> Predicate<T> alwaysFalse() {
057    return ObjectPredicate.ALWAYS_FALSE.withNarrowedType();
058  }
059
060  /**
061   * Returns a predicate that evaluates to {@code true} if the object reference being tested is
062   * null.
063   */
064  @GwtCompatible(serializable = true)
065  public static <T extends @Nullable Object> Predicate<T> isNull() {
066    return ObjectPredicate.IS_NULL.withNarrowedType();
067  }
068
069  /**
070   * Returns a predicate that evaluates to {@code true} if the object reference being tested is not
071   * null.
072   */
073  @GwtCompatible(serializable = true)
074  public static <T extends @Nullable Object> Predicate<T> notNull() {
075    return ObjectPredicate.NOT_NULL.withNarrowedType();
076  }
077
078  /**
079   * Returns a predicate that evaluates to {@code true} if the given predicate evaluates to {@code
080   * false}.
081   */
082  public static <T extends @Nullable Object> Predicate<T> not(Predicate<T> predicate) {
083    return new NotPredicate<>(predicate);
084  }
085
086  /**
087   * Returns a predicate that evaluates to {@code true} if each of its components evaluates to
088   * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
089   * as soon as a false predicate is found. It defensively copies the iterable passed in, so future
090   * changes to it won't alter the behavior of this predicate. If {@code components} is empty, the
091   * returned predicate will always evaluate to {@code true}.
092   */
093  public static <T extends @Nullable Object> Predicate<T> and(
094      Iterable<? extends Predicate<? super T>> components) {
095    return new AndPredicate<>(defensiveCopy(components));
096  }
097
098  /**
099   * Returns a predicate that evaluates to {@code true} if each of its components evaluates to
100   * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
101   * as soon as a false predicate is found. It defensively copies the array passed in, so future
102   * changes to it won't alter the behavior of this predicate. If {@code components} is empty, the
103   * returned predicate will always evaluate to {@code true}.
104   */
105  @SafeVarargs
106  public static <T extends @Nullable Object> Predicate<T> and(Predicate<? super T>... components) {
107    return new AndPredicate<T>(defensiveCopy(components));
108  }
109
110  /**
111   * Returns a predicate that evaluates to {@code true} if both of its components evaluate to {@code
112   * true}. The components are evaluated in order, and evaluation will be "short-circuited" as soon
113   * as a false predicate is found.
114   */
115  public static <T extends @Nullable Object> Predicate<T> and(
116      Predicate<? super T> first, Predicate<? super T> second) {
117    return new AndPredicate<>(Predicates.<T>asList(checkNotNull(first), checkNotNull(second)));
118  }
119
120  /**
121   * Returns a predicate that evaluates to {@code true} if any one of its components evaluates to
122   * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
123   * as soon as a true predicate is found. It defensively copies the iterable passed in, so future
124   * changes to it won't alter the behavior of this predicate. If {@code components} is empty, the
125   * returned predicate will always evaluate to {@code false}.
126   */
127  public static <T extends @Nullable Object> Predicate<T> or(
128      Iterable<? extends Predicate<? super T>> components) {
129    return new OrPredicate<>(defensiveCopy(components));
130  }
131
132  /**
133   * Returns a predicate that evaluates to {@code true} if any one of its components evaluates to
134   * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
135   * as soon as a true predicate is found. It defensively copies the array passed in, so future
136   * changes to it won't alter the behavior of this predicate. If {@code components} is empty, the
137   * returned predicate will always evaluate to {@code false}.
138   */
139  @SafeVarargs
140  public static <T extends @Nullable Object> Predicate<T> or(Predicate<? super T>... components) {
141    return new OrPredicate<T>(defensiveCopy(components));
142  }
143
144  /**
145   * Returns a predicate that evaluates to {@code true} if either of its components evaluates to
146   * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
147   * as soon as a true predicate is found.
148   */
149  public static <T extends @Nullable Object> Predicate<T> or(
150      Predicate<? super T> first, Predicate<? super T> second) {
151    return new OrPredicate<>(Predicates.<T>asList(checkNotNull(first), checkNotNull(second)));
152  }
153
154  /**
155   * Returns a predicate that evaluates to {@code true} if the object being tested {@code equals()}
156   * the given target or both are null.
157   */
158  public static <T extends @Nullable Object> Predicate<T> equalTo(@ParametricNullness T target) {
159    return (target == null)
160        ? Predicates.<T>isNull()
161        : new IsEqualToPredicate(target).withNarrowedType();
162  }
163
164  /**
165   * Returns a predicate that evaluates to {@code true} if the object being tested is an instance of
166   * the given class. If the object being tested is {@code null} this predicate evaluates to {@code
167   * false}.
168   *
169   * <p>If you want to filter an {@code Iterable} to narrow its type, consider using {@link
170   * com.google.common.collect.Iterables#filter(Iterable, Class)} in preference.
171   *
172   * <p><b>Warning:</b> contrary to the typical assumptions about predicates (as documented at
173   * {@link Predicate#apply}), the returned predicate may not be <i>consistent with equals</i>. For
174   * example, {@code instanceOf(ArrayList.class)} will yield different results for the two equal
175   * instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}.
176   */
177  @GwtIncompatible // Class.isInstance
178  public static <T extends @Nullable Object> Predicate<T> instanceOf(Class<?> clazz) {
179    return new InstanceOfPredicate<>(clazz);
180  }
181
182  /**
183   * Returns a predicate that evaluates to {@code true} if the class being tested is assignable to
184   * (is a subtype of) {@code clazz}. Example:
185   *
186   * {@snippet :
187   * List<Class<?>> classes = Arrays.asList(
188   *     Object.class, String.class, Number.class, Long.class);
189   * return Iterables.filter(classes, subtypeOf(Number.class));
190   * }
191   *
192   * The code above returns an iterable containing {@code Number.class} and {@code Long.class}.
193   *
194   * @since 20.0 (since 10.0 under the incorrect name {@code assignableFrom})
195   */
196  @J2ktIncompatible
197  @GwtIncompatible // Class.isAssignableFrom
198  public static Predicate<Class<?>> subtypeOf(Class<?> clazz) {
199    return new SubtypeOfPredicate(clazz);
200  }
201
202  /**
203   * Returns a predicate that evaluates to {@code true} if the object reference being tested is a
204   * member of the given collection. It does not defensively copy the collection passed in, so
205   * future changes to it will alter the behavior of the predicate.
206   *
207   * <p>This method can technically accept any {@code Collection<?>}, but using a typed collection
208   * helps prevent bugs. This approach doesn't block any potential users since it is always possible
209   * to use {@code Predicates.<Object>in()}.
210   *
211   * <p>You may prefer to use a method reference (e.g., {@code target::contains}) instead of this
212   * method. However, there are some subtle considerations:
213   *
214   * <ul>
215   *   <li>The {@link Predicate} returned by this method is {@link Serializable}.
216   *   <li>The {@link Predicate} returned by this method catches {@link ClassCastException} and
217   *       {@link NullPointerException}.
218   *   <li>Code that chains multiple predicates together (especially negations) may be more readable
219   *       using this method. For example, {@code not(in(target))} is generally more readable than
220   *       {@code not(target::contains)}.
221   *   <li>This method's name conflicts with Kotlin's {@code in} operator.
222   * </ul>
223   *
224   * @param target the collection that may contain the function input
225   */
226  @SuppressWarnings("NoHardKeywords") // We're stuck with the name for compatibility reasons.
227  public static <T extends @Nullable Object> Predicate<T> in(Collection<? extends T> target) {
228    return new InPredicate<>(target);
229  }
230
231  /**
232   * Returns the composition of a function and a predicate. For every {@code x}, the generated
233   * predicate returns {@code predicate(function(x))}.
234   *
235   * @return the composition of the provided function and predicate
236   */
237  public static <A extends @Nullable Object, B extends @Nullable Object> Predicate<A> compose(
238      Predicate<B> predicate, Function<A, ? extends B> function) {
239    return new CompositionPredicate<>(predicate, function);
240  }
241
242  /**
243   * Returns a predicate that evaluates to {@code true} if the {@code CharSequence} being tested
244   * contains any match for the given regular expression pattern. The test used is equivalent to
245   * {@code Pattern.compile(pattern).matcher(arg).find()}
246   *
247   * @throws IllegalArgumentException if the pattern is invalid
248   * @since 3.0
249   */
250  @GwtIncompatible // Only used by other GWT-incompatible code.
251  public static Predicate<CharSequence> containsPattern(String pattern) {
252    return new ContainsPatternFromStringPredicate(pattern);
253  }
254
255  /**
256   * Returns a predicate that evaluates to {@code true} if the {@code CharSequence} being tested
257   * contains any match for the given regular expression pattern. The test used is equivalent to
258   * {@code pattern.matcher(arg).find()}
259   *
260   * @since 3.0
261   */
262  @GwtIncompatible(value = "java.util.regex.Pattern")
263  public static Predicate<CharSequence> contains(Pattern pattern) {
264    return new ContainsPatternPredicate(new JdkPattern(pattern));
265  }
266
267  // End public API, begin private implementation classes.
268
269  // Package private for GWT serialization.
270  enum ObjectPredicate implements Predicate<@Nullable Object> {
271    /**
272     * @see Predicates#alwaysTrue()
273     */
274    ALWAYS_TRUE {
275      @Override
276      public boolean apply(@Nullable Object o) {
277        return true;
278      }
279
280      @Override
281      public String toString() {
282        return "Predicates.alwaysTrue()";
283      }
284    },
285    /**
286     * @see Predicates#alwaysFalse()
287     */
288    ALWAYS_FALSE {
289      @Override
290      public boolean apply(@Nullable Object o) {
291        return false;
292      }
293
294      @Override
295      public String toString() {
296        return "Predicates.alwaysFalse()";
297      }
298    },
299    /**
300     * @see Predicates#isNull()
301     */
302    IS_NULL {
303      @Override
304      public boolean apply(@Nullable Object o) {
305        return o == null;
306      }
307
308      @Override
309      public String toString() {
310        return "Predicates.isNull()";
311      }
312    },
313    /**
314     * @see Predicates#notNull()
315     */
316    NOT_NULL {
317      @Override
318      public boolean apply(@Nullable Object o) {
319        return o != null;
320      }
321
322      @Override
323      public String toString() {
324        return "Predicates.notNull()";
325      }
326    };
327
328    @SuppressWarnings("unchecked") // safe contravariant cast
329    <T extends @Nullable Object> Predicate<T> withNarrowedType() {
330      return (Predicate<T>) this;
331    }
332  }
333
334  /**
335   * @see Predicates#not(Predicate)
336   */
337  private static class NotPredicate<T extends @Nullable Object>
338      implements Predicate<T>, Serializable {
339    final Predicate<T> predicate;
340
341    NotPredicate(Predicate<T> predicate) {
342      this.predicate = checkNotNull(predicate);
343    }
344
345    @Override
346    public boolean apply(@ParametricNullness T t) {
347      return !predicate.apply(t);
348    }
349
350    @Override
351    public int hashCode() {
352      return ~predicate.hashCode();
353    }
354
355    @Override
356    public boolean equals(@Nullable Object obj) {
357      if (obj instanceof NotPredicate) {
358        NotPredicate<?> that = (NotPredicate<?>) obj;
359        return predicate.equals(that.predicate);
360      }
361      return false;
362    }
363
364    @Override
365    public String toString() {
366      return "Predicates.not(" + predicate + ")";
367    }
368
369    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
370  }
371
372  /**
373   * @see Predicates#and(Iterable)
374   */
375  private static class AndPredicate<T extends @Nullable Object>
376      implements Predicate<T>, Serializable {
377    private final List<? extends Predicate<? super T>> components;
378
379    private AndPredicate(List<? extends Predicate<? super T>> components) {
380      this.components = components;
381    }
382
383    @Override
384    public boolean apply(@ParametricNullness T t) {
385      // Avoid using the Iterator to avoid generating garbage (issue 820).
386      for (int i = 0; i < components.size(); i++) {
387        if (!components.get(i).apply(t)) {
388          return false;
389        }
390      }
391      return true;
392    }
393
394    @Override
395    public int hashCode() {
396      // add a random number to avoid collisions with OrPredicate
397      return components.hashCode() + 0x12472c2c;
398    }
399
400    @Override
401    public boolean equals(@Nullable Object obj) {
402      if (obj instanceof AndPredicate) {
403        AndPredicate<?> that = (AndPredicate<?>) obj;
404        return components.equals(that.components);
405      }
406      return false;
407    }
408
409    @Override
410    public String toString() {
411      return toStringHelper("and", components);
412    }
413
414    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
415  }
416
417  /**
418   * @see Predicates#or(Iterable)
419   */
420  private static class OrPredicate<T extends @Nullable Object>
421      implements Predicate<T>, Serializable {
422    private final List<? extends Predicate<? super T>> components;
423
424    private OrPredicate(List<? extends Predicate<? super T>> components) {
425      this.components = components;
426    }
427
428    @Override
429    public boolean apply(@ParametricNullness T t) {
430      // Avoid using the Iterator to avoid generating garbage (issue 820).
431      for (int i = 0; i < components.size(); i++) {
432        if (components.get(i).apply(t)) {
433          return true;
434        }
435      }
436      return false;
437    }
438
439    @Override
440    public int hashCode() {
441      // add a random number to avoid collisions with AndPredicate
442      return components.hashCode() + 0x053c91cf;
443    }
444
445    @Override
446    public boolean equals(@Nullable Object obj) {
447      if (obj instanceof OrPredicate) {
448        OrPredicate<?> that = (OrPredicate<?>) obj;
449        return components.equals(that.components);
450      }
451      return false;
452    }
453
454    @Override
455    public String toString() {
456      return toStringHelper("or", components);
457    }
458
459    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
460  }
461
462  private static String toStringHelper(String methodName, Iterable<?> components) {
463    StringBuilder builder = new StringBuilder("Predicates.").append(methodName).append('(');
464    boolean first = true;
465    for (Object o : components) {
466      if (!first) {
467        builder.append(',');
468      }
469      builder.append(o);
470      first = false;
471    }
472    return builder.append(')').toString();
473  }
474
475  /**
476   * @see Predicates#equalTo(Object)
477   */
478  private static class IsEqualToPredicate implements Predicate<@Nullable Object>, Serializable {
479    private final Object target;
480
481    private IsEqualToPredicate(Object target) {
482      this.target = target;
483    }
484
485    @Override
486    public boolean apply(@Nullable Object o) {
487      return target.equals(o);
488    }
489
490    @Override
491    public int hashCode() {
492      return target.hashCode();
493    }
494
495    @Override
496    public boolean equals(@Nullable Object obj) {
497      if (obj instanceof IsEqualToPredicate) {
498        IsEqualToPredicate that = (IsEqualToPredicate) obj;
499        return target.equals(that.target);
500      }
501      return false;
502    }
503
504    @Override
505    public String toString() {
506      return "Predicates.equalTo(" + target + ")";
507    }
508
509    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
510
511    @SuppressWarnings("unchecked") // safe contravariant cast
512    <T extends @Nullable Object> Predicate<T> withNarrowedType() {
513      return (Predicate<T>) this;
514    }
515  }
516
517  /**
518   * @see Predicates#instanceOf(Class)
519   */
520  @GwtIncompatible // Class.isInstance
521  private static class InstanceOfPredicate<T extends @Nullable Object>
522      implements Predicate<T>, Serializable {
523    private final Class<?> clazz;
524
525    private InstanceOfPredicate(Class<?> clazz) {
526      this.clazz = checkNotNull(clazz);
527    }
528
529    @Override
530    public boolean apply(@ParametricNullness T o) {
531      return clazz.isInstance(o);
532    }
533
534    @Override
535    public int hashCode() {
536      return clazz.hashCode();
537    }
538
539    @Override
540    public boolean equals(@Nullable Object obj) {
541      if (obj instanceof InstanceOfPredicate) {
542        InstanceOfPredicate<?> that = (InstanceOfPredicate<?>) obj;
543        return clazz == that.clazz;
544      }
545      return false;
546    }
547
548    @Override
549    public String toString() {
550      return "Predicates.instanceOf(" + clazz.getName() + ")";
551    }
552
553    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
554  }
555
556  /**
557   * @see Predicates#subtypeOf(Class)
558   */
559  @J2ktIncompatible
560  @GwtIncompatible // Class.isAssignableFrom
561  private static class SubtypeOfPredicate implements Predicate<Class<?>>, Serializable {
562    private final Class<?> clazz;
563
564    private SubtypeOfPredicate(Class<?> clazz) {
565      this.clazz = checkNotNull(clazz);
566    }
567
568    @Override
569    public boolean apply(Class<?> input) {
570      return clazz.isAssignableFrom(input);
571    }
572
573    @Override
574    public int hashCode() {
575      return clazz.hashCode();
576    }
577
578    @Override
579    public boolean equals(@Nullable Object obj) {
580      if (obj instanceof SubtypeOfPredicate) {
581        SubtypeOfPredicate that = (SubtypeOfPredicate) obj;
582        return clazz == that.clazz;
583      }
584      return false;
585    }
586
587    @Override
588    public String toString() {
589      return "Predicates.subtypeOf(" + clazz.getName() + ")";
590    }
591
592    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
593  }
594
595  /**
596   * @see Predicates#in(Collection)
597   */
598  private static class InPredicate<T extends @Nullable Object>
599      implements Predicate<T>, Serializable {
600    private final Collection<?> target;
601
602    private InPredicate(Collection<?> target) {
603      this.target = checkNotNull(target);
604    }
605
606    @Override
607    public boolean apply(@ParametricNullness T t) {
608      try {
609        return target.contains(t);
610      } catch (NullPointerException | ClassCastException e) {
611        return false;
612      }
613    }
614
615    @Override
616    /*
617     * We should probably not have implemented equals() at all, but given that we did, we can't
618     * provide a better implementation than the input Collection, at least without dramatic changes
619     * like copying it to a new Set—which might then test for element equality differently.
620     */
621    @SuppressWarnings("UndefinedEquals")
622    public boolean equals(@Nullable Object obj) {
623      if (obj instanceof InPredicate) {
624        InPredicate<?> that = (InPredicate<?>) obj;
625        return target.equals(that.target);
626      }
627      return false;
628    }
629
630    @Override
631    public int hashCode() {
632      return target.hashCode();
633    }
634
635    @Override
636    public String toString() {
637      return "Predicates.in(" + target + ")";
638    }
639
640    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
641  }
642
643  /**
644   * @see Predicates#compose(Predicate, Function)
645   */
646  private static class CompositionPredicate<A extends @Nullable Object, B extends @Nullable Object>
647      implements Predicate<A>, Serializable {
648    final Predicate<B> p;
649    final Function<A, ? extends B> f;
650
651    private CompositionPredicate(Predicate<B> p, Function<A, ? extends B> f) {
652      this.p = checkNotNull(p);
653      this.f = checkNotNull(f);
654    }
655
656    @Override
657    public boolean apply(@ParametricNullness A a) {
658      return p.apply(f.apply(a));
659    }
660
661    @Override
662    public boolean equals(@Nullable Object obj) {
663      if (obj instanceof CompositionPredicate) {
664        CompositionPredicate<?, ?> that = (CompositionPredicate<?, ?>) obj;
665        return f.equals(that.f) && p.equals(that.p);
666      }
667      return false;
668    }
669
670    @Override
671    public int hashCode() {
672      return f.hashCode() ^ p.hashCode();
673    }
674
675    @Override
676    public String toString() {
677      // TODO(cpovirk): maybe make this look like the method call does ("Predicates.compose(...)")
678      return p + "(" + f + ")";
679    }
680
681    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
682  }
683
684  /**
685   * @see Predicates#contains(Pattern)
686   */
687  @GwtIncompatible // Only used by other GWT-incompatible code.
688  private static class ContainsPatternPredicate implements Predicate<CharSequence>, Serializable {
689    final CommonPattern pattern;
690
691    ContainsPatternPredicate(CommonPattern pattern) {
692      this.pattern = checkNotNull(pattern);
693    }
694
695    @Override
696    public boolean apply(CharSequence t) {
697      return pattern.matcher(t).find();
698    }
699
700    @Override
701    public int hashCode() {
702      // Pattern uses Object.hashCode, so we have to reach
703      // inside to build a hashCode consistent with equals.
704
705      return Objects.hashCode(pattern.pattern(), pattern.flags());
706    }
707
708    @Override
709    public boolean equals(@Nullable Object obj) {
710      if (obj instanceof ContainsPatternPredicate) {
711        ContainsPatternPredicate that = (ContainsPatternPredicate) obj;
712
713        // Pattern uses Object (identity) equality, so we have to reach
714        // inside to compare individual fields.
715        return Objects.equal(pattern.pattern(), that.pattern.pattern())
716            && pattern.flags() == that.pattern.flags();
717      }
718      return false;
719    }
720
721    @Override
722    public String toString() {
723      String patternString =
724          MoreObjects.toStringHelper(pattern)
725              .add("pattern", pattern.pattern())
726              .add("pattern.flags", pattern.flags())
727              .toString();
728      return "Predicates.contains(" + patternString + ")";
729    }
730
731    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
732  }
733
734  /**
735   * @see Predicates#containsPattern(String)
736   */
737  @GwtIncompatible // Only used by other GWT-incompatible code.
738  private static class ContainsPatternFromStringPredicate extends ContainsPatternPredicate {
739
740    ContainsPatternFromStringPredicate(String string) {
741      super(Platform.compilePattern(string));
742    }
743
744    @Override
745    public String toString() {
746      return "Predicates.containsPattern(" + pattern.pattern() + ")";
747    }
748
749    @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0;
750  }
751
752  private static <T extends @Nullable Object> List<Predicate<? super T>> asList(
753      Predicate<? super T> first, Predicate<? super T> second) {
754    // TODO(kevinb): understand why we still get a warning despite @SafeVarargs!
755    return Arrays.<Predicate<? super T>>asList(first, second);
756  }
757
758  private static <T> List<T> defensiveCopy(T... array) {
759    return defensiveCopy(Arrays.asList(array));
760  }
761
762  static <T> List<T> defensiveCopy(Iterable<T> iterable) {
763    ArrayList<T> list = new ArrayList<>();
764    for (T element : iterable) {
765      list.add(checkNotNull(element));
766    }
767    return list;
768  }
769}