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 only one 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 OnePredicate<T> extends AbstractQuantifierPredicate<T> { 036 037 /** Serial version UID */ 038 private static final long serialVersionUID = -8125389089924745785L; 039 040 /** 041 * Creates the predicate. 042 * 043 * @param <T> the type that the predicate queries 044 * @param predicates the predicates to check, cloned, not null 045 * @return the {@code one} predicate 046 * @throws NullPointerException if the predicates array is null 047 * @throws NullPointerException if any predicate in the array is null 048 */ 049 public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) { 050 final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); 051 return new OnePredicate<>(preds); 052 } 053 054 /** 055 * Creates the predicate. 056 * <p> 057 * If the array is size zero, the predicate always returns false. 058 * If the array is size one, then that predicate is returned. 059 * </p> 060 * 061 * @param <T> the type that the predicate queries 062 * @param predicates the predicates to check, cloned, not null 063 * @return the {@code any} predicate 064 * @throws NullPointerException if the predicates array is null 065 * @throws NullPointerException if any predicate in the array is null 066 */ 067 @SuppressWarnings("unchecked") 068 public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) { 069 FunctorUtils.validate(predicates); 070 if (predicates.length == 0) { 071 return FalsePredicate.<T>falsePredicate(); 072 } 073 if (predicates.length == 1) { 074 return (Predicate<T>) predicates[0]; 075 } 076 // <T> not needed in Eclipse but needed by the command line compiler 077 return new OnePredicate<T>(FunctorUtils.copy(predicates)); 078 } 079 080 /** 081 * Constructor that performs no validation. 082 * Use {@code onePredicate} if you want that. 083 * 084 * @param predicates the predicates to check, not cloned, not null 085 */ 086 public OnePredicate(final Predicate<? super T>... predicates) { 087 super(predicates); 088 } 089 090 /** 091 * Evaluates the predicate returning true if only one decorated predicate 092 * returns true. 093 * 094 * @param object the input object 095 * @return true if only one decorated predicate returns true 096 */ 097 @Override 098 public boolean test(final T object) { 099 boolean match = false; 100 for (final Predicate<? super T> iPredicate : iPredicates) { 101 if (iPredicate.test(object)) { 102 if (match) { 103 return false; 104 } 105 match = true; 106 } 107 } 108 return match; 109 } 110 111}