KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > TransformedPredicate


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.functors;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.apache.commons.collections.Predicate;
21 import org.apache.commons.collections.Transformer;
22
23 /**
24  * Predicate implementation that transforms the given object before invoking
25  * another <code>Predicate</code>.
26  *
27  * @since Commons Collections 3.1
28  * @version $Revision: 1.4 $ $Date: 2004/05/31 16:43:17 $
29  * @author Alban Peignier
30  * @author Stephen Colebourne
31  */

32 public final class TransformedPredicate implements Predicate, PredicateDecorator, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = -5596090919668315834L;
36     
37     /** The transformer to call */
38     private final Transformer iTransformer;
39     /** The predicate to call */
40     private final Predicate iPredicate;
41
42     /**
43      * Factory to create the predicate.
44      *
45      * @param transformer the transformer to call
46      * @param predicate the predicate to call with the result of the transform
47      * @return the predicate
48      * @throws IllegalArgumentException if the transformer or the predicate is null
49      */

50     public static Predicate getInstance(Transformer transformer, Predicate predicate) {
51         if (transformer == null) {
52             throw new IllegalArgumentException JavaDoc("The transformer to call must not be null");
53         }
54         if (predicate == null) {
55             throw new IllegalArgumentException JavaDoc("The predicate to call must not be null");
56         }
57         return new TransformedPredicate(transformer, predicate);
58     }
59
60     /**
61      * Constructor that performs no validation.
62      * Use <code>getInstance</code> if you want that.
63      *
64      * @param transformer the transformer to use
65      * @param predicate the predicate to decorate
66      */

67     public TransformedPredicate(Transformer transformer, Predicate predicate) {
68         iTransformer = transformer;
69         iPredicate = predicate;
70     }
71     
72     /**
73      * Evaluates the predicate returning the result of the decorated predicate
74      * once the input has been transformed
75      *
76      * @param object the input object which will be transformed
77      * @return true if decorated predicate returns true
78      */

79     public boolean evaluate(Object JavaDoc object) {
80         Object JavaDoc result = iTransformer.transform(object);
81         return iPredicate.evaluate(result);
82     }
83
84     /**
85      * Gets the predicate being decorated.
86      *
87      * @return the predicate as the only element in an array
88      * @since Commons Collections 3.1
89      */

90     public Predicate[] getPredicates() {
91         return new Predicate[] {iPredicate};
92     }
93
94     /**
95      * Gets the transformer in use.
96      *
97      * @return the transformer
98      */

99     public Transformer getTransformer() {
100         return iTransformer;
101     }
102
103 }
104
Popular Tags