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.util.Collection;
20
21 import org.apache.commons.collections4.Predicate;
22
23 /**
24 * Predicate implementation that returns true if only one of the
25 * predicates return true.
26 * If the array of predicates is empty, then this predicate returns false.
27 * <p>
28 * NOTE: In versions prior to 3.2 an array size of zero or one
29 * threw an exception.
30 * </p>
31 *
32 * @param <T> the type of the input to the predicate.
33 * @since 3.0
34 */
35 public final class OnePredicate<T> extends AbstractQuantifierPredicate<T> {
36
37 /** Serial version UID */
38 private static final long serialVersionUID = -8125389089924745785L;
39
40 /**
41 * Creates the predicate.
42 *
43 * @param <T> the type that the predicate queries
44 * @param predicates the predicates to check, cloned, not null
45 * @return the {@code one} predicate
46 * @throws NullPointerException if the predicates array is null
47 * @throws NullPointerException if any predicate in the array is null
48 */
49 public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) {
50 final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
51 return new OnePredicate<>(preds);
52 }
53
54 /**
55 * Creates the predicate.
56 * <p>
57 * If the array is size zero, the predicate always returns false.
58 * If the array is size one, then that predicate is returned.
59 * </p>
60 *
61 * @param <T> the type that the predicate queries
62 * @param predicates the predicates to check, cloned, not null
63 * @return the {@code any} predicate
64 * @throws NullPointerException if the predicates array is null
65 * @throws NullPointerException if any predicate in the array is null
66 */
67 @SuppressWarnings("unchecked")
68 public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
69 FunctorUtils.validate(predicates);
70 if (predicates.length == 0) {
71 return FalsePredicate.<T>falsePredicate();
72 }
73 if (predicates.length == 1) {
74 return (Predicate<T>) predicates[0];
75 }
76 // <T> not needed in Eclipse but needed by the command line compiler
77 return new OnePredicate<T>(FunctorUtils.copy(predicates));
78 }
79
80 /**
81 * Constructor that performs no validation.
82 * Use {@code onePredicate} if you want that.
83 *
84 * @param predicates the predicates to check, not cloned, not null
85 */
86 public OnePredicate(final Predicate<? super T>... predicates) {
87 super(predicates);
88 }
89
90 /**
91 * Evaluates the predicate returning true if only one decorated predicate
92 * returns true.
93 *
94 * @param object the input object
95 * @return true if only one decorated predicate returns true
96 */
97 @Override
98 public boolean test(final T object) {
99 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 }