View Javadoc
1   /*
2    *  Copyright 2013 Chris Pheby
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 org.jadira.reflection.cloning.api;
17  
18  import java.util.IdentityHashMap;
19  
20  /**
21   * A CloneImplementor represents an atomic cloning capability. The interface is implemented by
22   * classes that can clone a specific type, as well as by {@link CloneStrategy} and
23   * {@link CloneDriver} implementations. In general users do not work with the methods on this class,
24   * using {@link Cloner} instead
25   */
26  public interface CloneImplementor {
27  
28  	/**
29  	 * Create a new, uninitialised instance of the given class (if supported). Depending on
30  	 * strategy, this may or may not initialise instance fields, and may or may not invoke
31  	 * constructors.
32  	 * @param c Type to create instance of
33  	 * @param <T> The type of the instance to be constructed
34  	 * @return The new instance
35  	 */
36  	<T> T newInstance(final Class<T> c);
37  
38  	/**
39  	 * True if this implementor can clone the given class
40  	 * @param clazz The class
41  	 * @return True if cloneable, false otherwise
42  	 */
43  	boolean canClone(Class<?> clazz);
44  
45  	/**
46  	 * Performs a clone.
47  	 * @param obj Object to clone
48  	 * @param context The CloneDriver that initiated the request
49  	 * @param referencesToReuse Any references for objects already cloned to ensure reference
50  	 *            identity is preserved
51  	 * @param stackDepth The number of objects already cloned in the current data structure
52  	 * @param <T> The type of the object to be cloned
53  	 * @return The cloned object
54  	 */
55  	<T> T clone(T obj, CloneDriver context, IdentityHashMap<Object, Object> referencesToReuse, long stackDepth);
56  }