1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.functors;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.collections.Closure;
22 import org.apache.commons.collections.Transformer;
23
24 /**
25 * Closure implementation that calls a Transformer using the input object
26 * and ignore the result.
27 *
28 * @since 3.0
29 * @version $Id: TransformerClosure.java 1429905 2013-01-07 17:15:14Z ggregory $
30 */
31 public class TransformerClosure<E> implements Closure<E>, Serializable {
32
33 /** Serial version UID */
34 private static final long serialVersionUID = -5194992589193388969L;
35
36 /** The transformer to wrap */
37 private final Transformer<? super E, ?> iTransformer;
38
39 /**
40 * Factory method that performs validation.
41 * <p>
42 * A null transformer will return the <code>NOPClosure</code>.
43 *
44 * @param <E> the type that the closure acts on
45 * @param transformer the transformer to call, null means nop
46 * @return the <code>transformer</code> closure
47 */
48 public static <E> Closure<E> transformerClosure(final Transformer<? super E, ?> transformer) {
49 if (transformer == null) {
50 return NOPClosure.<E>nopClosure();
51 }
52 return new TransformerClosure<E>(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(final Transformer<? super E, ?> 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(final E input) {
72 iTransformer.transform(input);
73 }
74
75 /**
76 * Gets the transformer.
77 *
78 * @return the transformer
79 * @since 3.1
80 */
81 public Transformer<? super E, ?> getTransformer() {
82 return iTransformer;
83 }
84
85 }