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