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 */ 017 package org.apache.commons.collections.functors; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.collections.FunctorException; 022 import org.apache.commons.collections.Predicate; 023 import org.apache.commons.collections.Transformer; 024 025 /** 026 * Predicate implementation that returns the result of a transformer. 027 * 028 * @since Commons Collections 3.0 029 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 030 * 031 * @author Stephen Colebourne 032 */ 033 public final class TransformerPredicate implements Predicate, Serializable { 034 035 /** Serial version UID */ 036 private static final long serialVersionUID = -2407966402920578741L; 037 038 /** The transformer to call */ 039 private final Transformer iTransformer; 040 041 /** 042 * Factory to create the predicate. 043 * 044 * @param transformer the transformer to decorate 045 * @return the predicate 046 * @throws IllegalArgumentException if the transformer is null 047 */ 048 public static Predicate getInstance(Transformer transformer) { 049 if (transformer == null) { 050 throw new IllegalArgumentException("The transformer to call must not be null"); 051 } 052 return new TransformerPredicate(transformer); 053 } 054 055 /** 056 * Constructor that performs no validation. 057 * Use <code>getInstance</code> if you want that. 058 * 059 * @param transformer the transformer to decorate 060 */ 061 public TransformerPredicate(Transformer transformer) { 062 super(); 063 iTransformer = transformer; 064 } 065 066 /** 067 * Evaluates the predicate returning the result of the decorated transformer. 068 * 069 * @param object the input object 070 * @return true if decorated transformer returns Boolean.TRUE 071 * @throws FunctorException if the transformer returns an invalid type 072 */ 073 public boolean evaluate(Object object) { 074 Object result = iTransformer.transform(object); 075 if (result instanceof Boolean == false) { 076 throw new FunctorException( 077 "Transformer must return an instanceof Boolean, it was a " 078 + (result == null ? "null object" : result.getClass().getName())); 079 } 080 return ((Boolean) result).booleanValue(); 081 } 082 083 /** 084 * Gets the transformer. 085 * 086 * @return the transformer 087 * @since Commons Collections 3.1 088 */ 089 public Transformer getTransformer() { 090 return iTransformer; 091 } 092 093 }