Skip to content

Commit

Permalink
1.9.3: can now clone large LinkedHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskougios committed Sep 11, 2016
1 parent b9c2036 commit 54b50ca
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<groupId>uk.com.robust-it</groupId>
<artifactId>cloning</artifactId>
<packaging>bundle</packaging>
<version>1.9.2</version>
<version>1.9.3</version>
<name>cloning</name>
<url>https://code.google.com/p/cloning/</url>
<description><![CDATA[
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/rits/cloning/Cloner.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected void registerFastCloners() {
fastCloners.put(HashSet.class, new FastClonerHashSet());
fastCloners.put(HashMap.class, new FastClonerHashMap());
fastCloners.put(TreeMap.class, new FastClonerTreeMap());
fastCloners.put(LinkedHashMap.class, new FastClonerLinkedHashMap());
fastCloners.put(ConcurrentHashMap.class, new FastClonerConcurrentHashMap());
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/rits/cloning/FastClonerLinkedHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.rits.cloning;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* @author kostantinos.kougios
*
* 21 May 2009
*/
public class FastClonerLinkedHashMap implements IFastCloner {
@SuppressWarnings({"unchecked", "rawtypes"})
public Object clone(final Object t, final IDeepCloner cloner, final Map<Object, Object> clones) {
final LinkedHashMap<?, ?> al = (LinkedHashMap) t;
final LinkedHashMap result = new LinkedHashMap();
for (final Map.Entry e : al.entrySet()) {
final Object key = cloner.deepClone(e.getKey(), clones);
final Object value = cloner.deepClone(e.getValue(), clones);

result.put(key, value);
}
return result;
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/rits/tests/cloning/TestCloner.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,4 +776,32 @@ public void testUnregisterFastCloner() {
cloner.unregisterFastCloner(HashMap.class);
cloner.registerFastCloner(HashMap.class, new FastClonerHashMap());
}

public void testEmptyLinkedHashMap() {
LinkedHashMap<Integer, Integer> m = new LinkedHashMap<Integer, Integer>();
LinkedHashMap<Integer, Integer> cloned = cloner.deepClone(m);
assertEquals(m, cloned);
}

public void testLinkedHashMap() {
LinkedHashMap<Integer, Integer> m = new LinkedHashMap<Integer, Integer>();
for (int i = 1; i < 10000; i++) {
m.put(i, i * 2);
}
LinkedHashMap<Integer, Integer> cloned = cloner.deepClone(m);
assertEquals(m, cloned);
}

public void testLinkedHashMapIterationOrder() {
LinkedHashMap<Integer, Integer> m = new LinkedHashMap<Integer, Integer>();
for (int i = 1000; i >= 1; i--) {
m.put(i, i * 2);
}
LinkedHashMap<Integer, Integer> cloned = cloner.deepClone(m);
Iterator<Integer> it = cloned.keySet().iterator();
for (int i = 1000; i >= 1; i--) {
assertEquals((Integer) i, it.next());
}
}

}

0 comments on commit 54b50ca

Please sign in to comment.