Brian Jones <[EMAIL PROTECTED]> writes:

   "Jochen Hoenicke" <[EMAIL PROTECTED]> writes:

   > Hello,
   > 
   > The clone method in java.util.Vector and some other collection
   > classes is incompatible with the behaviour of the Sun Java methods:
   > 
   > When I extend java.util.Vector and clone that class, the result is a
   > java.util.Vector and not the extended class.  This is also true, when
   > I clone java.util.Stack.

   Jochen,

   If you'd like to submit a diff, I'll add it to the CVS tree.  The same
   for anybody who wants to point out a problem, but thanks very much for
   the heads up on this.

   Brian

Okay here is a patch for Vector, Hashtable, HashMap and HashSet.  I
don't have a working japhar, yet, so I can't test if it even
compiles.  But the patch is quite trivial and should be correct.

diff -u classpath/java/util/HashMap.java classpath/java/util/new/HashMap.java
--- classpath/java/util/HashMap.java    Fri Nov 13 16:39:19 1998
+++ classpath/java/util/new/HashMap.java        Sat Nov 14 13:08:42 1998
@@ -212,15 +212,14 @@
      */
     public Object clone()
     {
-       Map.Entry entry;
-       Iterator it = entrySet().iterator();
-       HashMap clone = new HashMap(capacity, loadFactor);
-       while (it.hasNext())
-           {
-               entry = (Map.Entry) it.next();
-               clone.internalPut(entry.getKey(), entry.getValue());
-           }
-       return clone;
+        try {
+            HashMap clone = (HashMap) super.clone();
+            clone.clear();
+            clone.putAll(this);
+            return clone;
+        } catch (CloneNotSupportedException ex) {
+            return null;
+        }
     }
 
     /** returns a "set view" of this HashMap's keys */
diff -u classpath/java/util/HashSet.java classpath/java/util/new/HashSet.java
--- classpath/java/util/HashSet.java    Fri Nov 13 16:39:19 1998
+++ classpath/java/util/new/HashSet.java        Sat Nov 14 13:07:25 1998
@@ -133,11 +133,13 @@
      */
     public Object clone()
     {
-       Iterator it = iterator();
-       HashSet clone = new HashSet(map.capacity, map.loadFactor);
-       while (it.hasNext())
-           clone.internalAdd(it.next());
-       return clone;
+        try {
+            HashSet clone = (HashSet) super.clone();
+            clone.map = (HashMap) map.clone();
+            return clone;
+        } catch (CloneNotSupportedException ex) {
+            return null;
+        }
     }
 
     /**
diff -u classpath/java/util/Hashtable.java classpath/java/util/new/Hashtable.java
--- classpath/java/util/Hashtable.java  Fri Nov 13 16:39:43 1998
+++ classpath/java/util/new/Hashtable.java      Sat Nov 14 13:06:35 1998
@@ -474,15 +474,14 @@
      */
     public synchronized Object clone()
     {
-       Map.Entry entry;
-       Iterator it = entrySet().iterator();
-       Hashtable clone = new Hashtable(capacity, loadFactor);
-       while (it.hasNext())
-           {
-               entry = (Map.Entry) it.next();
-               clone.internalPut(entry.getKey(), entry.getValue());
-           }
-       return clone;
+        try {
+            Hashtable clone = (Hashtable) super.clone();
+            clone.clear();
+            clone.putAll(this);
+            return clone;
+        } catch (CloneNotSupportedException ex) {
+            return null;
+        }
     }
 
     /**
diff -u classpath/java/util/Vector.java classpath/java/util/new/Vector.java
--- classpath/java/util/Vector.java     Fri Nov 13 16:40:34 1998
+++ classpath/java/util/new/Vector.java Sat Nov 14 12:59:45 1998
@@ -453,7 +453,14 @@
    * Creates a new Vector with the same contents as this one.
    */
   public Object clone() {
-    return new Vector(this);
+    try {
+      Vector clone = (Vector) super.clone();
+      clone.elementData = new Object[elementCount];
+      System.arraycopy(elementData, 0, clone.elementData, 0, elementCount);
+      return clone;
+    } catch (CloneNotSupportedException ex) {
+      return null;
+    }
   }
 
   /**

Reply via email to