TransformedPredicate.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 java.util.Objects;

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

  22. /**
  23.  * Predicate implementation that transforms the given object before invoking
  24.  * another {@code Predicate}.
  25.  *
  26.  * @param <T> the type of the input to the predicate.
  27.  * @since 3.1
  28.  */
  29. public final class TransformedPredicate<T> extends AbstractPredicate<T> implements PredicateDecorator<T>, Serializable {

  30.     /** Serial version UID */
  31.     private static final long serialVersionUID = -5596090919668315834L;

  32.     /**
  33.      * Creates the predicate.
  34.      *
  35.      * @param <T> the type that the predicate queries
  36.      * @param transformer  the transformer to call
  37.      * @param predicate  the predicate to call with the result of the transform
  38.      * @return the predicate
  39.      * @throws NullPointerException if the transformer or the predicate is null
  40.      */
  41.     public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer,
  42.                                                         final Predicate<? super T> predicate) {
  43.         return new TransformedPredicate<>(Objects.requireNonNull(transformer, "transformer"),
  44.                 Objects.requireNonNull(predicate, "predicate"));
  45.     }

  46.     /** The transformer to call */
  47.     private final Transformer<? super T, ? extends T> iTransformer;

  48.     /** The predicate to call */
  49.     private final Predicate<? super T> iPredicate;

  50.     /**
  51.      * Constructor that performs no validation.
  52.      * Use {@code transformedPredicate} if you want that.
  53.      *
  54.      * @param transformer  the transformer to use
  55.      * @param predicate  the predicate to decorate
  56.      */
  57.     public TransformedPredicate(final Transformer<? super T, ? extends T> transformer,
  58.                                 final Predicate<? super T> predicate) {
  59.         iTransformer = transformer;
  60.         iPredicate = predicate;
  61.     }

  62.     /**
  63.      * Gets the predicate being decorated.
  64.      *
  65.      * @return the predicate as the only element in an array
  66.      * @since 3.1
  67.      */
  68.     @Override
  69.     @SuppressWarnings("unchecked")
  70.     public Predicate<? super T>[] getPredicates() {
  71.         return new Predicate[] {iPredicate};
  72.     }

  73.     /**
  74.      * Gets the transformer in use.
  75.      *
  76.      * @return the transformer
  77.      */
  78.     public Transformer<? super T, ? extends T> getTransformer() {
  79.         return iTransformer;
  80.     }

  81.     /**
  82.      * Evaluates the predicate returning the result of the decorated predicate
  83.      * once the input has been transformed
  84.      *
  85.      * @param object  the input object which will be transformed
  86.      * @return true if decorated predicate returns true
  87.      */
  88.     @Override
  89.     public boolean test(final T object) {
  90.         final T result = iTransformer.apply(object);
  91.         return iPredicate.test(result);
  92.     }

  93. }