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.Predicate; 022 import org.apache.commons.collections.Transformer; 023 024 /** 025 * Predicate implementation that transforms the given object before invoking 026 * another <code>Predicate</code>. 027 * 028 * @since Commons Collections 3.1 029 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 030 * @author Alban Peignier 031 * @author Stephen Colebourne 032 */ 033 public final class TransformedPredicate implements Predicate, PredicateDecorator, Serializable { 034 035 /** Serial version UID */ 036 private static final long serialVersionUID = -5596090919668315834L; 037 038 /** The transformer to call */ 039 private final Transformer iTransformer; 040 /** The predicate to call */ 041 private final Predicate iPredicate; 042 043 /** 044 * Factory to create the predicate. 045 * 046 * @param transformer the transformer to call 047 * @param predicate the predicate to call with the result of the transform 048 * @return the predicate 049 * @throws IllegalArgumentException if the transformer or the predicate is null 050 */ 051 public static Predicate getInstance(Transformer transformer, Predicate predicate) { 052 if (transformer == null) { 053 throw new IllegalArgumentException("The transformer to call must not be null"); 054 } 055 if (predicate == null) { 056 throw new IllegalArgumentException("The predicate to call must not be null"); 057 } 058 return new TransformedPredicate(transformer, predicate); 059 } 060 061 /** 062 * Constructor that performs no validation. 063 * Use <code>getInstance</code> if you want that. 064 * 065 * @param transformer the transformer to use 066 * @param predicate the predicate to decorate 067 */ 068 public TransformedPredicate(Transformer transformer, Predicate predicate) { 069 iTransformer = transformer; 070 iPredicate = predicate; 071 } 072 073 /** 074 * Evaluates the predicate returning the result of the decorated predicate 075 * once the input has been transformed 076 * 077 * @param object the input object which will be transformed 078 * @return true if decorated predicate returns true 079 */ 080 public boolean evaluate(Object object) { 081 Object result = iTransformer.transform(object); 082 return iPredicate.evaluate(result); 083 } 084 085 /** 086 * Gets the predicate being decorated. 087 * 088 * @return the predicate as the only element in an array 089 * @since Commons Collections 3.1 090 */ 091 public Predicate[] getPredicates() { 092 return new Predicate[] {iPredicate}; 093 } 094 095 /** 096 * Gets the transformer in use. 097 * 098 * @return the transformer 099 */ 100 public Transformer getTransformer() { 101 return iTransformer; 102 } 103 104 }