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     * <p>
042     * If the array is size zero, the predicate always returns false.
043     * If the array is size one, then that predicate is returned.
044     *
045     * @param <T> the type that the predicate queries
046     * @param predicates  the predicates to check, cloned, not null
047     * @return the <code>any</code> predicate
048     * @throws NullPointerException if the predicates array is null
049     * @throws NullPointerException if any predicate in the array is null
050     */
051    @SuppressWarnings("unchecked")
052    public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
053        FunctorUtils.validate(predicates);
054        if (predicates.length == 0) {
055            return FalsePredicate.<T>falsePredicate();
056        }
057        if (predicates.length == 1) {
058            return (Predicate<T>) predicates[0];
059        }
060        return new OnePredicate<>(FunctorUtils.copy(predicates));
061    }
062
063    /**
064     * Factory to create the predicate.
065     *
066     * @param <T> the type that the predicate queries
067     * @param predicates  the predicates to check, cloned, not null
068     * @return the <code>one</code> predicate
069     * @throws NullPointerException if the predicates array is null
070     * @throws NullPointerException if any predicate in the array is null
071     */
072    public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) {
073        final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
074        return new OnePredicate<>(preds);
075    }
076
077    /**
078     * Constructor that performs no validation.
079     * Use <code>onePredicate</code> 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}