PredicateTransformer.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.io.Serializable;

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

  21. /**
  22.  * Transformer implementation that calls a Predicate using the input object
  23.  * and then returns the result.
  24.  *
  25.  * @param <T> the type of the input and result to the function.
  26.  * @since 3.0
  27.  */
  28. public class PredicateTransformer<T> implements Transformer<T, Boolean>, Serializable {

  29.     /** Serial version UID */
  30.     private static final long serialVersionUID = 5278818408044349346L;

  31.     /**
  32.      * Factory method that performs validation.
  33.      *
  34.      * @param <T>  the input type
  35.      * @param predicate  the predicate to call, not null
  36.      * @return the {@code predicate} transformer
  37.      * @throws IllegalArgumentException if the predicate is null
  38.      */
  39.     public static <T> Transformer<T, Boolean> predicateTransformer(final Predicate<? super T> predicate) {
  40.         if (predicate == null) {
  41.             throw new IllegalArgumentException("Predicate must not be null");
  42.         }
  43.         return new PredicateTransformer<>(predicate);
  44.     }

  45.     /** The closure to wrap */
  46.     private final Predicate<? super T> iPredicate;

  47.     /**
  48.      * Constructor that performs no validation.
  49.      * Use {@code predicateTransformer} if you want that.
  50.      *
  51.      * @param predicate  the predicate to call, not null
  52.      */
  53.     public PredicateTransformer(final Predicate<? super T> predicate) {
  54.         iPredicate = predicate;
  55.     }

  56.     /**
  57.      * Gets the predicate.
  58.      *
  59.      * @return the predicate
  60.      * @since 3.1
  61.      */
  62.     public Predicate<? super T> getPredicate() {
  63.         return iPredicate;
  64.     }

  65.     /**
  66.      * Transforms the input to result by calling a predicate.
  67.      *
  68.      * @param input  the input object to transform
  69.      * @return the transformed result
  70.      */
  71.     @Override
  72.     public Boolean transform(final T input) {
  73.         return Boolean.valueOf(iPredicate.test(input));
  74.     }

  75. }