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.transformer;
17  
18  import net.sf.collections15.Closure;
19  import net.sf.collections15.Transformer;
20  
21  import java.io.Serializable;
22  
23  
24  /***
25   * <code>Transformer</code> implementation that invokes a {@link Closure} using
26   * the input object, before returning the input object.
27   *
28   * @author Stephen Colebourne
29   * @author Chris Lambrou (port to Java 5.0)
30   * @since Collections15 1.0
31   */
32  public class ClosureTransformer <E> implements Transformer<E, E>, Serializable
33  {
34  
35      static final long serialVersionUID = 8843037897293234788L;
36  
37      /***
38       * The closure executed on input objects.
39       */
40      private final Closure<? super E> closure;
41  
42      /***
43       * Returns an instance whose transform method executes the specified
44       * <code>Closure</code> on an input object, before returning the input
45       * object as the output.
46       *
47       * @param closure The <code>Closure</code> to execute on input objects.
48       *
49       * @return An instance whose transform method executes the specified
50       *         <code>Closure</code> on an input object, before returning the
51       *         input object as the output.
52       *
53       * @throws IllegalArgumentException Thrown if <code>closure</code> is
54       *                                  <code>null</code>.
55       */
56      public static <T> ClosureTransformer<T> getInstance(Closure<? super T> closure)
57      {
58          return new ClosureTransformer<T>(closure);
59      }
60  
61      /***
62       * Creates a new instance whose transform method executes the specified
63       * <code>Closure</code> on an input object, before returning the input
64       * object as output.
65       *
66       * @param closure The <code>Closure</code> to execute on input objects.
67       *
68       * @throws IllegalArgumentException Thrown if <code>closure</code> is
69       *                                  <code>null</code>.
70       */
71      protected ClosureTransformer(Closure<? super E> closure)
72      {
73          if (closure == null) {
74              throw new IllegalArgumentException("Closure must not be null");
75          }
76          this.closure = closure;
77      }
78  
79      /***
80       * Transforms the input by executing the <code>Closure</code> on the input,
81       * and then returning is as output.
82       *
83       * @param input The input object to transform.
84       *
85       * @return The input object, after the <code>Closure</code> has been
86       *         executed on it.
87       */
88      public E transform(E input)
89      {
90          closure.execute(input);
91          return input;
92      }
93  }