001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.functors; 018 019import java.io.Serializable; 020 021import org.apache.commons.collections4.FunctorException; 022import org.apache.commons.collections4.Predicate; 023import org.apache.commons.collections4.Transformer; 024 025/** 026 * Predicate implementation that returns the result of a transformer. 027 * 028 * @since 3.0 029 * @version $Id: TransformerPredicate.html 972421 2015-11-14 20:00:04Z tn $ 030 */ 031public final class TransformerPredicate<T> implements Predicate<T>, Serializable { 032 033 /** Serial version UID */ 034 private static final long serialVersionUID = -2407966402920578741L; 035 036 /** The transformer to call */ 037 private final Transformer<? super T, Boolean> iTransformer; 038 039 /** 040 * Factory to create the predicate. 041 * 042 * @param <T> the type that the predicate queries 043 * @param transformer the transformer to decorate 044 * @return the predicate 045 * @throws IllegalArgumentException if the transformer is null 046 */ 047 public static <T> Predicate<T> transformerPredicate(final Transformer<? super T, Boolean> transformer) { 048 if (transformer == null) { 049 throw new IllegalArgumentException("The transformer to call must not be null"); 050 } 051 return new TransformerPredicate<T>(transformer); 052 } 053 054 /** 055 * Constructor that performs no validation. 056 * Use <code>transformerPredicate</code> if you want that. 057 * 058 * @param transformer the transformer to decorate 059 */ 060 public TransformerPredicate(final Transformer<? super T, Boolean> transformer) { 061 super(); 062 iTransformer = transformer; 063 } 064 065 /** 066 * Evaluates the predicate returning the result of the decorated transformer. 067 * 068 * @param object the input object 069 * @return true if decorated transformer returns Boolean.TRUE 070 * @throws FunctorException if the transformer returns an invalid type 071 */ 072 public boolean evaluate(final T object) { 073 final Boolean result = iTransformer.transform(object); 074 if (result == null) { 075 throw new FunctorException( 076 "Transformer must return an instanceof Boolean, it was a null object"); 077 } 078 return result.booleanValue(); 079 } 080 081 /** 082 * Gets the transformer. 083 * 084 * @return the transformer 085 * @since 3.1 086 */ 087 public Transformer<? super T, Boolean> getTransformer() { 088 return iTransformer; 089 } 090 091}