KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Transformer implementation that calls a Predicate using the input object
25  * and then returns the input.
26  *
27  * @since Commons Collections 3.0
28  * @version $Revision: 1.5 $ $Date: 2004/05/16 11:36:31 $
29  *
30  * @author Stephen Colebourne
31  */

32 public class PredicateTransformer implements Transformer, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = 5278818408044349346L;
36
37     /** The closure to wrap */
38     private final Predicate iPredicate;
39
40     /**
41      * Factory method that performs validation.
42      *
43      * @param predicate the predicate to call, not null
44      * @return the <code>predicate</code> transformer
45      * @throws IllegalArgumentException if the predicate is null
46      */

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

60     public PredicateTransformer(Predicate predicate) {
61         super();
62         iPredicate = predicate;
63     }
64
65     /**
66      * Transforms the input to result by calling a predicate.
67      *
68      * @param input the input object to transform
69      * @return the transformed result
70      */

71     public Object JavaDoc transform(Object JavaDoc input) {
72         return (iPredicate.evaluate(input) ? Boolean.TRUE : Boolean.FALSE);
73     }
74
75     /**
76      * Gets the predicate.
77      *
78      * @return the predicate
79      * @since Commons Collections 3.1
80      */

81     public Predicate getPredicate() {
82         return iPredicate;
83     }
84
85 }
86
Popular Tags