From aa0666e4755bddeea01e5137549b0423a663fc44 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Wed, 25 Nov 2009 22:13:16 +0000 Subject: [PATCH] clone() was broken. If you had a StreamableHashIntMap and cloned it you would get a HashIntMap. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2654 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/HashIntMap.java | 41 +++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/util/HashIntMap.java b/src/java/com/samskivert/util/HashIntMap.java index 32fdd30d..0e5ccfe1 100644 --- a/src/java/com/samskivert/util/HashIntMap.java +++ b/src/java/com/samskivert/util/HashIntMap.java @@ -458,11 +458,23 @@ public class HashIntMap extends AbstractMap @Override public Object clone () { - HashIntMap copy = new HashIntMap(_buckets.length, _loadFactor); - for (IntEntry entry : intEntrySet()) { - copy.put(entry.getIntKey(), entry.getValue()); + try { + @SuppressWarnings("unchecked") + HashIntMap result = (HashIntMap) super.clone(); + result._keySet = null; + Record[] buckets = result._buckets = result._buckets.clone(); + for (int ii = buckets.length - 1; ii >= 0; ii--) { + if (buckets[ii] != null) { + @SuppressWarnings("unchecked") + Record entry = (Record) buckets[ii].clone(); + buckets[ii] = entry; + } + } + return result; + + } catch (CloneNotSupportedException cnse) { + throw new RuntimeException(cnse); // won't happen } - return copy; } /** @@ -513,7 +525,8 @@ public class HashIntMap extends AbstractMap return recs; } - protected static class Record implements Entry, IntEntry + protected static class Record + implements Cloneable, Entry, IntEntry { public Record next; public int key; @@ -566,6 +579,24 @@ public class HashIntMap extends AbstractMap { return key + "=" + StringUtil.toString(value); } + + @Override public Object clone () + { + try { + @SuppressWarnings("unchecked") + Record result = (Record) super.clone(); + // value is not cloned + if (result.next != null) { + @SuppressWarnings("unchecked") + Record next = (Record) result.next.clone(); + result.next = next; + } + return result; + + } catch (CloneNotSupportedException cnse) { + throw new RuntimeException(cnse); // won't happen + } + } } protected Record[] _buckets;