1 16 package org.apache.commons.collections.functors; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.collections.FunctorException; 21 import org.apache.commons.collections.Predicate; 22 import org.apache.commons.collections.Transformer; 23 24 32 public final class TransformerPredicate implements Predicate, Serializable { 33 34 35 static final long serialVersionUID = -2407966402920578741L; 36 37 38 private final Transformer iTransformer; 39 40 47 public static Predicate getInstance(Transformer transformer) { 48 if (transformer == null) { 49 throw new IllegalArgumentException ("The transformer to call must not be null"); 50 } 51 return new TransformerPredicate(transformer); 52 } 53 54 60 public TransformerPredicate(Transformer transformer) { 61 super(); 62 iTransformer = transformer; 63 } 64 65 72 public boolean evaluate(Object object) { 73 Object result = iTransformer.transform(object); 74 if (result instanceof Boolean == false) { 75 throw new FunctorException( 76 "Transformer must return an instanceof Boolean, it was a " 77 + (result == null ? "null object" : result.getClass().getName())); 78 } 79 return ((Boolean ) result).booleanValue(); 80 } 81 82 88 public Transformer getTransformer() { 89 return iTransformer; 90 } 91 92 } 93 | Popular Tags |