1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.util; 18 19 import java.util.Arrays; 20 21 /** 22 * Predicates contains static methods for creating the standard set of 23 * {@code Predicate} objects. 24 */ 25 public class Predicates { 26 27 private Predicates() { 28 } 29 30 /** 31 * Returns a Predicate that evaluates to true iff each of its components 32 * evaluates to true. The components are evaluated in order, and evaluation 33 * will be "short-circuited" as soon as the answer is determined. 34 */ 35 public static <T> Predicate<T> and(Predicate<? super T>... components) { 36 return and(Arrays.asList(components)); 37 } 38 39 /** 40 * Returns a Predicate that evaluates to true iff each of its components 41 * evaluates to true. The components are evaluated in order, and evaluation 42 * will be "short-circuited" as soon as the answer is determined. Does not 43 * defensively copy the iterable passed in, so future changes to it will alter 44 * the behavior of this Predicate. If components is empty, the returned 45 * Predicate will always evaluate to true. 46 */ 47 public static <T> Predicate<T> and(Iterable<? extends Predicate<? super T>> components) { 48 return new AndPredicate(components); 49 } 50 51 /** 52 * Returns a Predicate that evaluates to true iff any one of its components 53 * evaluates to true. The components are evaluated in order, and evaluation 54 * will be "short-circuited" as soon as the answer is determined. 55 */ 56 public static <T> Predicate<T> or(Predicate<? super T>... components) { 57 return or(Arrays.asList(components)); 58 } 59 60 /** 61 * Returns a Predicate that evaluates to true iff any one of its components 62 * evaluates to true. The components are evaluated in order, and evaluation 63 * will be "short-circuited" as soon as the answer is determined. Does not 64 * defensively copy the iterable passed in, so future changes to it will alter 65 * the behavior of this Predicate. If components is empty, the returned 66 * Predicate will always evaluate to false. 67 */ 68 public static <T> Predicate<T> or(Iterable<? extends Predicate<? super T>> components) { 69 return new OrPredicate(components); 70 } 71 72 /** 73 * Returns a Predicate that evaluates to true iff the given Predicate 74 * evaluates to false. 75 */ 76 public static <T> Predicate<T> not(Predicate<? super T> predicate) { 77 return new NotPredicate<T>(predicate); 78 } 79 80 private static class AndPredicate<T> implements Predicate<T> { 81 private final Iterable<? extends Predicate<? super T>> components; 82 83 private AndPredicate(Iterable<? extends Predicate<? super T>> components) { 84 this.components = components; 85 } 86 87 public boolean apply(T t) { 88 for (Predicate<? super T> predicate : components) { 89 if (!predicate.apply(t)) { 90 return false; 91 } 92 } 93 return true; 94 } 95 } 96 97 private static class OrPredicate<T> implements Predicate<T> { 98 private final Iterable<? extends Predicate<? super T>> components; 99 100 private OrPredicate(Iterable<? extends Predicate<? super T>> components) { 101 this.components = components; 102 } 103 104 public boolean apply(T t) { 105 for (Predicate<? super T> predicate : components) { 106 if (predicate.apply(t)) { 107 return true; 108 } 109 } 110 return false; 111 } 112 } 113 114 private static class NotPredicate<T> implements Predicate<T> { 115 private final Predicate<? super T> predicate; 116 117 private NotPredicate(Predicate<? super T> predicate) { 118 this.predicate = predicate; 119 } 120 121 public boolean apply(T t) { 122 return !predicate.apply(t); 123 } 124 } 125 } 126