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