001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.functors; 018 019import java.io.Serializable; 020import java.util.Objects; 021 022import org.apache.commons.collections4.Predicate; 023 024/** 025 * Predicate implementation that returns true if both the predicates return true. 026 * 027 * @since 3.0 028 */ 029public final class AndPredicate<T> implements PredicateDecorator<T>, Serializable { 030 031 /** Serial version UID */ 032 private static final long serialVersionUID = 4189014213763186912L; 033 034 /** 035 * Factory to create the predicate. 036 * 037 * @param <T> the type that the predicate queries 038 * @param predicate1 the first predicate to check, not null 039 * @param predicate2 the second predicate to check, not null 040 * @return the {@code and} predicate 041 * @throws NullPointerException if either predicate is null 042 */ 043 public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1, 044 final Predicate<? super T> predicate2) { 045 return new AndPredicate<>(Objects.requireNonNull(predicate1, "predicate1"), 046 Objects.requireNonNull(predicate2, "predicate2")); 047 } 048 /** The array of predicates to call */ 049 private final Predicate<? super T> iPredicate1; 050 051 /** The array of predicates to call */ 052 private final Predicate<? super T> iPredicate2; 053 054 /** 055 * Constructor that performs no validation. 056 * Use {@code andPredicate} if you want that. 057 * 058 * @param predicate1 the first predicate to check, not null 059 * @param predicate2 the second predicate to check, not null 060 */ 061 public AndPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { 062 iPredicate1 = predicate1; 063 iPredicate2 = predicate2; 064 } 065 066 /** 067 * Evaluates the predicate returning true if both predicates return true. 068 * 069 * @param object the input object 070 * @return true if both decorated predicates return true 071 */ 072 @Override 073 public boolean evaluate(final T object) { 074 return iPredicate1.evaluate(object) && iPredicate2.evaluate(object); 075 } 076 077 /** 078 * Gets the two predicates being decorated as an array. 079 * 080 * @return the predicates 081 * @since 3.1 082 */ 083 @Override 084 @SuppressWarnings("unchecked") 085 public Predicate<? super T>[] getPredicates() { 086 return new Predicate[] {iPredicate1, iPredicate2}; 087 } 088 089}