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.FunctorException; 23 import org.apache.commons.collections4.Predicate; 24 import org.apache.commons.collections4.Transformer; 25 26 /** 27 * Predicate implementation that returns the result of a transformer. 28 * 29 * @since 3.0 30 */ 31 public final class TransformerPredicate<T> implements Predicate<T>, Serializable { 32 33 /** Serial version UID */ 34 private static final long serialVersionUID = -2407966402920578741L; 35 36 /** 37 * Factory to create the predicate. 38 * 39 * @param <T> the type that the predicate queries 40 * @param transformer the transformer to decorate 41 * @return the predicate 42 * @throws NullPointerException if the transformer is null 43 */ 44 public static <T> Predicate<T> transformerPredicate(final Transformer<? super T, Boolean> transformer) { 45 return new TransformerPredicate<>(Objects.requireNonNull(transformer, "transformer")); 46 } 47 48 /** The transformer to call */ 49 private final Transformer<? super T, Boolean> iTransformer; 50 51 /** 52 * Constructor that performs no validation. 53 * Use {@code transformerPredicate} if you want that. 54 * 55 * @param transformer the transformer to decorate 56 */ 57 public TransformerPredicate(final Transformer<? super T, Boolean> transformer) { 58 iTransformer = transformer; 59 } 60 61 /** 62 * Evaluates the predicate returning the result of the decorated transformer. 63 * 64 * @param object the input object 65 * @return true if decorated transformer returns Boolean.TRUE 66 * @throws FunctorException if the transformer returns an invalid type 67 */ 68 @Override 69 public boolean evaluate(final T object) { 70 final Boolean result = iTransformer.transform(object); 71 if (result == null) { 72 throw new FunctorException( 73 "Transformer must return an instanceof Boolean, it was a null object"); 74 } 75 return result.booleanValue(); 76 } 77 78 /** 79 * Gets the transformer. 80 * 81 * @return the transformer 82 * @since 3.1 83 */ 84 public Transformer<? super T, Boolean> getTransformer() { 85 return iTransformer; 86 } 87 88 }