Author: jason.greene(a)jboss.com
Date: 2008-08-08 14:43:15 -0400 (Fri, 08 Aug 2008)
New Revision: 6548
Modified:
core/trunk/src/main/java/org/jboss/cache/util/Immutables.java
Log:
Fix type-o
Add fast-path for known collection types
Modified: core/trunk/src/main/java/org/jboss/cache/util/Immutables.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/Immutables.java 2008-08-08 17:05:16 UTC
(rev 6547)
+++ core/trunk/src/main/java/org/jboss/cache/util/Immutables.java 2008-08-08 18:43:15 UTC
(rev 6548)
@@ -28,9 +28,13 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
import java.util.Map.Entry;
/**
@@ -127,11 +131,12 @@
*/
public static <T> Set<T> immutableSetCopy(Set<? extends T> set)
{
- Set<? extends T> copy = attemptClone(set);
+ Set<? extends T> copy = attemptKnownSetCopy(set);
if (copy == null)
+ attemptClone(set);
+ if (copy == null)
// Set uses Collection copy-ctor
copy = attemptCopyConstructor(set, Collection.class);
-
if (copy == null)
copy = new HashSet<T>(set);
@@ -158,13 +163,16 @@
*/
public static <K,V> Map<K,V> immutableMapCopy(Map<? extends K, ?
extends V> map)
{
- Map<? extends K, ? extends V> copy = attemptClone(map);
+ Map<? extends K, ? extends V> copy = attemptKnownMapCopy(map);
+
if (copy == null)
+ attemptClone(map);
+ if (copy == null)
copy = attemptCopyConstructor(map, Map.class);
if (copy == null)
copy = new HashMap<K,V>(map);
- return new ImmutableMapWrapper<K,V>(map);
+ return new ImmutableMapWrapper<K,V>(copy);
}
/**
@@ -175,17 +183,46 @@
*/
public static <T> Collection<T> immutableCollectionCopy(Collection<?
extends T> collection)
{
- Collection<? extends T> copy = attemptClone(collection);
+ Collection<? extends T> copy = attemptKnownSetCopy(collection);
if (copy == null)
+ copy = attemptClone(collection);
+ if (copy == null)
copy = attemptCopyConstructor(collection, Collection.class);
if (copy == null)
copy = new ArrayList<T>(collection);
- return new ImmutableCollectionWrapper<T>(collection);
+ return new ImmutableCollectionWrapper<T>(copy);
}
+ @SuppressWarnings("unchecked")
+ private static <T extends Map> T attemptKnownMapCopy(T map)
+ {
+ if (map instanceof FastCopyHashMap)
+ return (T)((FastCopyHashMap) map).clone();
+ if (map instanceof HashMap)
+ return (T)((HashMap) map).clone();
+ if (map instanceof LinkedHashMap)
+ return (T)((LinkedHashMap) map).clone();
+ if (map instanceof TreeMap)
+ return (T)((TreeMap) map).clone();
+ return null;
+ }
+
@SuppressWarnings("unchecked")
+ private static <T extends Collection> T attemptKnownSetCopy(T set)
+ {
+ if (set instanceof HashSet)
+ return (T)((HashSet) set).clone();
+ if (set instanceof LinkedHashSet)
+ return (T)((LinkedHashSet) set).clone();
+ if (set instanceof TreeSet)
+ return (T)((TreeSet) set).clone();
+
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
private static <T> T attemptClone(T source)
{
if (source instanceof Cloneable)
@@ -537,5 +574,10 @@
{
return new ImmutableCollectionWrapper<V>(map.values());
}
+
+ public String toString()
+ {
+ return map.toString();
+ }
}
}
Show replies by date