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()}.
25   * </p>
26   * <p>
27   * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> 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   * @param <T> the type of the input and result to the function.
34   * @since 3.0
35   */
36  public class CloneTransformer<T> implements Transformer<T, T> {
37  
38      /** Singleton predicate instance */
39      @SuppressWarnings("rawtypes") // the singleton instance works for all types
40      public static final Transformer INSTANCE = new CloneTransformer<>();
41  
42      /**
43       * Factory returning the singleton instance.
44       *
45       * @param <T>  the type of the objects to be cloned
46       * @return the singleton instance
47       * @since 3.1
48       */
49      public static <T> Transformer<T, T> cloneTransformer() {
50          return INSTANCE;
51      }
52  
53      /**
54       * Constructs a new instance.
55       */
56      private CloneTransformer() {
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).get();
71      }
72  
73  }