OnePredicate.java

  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. import java.util.Collection;

  19. import org.apache.commons.collections4.Predicate;

  20. /**
  21.  * Predicate implementation that returns true if only one of the
  22.  * predicates return true.
  23.  * If the array of predicates is empty, then this predicate returns false.
  24.  * <p>
  25.  * NOTE: In versions prior to 3.2 an array size of zero or one
  26.  * threw an exception.
  27.  * </p>
  28.  *
  29.  * @param <T> the type of the input to the predicate.
  30.  * @since 3.0
  31.  */
  32. public final class OnePredicate<T> extends AbstractQuantifierPredicate<T> {

  33.     /** Serial version UID */
  34.     private static final long serialVersionUID = -8125389089924745785L;

  35.     /**
  36.      * Creates the predicate.
  37.      *
  38.      * @param <T> the type that the predicate queries
  39.      * @param predicates  the predicates to check, cloned, not null
  40.      * @return the {@code one} predicate
  41.      * @throws NullPointerException if the predicates array is null
  42.      * @throws NullPointerException if any predicate in the array is null
  43.      */
  44.     public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates) {
  45.         final Predicate<? super T>[] preds = FunctorUtils.validate(predicates);
  46.         return new OnePredicate<>(preds);
  47.     }

  48.     /**
  49.      * Creates the predicate.
  50.      * <p>
  51.      * If the array is size zero, the predicate always returns false.
  52.      * If the array is size one, then that predicate is returned.
  53.      * </p>
  54.      *
  55.      * @param <T> the type that the predicate queries
  56.      * @param predicates  the predicates to check, cloned, not null
  57.      * @return the {@code any} predicate
  58.      * @throws NullPointerException if the predicates array is null
  59.      * @throws NullPointerException if any predicate in the array is null
  60.      */
  61.     @SuppressWarnings("unchecked")
  62.     public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) {
  63.         FunctorUtils.validate(predicates);
  64.         if (predicates.length == 0) {
  65.             return FalsePredicate.<T>falsePredicate();
  66.         }
  67.         if (predicates.length == 1) {
  68.             return (Predicate<T>) predicates[0];
  69.         }
  70.         // <T> not needed in Eclipse but needed by the command line compiler
  71.         return new OnePredicate<T>(FunctorUtils.copy(predicates));
  72.     }

  73.     /**
  74.      * Constructor that performs no validation.
  75.      * Use {@code onePredicate} if you want that.
  76.      *
  77.      * @param predicates  the predicates to check, not cloned, not null
  78.      */
  79.     public OnePredicate(final Predicate<? super T>... predicates) {
  80.         super(predicates);
  81.     }

  82.     /**
  83.      * Evaluates the predicate returning true if only one decorated predicate
  84.      * returns true.
  85.      *
  86.      * @param object  the input object
  87.      * @return true if only one decorated predicate returns true
  88.      */
  89.     @Override
  90.     public boolean test(final T object) {
  91.         boolean match = false;
  92.         for (final Predicate<? super T> iPredicate : iPredicates) {
  93.             if (iPredicate.test(object)) {
  94.                 if (match) {
  95.                     return false;
  96.                 }
  97.                 match = true;
  98.             }
  99.         }
  100.         return match;
  101.     }

  102. }