KickJava   Java API By Example, From Geeks To Geeks.

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


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.Closure;
21 import org.apache.commons.collections.Transformer;
22
23 /**
24  * Transformer implementation that calls a Closure 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 ClosureTransformer implements Transformer, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = 478466901448617286L;
36
37     /** The closure to wrap */
38     private final Closure iClosure;
39
40     /**
41      * Factory method that performs validation.
42      *
43      * @param closure the closure to call, not null
44      * @return the <code>closure</code> transformer
45      * @throws IllegalArgumentException if the closure is null
46      */

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

60     public ClosureTransformer(Closure closure) {
61         super();
62         iClosure = closure;
63     }
64
65     /**
66      * Transforms the input to result by executing a closure.
67      *
68      * @param input the input object to transform
69      * @return the transformed result
70      */

71     public Object JavaDoc transform(Object JavaDoc input) {
72         iClosure.execute(input);
73         return input;
74     }
75
76     /**
77      * Gets the closure.
78      *
79      * @return the closure
80      * @since Commons Collections 3.1
81      */

82     public Closure getClosure() {
83         return iClosure;
84     }
85
86 }
87
Popular Tags