KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > clone > lib > BasicCloneable


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.medor.clone.lib;
19
20 import org.objectweb.medor.clone.api.Cloneable;
21
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 /**
26  * is a basic implemation of the Cloneable interface. Each cloneable object
27  * should extends this class and override the clone(Object,Map) method such as
28  * the following example:<br/>
29  * public class Toto extends BasicCloneable {
30  * String f1;
31  * Toto ref;
32  *
33  * public Object clone(Object clone, Map obj2clone) throws CloneNotSupportedException {
34  * clone = super.clone(clone, obj2clone);
35  * ((Toto) clone).f1 = f1;
36  * ((Toto) clone).ref = (Toto) getClone(ref, obj2clone);
37  * return clone;
38  * }
39  * }
40  *
41  * @author S.Chassande-Barrioz
42  */

43 public class BasicCloneable implements Cloneable JavaDoc {
44     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
45         return clone(null, new HashMap JavaDoc());
46     }
47
48     /**
49      * Shortcut method permitting to clone the current instance.
50      * @param obj2clone is the cloning context.
51      * @return a clone of the current object (never null)
52      * @throws CloneNotSupportedException if the cloning is not possible due
53      * to a problem.
54      */

55     public final Object JavaDoc clone(Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
56         if (obj2clone == null) {
57             return this.clone(null, null);
58         }
59         Object JavaDoc o = obj2clone.get(this);
60         if (o == null) {
61             return this.clone(null, obj2clone);
62         }
63         return o;
64     }
65
66     /**
67      * Root implementation of the class tree.
68      * If the clone parameter is null, a clone of the current instance is
69      * instanciate throught getClass().newInstance() way.
70      * The clone instance (previoulsy instanciated or the parameter) is
71      * registered in the cloning context if the context is not null.
72      * @param clone is the clone instance to register. If this parameter is
73      * null, a new instance of the current object is instanciated
74      * (getClass().newInstance()). if this parameter is not null, it must be an
75      * instance of the current class.
76      * @param obj2clone is the cloning context used to register the clone. If
77      * this parameter is null, no registration is done.
78      * @return a clone of the current instance.
79      * @throws CloneNotSupportedException if the cloning is not possible due
80      * to a problem. A typically problem is when the clone parameter is null
81      * and when no no arg constructor is availlable in the class of the
82      * current instance.
83      */

84     public Object JavaDoc clone(Object JavaDoc clone,
85                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
86         if (clone == null) {
87             try {
88                 clone = getClass().newInstance();
89             } catch (Exception JavaDoc e) {
90                 throw new CloneNotSupportedException JavaDoc("Impossible to clone a "
91                     + getClass().getName() + " instance: " + e.getMessage());
92             }
93         } else if (clone.getClass().equals(getClass())) {
94             throw new CloneNotSupportedException JavaDoc(
95                 "The clone parameter is not an instance of the current instance,"
96                 + " current: " + getClass().getName()
97                 + ", parameter class: " + clone.getClass().getName());
98         }
99         if (obj2clone != null) {
100             obj2clone.put(this, clone);
101         }
102         return clone;
103     }
104
105     /**
106      * This static method is a shortcut method permitting to fetch the clone of
107      * an object. If the cloning context is not null, this implementation check
108      * if a clone is already available in the context. If yes the clone instance
109      * is returned. Otherwise, the cloning is launch on the obj parameter.
110      * @param obj is the object to clone. If this parameter is null, a null
111      * value is returned.
112      * @param obj2clone is the cloning context
113      * @return an clone instance of the obj parameter. The returned value is null
114      * only if the obj parameter is null.
115      * @throws CloneNotSupportedException if the cloning is not possible due
116      * to a problem.
117      */

118     public static Object JavaDoc getClone(Cloneable JavaDoc obj,
119                            Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
120         if (obj == null) {
121             return null;
122         }
123         if (obj2clone == null) {
124             return obj.clone(null, null);
125         }
126         Object JavaDoc o = obj2clone.get(obj);
127         if (o == null) {
128             return obj.clone(null, obj2clone);
129         }
130         return o;
131     }
132 }
133
Popular Tags