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 19 import java.io.Serializable; 20 import java.util.Objects; 21 22 import org.apache.commons.collections4.Predicate; 23 import org.apache.commons.collections4.Transformer; 24 25 /** 26 * Predicate implementation that transforms the given object before invoking 27 * another {@code Predicate}. 28 * 29 * @since 3.1 30 */ 31 public final class TransformedPredicate<T> implements PredicateDecorator<T>, Serializable { 32 33 /** Serial version UID */ 34 private static final long serialVersionUID = -5596090919668315834L; 35 36 /** 37 * Factory to create the predicate. 38 * 39 * @param <T> the type that the predicate queries 40 * @param transformer the transformer to call 41 * @param predicate the predicate to call with the result of the transform 42 * @return the predicate 43 * @throws NullPointerException if the transformer or the predicate is null 44 */ 45 public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer, 46 final Predicate<? super T> predicate) { 47 return new TransformedPredicate<>(Objects.requireNonNull(transformer, "transformer"), 48 Objects.requireNonNull(predicate, "predicate")); 49 } 50 51 /** The transformer to call */ 52 private final Transformer<? super T, ? extends T> iTransformer; 53 54 /** The predicate to call */ 55 private final Predicate<? super T> iPredicate; 56 57 /** 58 * Constructor that performs no validation. 59 * Use {@code transformedPredicate} if you want that. 60 * 61 * @param transformer the transformer to use 62 * @param predicate the predicate to decorate 63 */ 64 public TransformedPredicate(final Transformer<? super T, ? extends T> transformer, 65 final Predicate<? super T> predicate) { 66 iTransformer = transformer; 67 iPredicate = predicate; 68 } 69 70 /** 71 * Evaluates the predicate returning the result of the decorated predicate 72 * once the input has been transformed 73 * 74 * @param object the input object which will be transformed 75 * @return true if decorated predicate returns true 76 */ 77 @Override 78 public boolean evaluate(final T object) { 79 final T result = iTransformer.transform(object); 80 return iPredicate.evaluate(result); 81 } 82 83 /** 84 * Gets the predicate being decorated. 85 * 86 * @return the predicate as the only element in an array 87 * @since 3.1 88 */ 89 @Override 90 @SuppressWarnings("unchecked") 91 public Predicate<? super T>[] getPredicates() { 92 return new Predicate[] {iPredicate}; 93 } 94 95 /** 96 * Gets the transformer in use. 97 * 98 * @return the transformer 99 */ 100 public Transformer<? super T, ? extends T> getTransformer() { 101 return iTransformer; 102 } 103 104 }