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: * Closure implementation that calls a Transformer using the input object
25: * and ignore the result.
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 TransformerClosure implements Closure, Serializable {
33:
34: /** Serial version UID */
35: private static final long serialVersionUID = -5194992589193388969L;
36:
37: /** The transformer to wrap */
38: private final Transformer iTransformer;
39:
40: /**
41: * Factory method that performs validation.
42: * <p>
43: * A null transformer will return the <code>NOPClosure</code>.
44: *
45: * @param transformer the transformer to call, null means nop
46: * @return the <code>transformer</code> closure
47: */
48: public static Closure getInstance(Transformer transformer) {
49: if (transformer == null) {
50: return NOPClosure.INSTANCE;
51: }
52: return new TransformerClosure(transformer);
53: }
54:
55: /**
56: * Constructor that performs no validation.
57: * Use <code>getInstance</code> if you want that.
58: *
59: * @param transformer the transformer to call, not null
60: */
61: public TransformerClosure(Transformer transformer) {
62: super ();
63: iTransformer = transformer;
64: }
65:
66: /**
67: * Executes the closure by calling the decorated transformer.
68: *
69: * @param input the input object
70: */
71: public void execute(Object input) {
72: iTransformer.transform(input);
73: }
74:
75: /**
76: * Gets the transformer.
77: *
78: * @return the transformer
79: * @since Commons Collections 3.1
80: */
81: public Transformer getTransformer() {
82: return iTransformer;
83: }
84:
85: }
|