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.Factory;
19  import net.sf.collections15.Transformer;
20  
21  import java.io.Serializable;
22  
23  /***
24   * Transformer implementation whose {@link #transform} method uses a {@link
25   * Factory} to create the returned output object.
26   *
27   * @author Stephen Colebourne
28   * @author Chris Lambrou (port to Java 5.0)
29   * @since Collections15 1.0
30   */
31  public class FactoryTransformer <I, O> implements Transformer<I, O>, Serializable
32  {
33  
34      static final long serialVersionUID = 6856835521640159018L;
35  
36      /***
37       * The <code>Factory</code> used to create output objects for the {@link
38       * #transform} method.
39       */
40      private final Factory<O> factory;
41  
42      /***
43       * Factory method that performs validation.
44       *
45       * @param factory the factory to call, not null
46       *
47       * @return the <code>factory</code> transformer
48       *
49       * @throws IllegalArgumentException if the factory is null
50       */
51      public static <I, O> Transformer<I, O> getInstance(Factory<O> factory)
52      {
53          if (factory == null) {
54              throw new IllegalArgumentException("Factory must not be null");
55          }
56          return new FactoryTransformer<I, O>(factory);
57      }
58  
59      /***
60       * Creates a new instance that uses the specified <code>Factory</code> to
61       * create the transform output objects.
62       *
63       * @param factory The <code>Factory</code> used to create the transform
64       *                output objects.
65       *
66       * @throws IllegalArgumentException Thrown if <code>factory</code> is
67       *                                  <code>null</code>.
68       */
69      protected FactoryTransformer(Factory<O> factory)
70      {
71          this.factory = factory;
72      }
73  
74      /***
75       * Transforms the input by ignoring the input and returning a new object
76       * created by the wrapped <code>Factory</code>.
77       *
78       * @param input The input object, which is ignored.
79       *
80       * @return A new object created using the wrapped <code>Factory</code>.
81       */
82      public O transform(I input)
83      {
84          return factory.create();
85      }
86  
87      /***
88       * Returns the wrapped factory.
89       *
90       * @return The wrapped factory.
91       *
92       * @since Collections15 1.0
93       */
94      public Factory<O> getFactory()
95      {
96          return factory;
97      }
98  
99  }