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.Transformer;
19  import net.sf.collections15.functors.factory.PrototypeFactory;
20  
21  import java.io.Serializable;
22  
23  /***
24   * <code>Transformer</code> implementation that returns a clone of the input
25   * object.
26   * <p/>
27   * Clone is performed using the {@link PrototypeFactory} class.
28   *
29   * @author Stephen Colebourne
30   * @author Chris Lambrou (port to Java 5.0)
31   * @since Collections15 1.0
32   */
33  public class CloneTransformer <E> implements Transformer<E, E>, Serializable
34  {
35  
36      static final long serialVersionUID = -1128592221939925427L;
37  
38      /***
39       * Returns a new <code>CloneTransformer</code> instance.
40       *
41       * @return A new <code>CloneTransformer</code> instance.
42       *
43       * @since Collections15 1.0
44       */
45      public static <E> CloneTransformer<E> getInstance()
46      {
47          return new CloneTransformer<E>();
48      }
49  
50      /***
51       * Creates a new instance.
52       */
53      protected CloneTransformer()
54      {
55      }
56  
57      /***
58       * Transforms the input object, producing a clone of it. The cloning is
59       * implemented by {@link PrototypeFactory#getInstance}.
60       *
61       * @param input The input object to transform.
62       *
63       * @return A clone of the input object.
64       *
65       * @throws IllegalArgumentException If a <code>Factory</code> cannot be
66       *                                  created that is capable of creating a
67       *                                  copy of the input object. See the {@link
68       *                                  PrototypeFactory} class description for
69       *                                  details on the methods used to clone the
70       *                                  input object.
71       */
72      public E transform(E input)
73      {
74          if (input == null) {
75              return null;
76          }
77          return PrototypeFactory.getInstance(input).create();
78      }
79  
80  }