01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
29: *
30: * @author Stephen Colebourne
31: */
32: public class ClosureTransformer implements Transformer, Serializable {
33:
34: /** Serial version UID */
35: private 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(
50: "Closure must not be null");
51: }
52: return new ClosureTransformer(closure);
53: }
54:
55: /**
56: * Constructor that performs no validation.
57: * Use <code>getInstance</code> if you want that.
58: *
59: * @param closure the closure to call, not null
60: */
61: public ClosureTransformer(Closure closure) {
62: super ();
63: iClosure = closure;
64: }
65:
66: /**
67: * Transforms the input to result by executing a closure.
68: *
69: * @param input the input object to transform
70: * @return the transformed result
71: */
72: public Object transform(Object input) {
73: iClosure.execute(input);
74: return input;
75: }
76:
77: /**
78: * Gets the closure.
79: *
80: * @return the closure
81: * @since Commons Collections 3.1
82: */
83: public Closure getClosure() {
84: return iClosure;
85: }
86:
87: }
|