1 18 package org.objectweb.medor.clone.lib; 19 20 import org.objectweb.medor.clone.api.Cloneable; 21 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 43 public class BasicCloneable implements Cloneable { 44 public Object clone() throws CloneNotSupportedException { 45 return clone(null, new HashMap ()); 46 } 47 48 55 public final Object clone(Map obj2clone) throws CloneNotSupportedException { 56 if (obj2clone == null) { 57 return this.clone(null, null); 58 } 59 Object o = obj2clone.get(this); 60 if (o == null) { 61 return this.clone(null, obj2clone); 62 } 63 return o; 64 } 65 66 84 public Object clone(Object clone, 85 Map obj2clone) throws CloneNotSupportedException { 86 if (clone == null) { 87 try { 88 clone = getClass().newInstance(); 89 } catch (Exception e) { 90 throw new CloneNotSupportedException ("Impossible to clone a " 91 + getClass().getName() + " instance: " + e.getMessage()); 92 } 93 } else if (clone.getClass().equals(getClass())) { 94 throw new CloneNotSupportedException ( 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 118 public static Object getClone(Cloneable obj, 119 Map obj2clone) throws CloneNotSupportedException { 120 if (obj == null) { 121 return null; 122 } 123 if (obj2clone == null) { 124 return obj.clone(null, null); 125 } 126 Object o = obj2clone.get(obj); 127 if (o == null) { 128 return obj.clone(null, obj2clone); 129 } 130 return o; 131 } 132 } 133 | Popular Tags |