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 */ 017 package org.apache.commons.collections.functors; 018 019 import java.io.Serializable; 020 import java.util.Collection; 021 022 import org.apache.commons.collections.Predicate; 023 024 /** 025 * Predicate implementation that returns true if any of the 026 * predicates return true. 027 * If the array of predicates is empty, then this predicate returns false. 028 * <p> 029 * NOTE: In versions prior to 3.2 an array size of zero or one 030 * threw an exception. 031 * 032 * @since Commons Collections 3.0 033 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 034 * 035 * @author Stephen Colebourne 036 * @author Matt Benson 037 */ 038 public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable { 039 040 /** Serial version UID */ 041 private static final long serialVersionUID = 7429999530934647542L; 042 043 /** The array of predicates to call */ 044 private final Predicate[] iPredicates; 045 046 /** 047 * Factory to create the predicate. 048 * <p> 049 * If the array is size zero, the predicate always returns false. 050 * If the array is size one, then that predicate is returned. 051 * 052 * @param predicates the predicates to check, cloned, not null 053 * @return the <code>any</code> predicate 054 * @throws IllegalArgumentException if the predicates array is null 055 * @throws IllegalArgumentException if any predicate in the array is null 056 */ 057 public static Predicate getInstance(Predicate[] predicates) { 058 FunctorUtils.validate(predicates); 059 if (predicates.length == 0) { 060 return FalsePredicate.INSTANCE; 061 } 062 if (predicates.length == 1) { 063 return predicates[0]; 064 } 065 return new AnyPredicate(FunctorUtils.copy(predicates)); 066 } 067 068 /** 069 * Factory to create the predicate. 070 * <p> 071 * If the collection is size zero, the predicate always returns false. 072 * If the collection is size one, then that predicate is returned. 073 * 074 * @param predicates the predicates to check, cloned, not null 075 * @return the <code>all</code> predicate 076 * @throws IllegalArgumentException if the predicates array is null 077 * @throws IllegalArgumentException if any predicate in the array is null 078 */ 079 public static Predicate getInstance(Collection predicates) { 080 Predicate[] preds = FunctorUtils.validate(predicates); 081 if (preds.length == 0) { 082 return FalsePredicate.INSTANCE; 083 } 084 if (preds.length == 1) { 085 return preds[0]; 086 } 087 return new AnyPredicate(preds); 088 } 089 090 /** 091 * Constructor that performs no validation. 092 * Use <code>getInstance</code> if you want that. 093 * 094 * @param predicates the predicates to check, not cloned, not null 095 */ 096 public AnyPredicate(Predicate[] predicates) { 097 super(); 098 iPredicates = predicates; 099 } 100 101 /** 102 * Evaluates the predicate returning true if any predicate returns true. 103 * 104 * @param object the input object 105 * @return true if any decorated predicate return true 106 */ 107 public boolean evaluate(Object object) { 108 for (int i = 0; i < iPredicates.length; i++) { 109 if (iPredicates[i].evaluate(object)) { 110 return true; 111 } 112 } 113 return false; 114 } 115 116 /** 117 * Gets the predicates, do not modify the array. 118 * 119 * @return the predicates 120 * @since Commons Collections 3.1 121 */ 122 public Predicate[] getPredicates() { 123 return iPredicates; 124 } 125 126 }