View Javadoc
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.collections4.functors;
18  
19  import org.apache.commons.collections4.Transformer;
20  
21  /**
22   * Transformer implementation that returns a clone of the input object.
23   * <p>
24   * Clone is performed using <code>PrototypeFactory.prototypeFactory(input).create()</code>.
25   * </p>
26   * <p>
27   * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> be serializable anymore
28   * in order to prevent potential remote code execution exploits. Please refer to
29   * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
30   * for more details.
31   * </p>
32   *
33   * @since 3.0
34   */
35  public class CloneTransformer<T> implements Transformer<T, T> {
36  
37      /** Singleton predicate instance */
38      @SuppressWarnings("rawtypes") // the singleton instance works for all types
39      public static final Transformer INSTANCE = new CloneTransformer<>();
40  
41      /**
42       * Factory returning the singleton instance.
43       *
44       * @param <T>  the type of the objects to be cloned
45       * @return the singleton instance
46       * @since 3.1
47       */
48      public static <T> Transformer<T, T> cloneTransformer() {
49          return INSTANCE;
50      }
51  
52      /**
53       * Constructor.
54       */
55      private CloneTransformer() {
56          super();
57      }
58  
59      /**
60       * Transforms the input to result by cloning it.
61       *
62       * @param input  the input object to transform
63       * @return the transformed result
64       */
65      @Override
66      public T transform(final T input) {
67          if (input == null) {
68              return null;
69          }
70          return PrototypeFactory.prototypeFactory(input).create();
71      }
72  
73  }