View Javadoc

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 net.sf.collections15.functors.closure;
17  
18  import net.sf.collections15.Closure;
19  import net.sf.collections15.Transformer;
20  
21  import java.io.Serializable;
22  
23  
24  /***
25   * Closure implementation that calls a <code>Transformer</code> using the input
26   * object and ignore the result.
27   *
28   * @author Stephen Colebourne
29   * @author Chris Lambrou (port to Java 5.0)
30   * @since Collections15 1.0
31   */
32  public class TransformerClosure <E> implements Closure<E>, Serializable
33  {
34  
35      static final long serialVersionUID = -9059061355754406104L;
36  
37      /***
38       * The <code>Transformer</code> to wrap.
39       */
40      private final Transformer<? super E, ?> transformer;
41  
42      /***
43       * Returns a <code>TransformerClosure</code> instance whose {@link #execute
44       * execute} method transforms the input argument using a specified
45       * <code>Transformer</code>, and ignores the output value.
46       *
47       * @param transformer The <code>Transformer</code> whose {@link
48       *                    Transformer#transform} method is called when the
49       *                    <code>Closure</code> is executed.
50       *
51       * @return A new <code>Closure</code> whose <code>execute</code> method
52       *         transforms the input object, discarding the result.
53       *
54       * @throws IllegalArgumentException Thrown if the specified <code>Transformer</code>
55       *                                  is <code>null</code>.
56       */
57      public static <T> TransformerClosure<T> getInstance(Transformer<? super T, ?> transformer)
58      {
59          return new TransformerClosure<T>(transformer);
60      }
61  
62      /***
63       * Creates a new instance whose {@link #execute execute} method transforms
64       * the input argument using a specified <code>Transformer</code>, and
65       * ignores the output value.
66       *
67       * @param transformer The <code>Transformer</code> whose {@link
68       *                    Transformer#transform} method is called when the
69       *                    <code>Closure</code> is executed.
70       *
71       * @throws IllegalArgumentException Thrown if the specified <code>Transformer</code>
72       *                                  is <code>null</code>.
73       */
74      protected TransformerClosure(Transformer<? super E, ?> transformer)
75      {
76          if (transformer == null) {
77              throw new IllegalArgumentException("null transformer");
78          }
79          this.transformer = transformer;
80      }
81  
82      /***
83       * Executes the closure by calling the {@link Transformer#transform
84       * transform} method of the wrapped <code>Transformer</code>, ignoring its
85       * return value.
86       *
87       * @param input The input object to act upon.
88       */
89      public void execute(E input)
90      {
91          transformer.transform(input);
92      }
93  
94      /***
95       * Gets the wrapped <code>Transformer</code>.
96       *
97       * @return The <wrapped code>Transformer</code>.
98       *
99       * @since Collections15 1.0
100      */
101     public Transformer<? super E, ?> getTransformer()
102     {
103         return transformer;
104     }
105 
106 }