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.util.Collection; 020 021import org.apache.commons.collections4.Predicate; 022 023/** 024 * Predicate implementation that returns true if any of the 025 * predicates return true. 026 * If the array of predicates is empty, then this predicate returns false. 027 * <p> 028 * NOTE: In versions prior to 3.2 an array size of zero or one 029 * threw an exception. 030 * </p> 031 * 032 * @param <T> the type of the input to the predicate. 033 * @since 3.0 034 */ 035public final class AnyPredicate<T> extends AbstractQuantifierPredicate<T> { 036 037 /** Serial version UID */ 038 private static final long serialVersionUID = 7429999530934647542L; 039 040 /** 041 * Creates the predicate. 042 * <p> 043 * If the collection is size zero, the predicate always returns false. 044 * If the collection is size one, then that predicate is returned. 045 * </p> 046 * 047 * @param <T> the type that the predicate queries 048 * @param predicates the predicates to check, cloned, not null 049 * @return the {@code all} predicate 050 * @throws NullPointerException if the predicates array is null 051 * @throws NullPointerException if any predicate in the array is null 052 */ 053 @SuppressWarnings("unchecked") 054 public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates) { 055 final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); 056 if (preds.length == 0) { 057 return FalsePredicate.<T>falsePredicate(); 058 } 059 if (preds.length == 1) { 060 return (Predicate<T>) preds[0]; 061 } 062 return new AnyPredicate<>(preds); 063 } 064 065 /** 066 * Creates the predicate. 067 * <p> 068 * If the array is size zero, the predicate always returns false. 069 * If the array is size one, then that predicate is returned. 070 * </p> 071 * 072 * @param <T> the type that the predicate queries 073 * @param predicates the predicates to check, cloned, not null 074 * @return the {@code any} predicate 075 * @throws NullPointerException if the predicates array is null 076 * @throws NullPointerException if any predicate in the array is null 077 */ 078 @SuppressWarnings("unchecked") 079 public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) { 080 FunctorUtils.validate(predicates); 081 if (predicates.length == 0) { 082 return FalsePredicate.<T>falsePredicate(); 083 } 084 if (predicates.length == 1) { 085 return (Predicate<T>) predicates[0]; 086 } 087 return new AnyPredicate<T>(FunctorUtils.copy(predicates)); 088 } 089 090 /** 091 * Constructor that performs no validation. 092 * Use {@code anyPredicate} if you want that. 093 * 094 * @param predicates the predicates to check, not cloned, not null 095 */ 096 public AnyPredicate(final Predicate<? super T>... predicates) { 097 super(predicates); 098 } 099 100 /** 101 * Evaluates the predicate returning true if any predicate returns true. 102 * 103 * @param object the input object 104 * @return true if any decorated predicate return true 105 */ 106 @Override 107 public boolean test(final T object) { 108 for (final Predicate<? super T> iPredicate : iPredicates) { 109 if (iPredicate.test(object)) { 110 return true; 111 } 112 } 113 return false; 114 } 115 116}