KickJava   Java API By Example, From Geeks To Geeks.

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


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.FunctorException;
21 import org.apache.commons.collections.Predicate;
22 import org.apache.commons.collections.Transformer;
23
24 /**
25  * Predicate implementation that returns the result of a transformer.
26  *
27  * @since Commons Collections 3.0
28  * @version $Revision: 1.6 $ $Date: 2004/05/16 11:16:01 $
29  *
30  * @author Stephen Colebourne
31  */

32 public final class TransformerPredicate implements Predicate, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = -2407966402920578741L;
36     
37     /** The transformer to call */
38     private final Transformer iTransformer;
39     
40     /**
41      * Factory to create the predicate.
42      *
43      * @param transformer the transformer to decorate
44      * @return the predicate
45      * @throws IllegalArgumentException if the transformer is null
46      */

47     public static Predicate getInstance(Transformer transformer) {
48         if (transformer == null) {
49             throw new IllegalArgumentException JavaDoc("The transformer to call must not be null");
50         }
51         return new TransformerPredicate(transformer);
52     }
53
54     /**
55      * Constructor that performs no validation.
56      * Use <code>getInstance</code> if you want that.
57      *
58      * @param transformer the transformer to decorate
59      */

60     public TransformerPredicate(Transformer transformer) {
61         super();
62         iTransformer = transformer;
63     }
64
65     /**
66      * Evaluates the predicate returning the result of the decorated transformer.
67      *
68      * @param object the input object
69      * @return true if decorated transformer returns Boolean.TRUE
70      * @throws FunctorException if the transformer returns an invalid type
71      */

72     public boolean evaluate(Object JavaDoc object) {
73         Object JavaDoc result = iTransformer.transform(object);
74         if (result instanceof Boolean JavaDoc == 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 JavaDoc) result).booleanValue();
80     }
81
82     /**
83      * Gets the transformer.
84      *
85      * @return the transformer
86      * @since Commons Collections 3.1
87      */

88     public Transformer getTransformer() {
89         return iTransformer;
90     }
91
92 }
93
Popular Tags