1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.collections4.functors; 18 19 import java.io.Serializable; 20 import java.util.Objects; 21 22 import org.apache.commons.collections4.Predicate; 23 24 /** 25 * Predicate implementation that returns true if both the predicates return true. 26 * 27 * @since 3.0 28 */ 29 public final class AndPredicate<T> implements PredicateDecorator<T>, Serializable { 30 31 /** Serial version UID */ 32 private static final long serialVersionUID = 4189014213763186912L; 33 34 /** 35 * Factory to create the predicate. 36 * 37 * @param <T> the type that the predicate queries 38 * @param predicate1 the first predicate to check, not null 39 * @param predicate2 the second predicate to check, not null 40 * @return the {@code and} predicate 41 * @throws NullPointerException if either predicate is null 42 */ 43 public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1, 44 final Predicate<? super T> predicate2) { 45 return new AndPredicate<>(Objects.requireNonNull(predicate1, "predicate1"), 46 Objects.requireNonNull(predicate2, "predicate2")); 47 } 48 /** The array of predicates to call */ 49 private final Predicate<? super T> iPredicate1; 50 51 /** The array of predicates to call */ 52 private final Predicate<? super T> iPredicate2; 53 54 /** 55 * Constructor that performs no validation. 56 * Use {@code andPredicate} if you want that. 57 * 58 * @param predicate1 the first predicate to check, not null 59 * @param predicate2 the second predicate to check, not null 60 */ 61 public AndPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { 62 iPredicate1 = predicate1; 63 iPredicate2 = predicate2; 64 } 65 66 /** 67 * Evaluates the predicate returning true if both predicates return true. 68 * 69 * @param object the input object 70 * @return true if both decorated predicates return true 71 */ 72 @Override 73 public boolean evaluate(final T object) { 74 return iPredicate1.evaluate(object) && iPredicate2.evaluate(object); 75 } 76 77 /** 78 * Gets the two predicates being decorated as an array. 79 * 80 * @return the predicates 81 * @since 3.1 82 */ 83 @Override 84 @SuppressWarnings("unchecked") 85 public Predicate<? super T>[] getPredicates() { 86 return new Predicate[] {iPredicate1, iPredicate2}; 87 } 88 89 }