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