Author: manik.surtani(a)jboss.com
Date: 2009-08-26 06:52:18 -0400 (Wed, 26 Aug 2009)
New Revision: 8205
Modified:
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/util/Immutables.java
Log:
[JBCACHE-1535] (Immutables should be optimised for more set implementations)
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2009-08-26 01:30:29 UTC
(rev 8204)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2009-08-26 10:52:18 UTC
(rev 8205)
@@ -420,7 +420,7 @@
@SuppressWarnings("unchecked")
public Set<Object> getChildrenNames()
{
- return children.isEmpty() ? Collections.emptySet() :
Immutables.immutableSetCopy(children.keySet());
+ return children.isEmpty() ? Collections.emptySet() :
Immutables.immutableSetWrap(new HashSet<Object>(children.keySet()));
}
public Set<K> getKeys()
@@ -429,7 +429,7 @@
{
return Collections.emptySet();
}
- return Immutables.immutableSetCopy(data.keySet());
+ return Immutables.immutableSetWrap(new HashSet<K>(data.keySet()));
}
public boolean containsKey(K key)
Modified: core/trunk/src/main/java/org/jboss/cache/util/Immutables.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/Immutables.java 2009-08-26 01:30:29 UTC
(rev 8204)
+++ core/trunk/src/main/java/org/jboss/cache/util/Immutables.java 2009-08-26 10:52:18 UTC
(rev 8205)
@@ -63,6 +63,9 @@
*/
public static <T> List<T> immutableListCopy(List<? extends T> list)
{
+ if (list == null) return null;
+ if (list.isEmpty()) return Collections.emptyList();
+ if (list.size() == 1) return Collections.singletonList(list.get(0));
return new ImmutableListCopy<T>(list);
}
@@ -120,6 +123,10 @@
*/
public static <T> Set<T> immutableSetCopy(Set<? extends T> set)
{
+ if (set == null) return null;
+ if (set.isEmpty()) return Collections.emptySet();
+ if (set.size() == 1) return Collections.singleton(set.iterator().next());
+
Set<? extends T> copy = attemptKnownSetCopy(set);
if (copy == null)
attemptClone(set);
@@ -153,6 +160,13 @@
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> immutableMapCopy(Map<? extends K, ?
extends V> map)
{
+ if (map == null) return null;
+ if (map.isEmpty()) return Collections.emptyMap();
+ if (map.size() == 1)
+ {
+ Map.Entry<? extends K, ? extends V> me =
map.entrySet().iterator().next();
+ return Collections.singletonMap(me.getKey(), me.getValue());
+ }
Map<? extends K, ? extends V> copy = attemptKnownMapCopy(map);
if (copy == null)
@@ -174,6 +188,9 @@
@SuppressWarnings("unchecked")
public static <T> Collection<T> immutableCollectionCopy(Collection<?
extends T> collection)
{
+ if (collection == null) return null;
+ if (collection.isEmpty()) return Collections.emptySet();
+ if (collection.size() == 1) return
Collections.singleton(collection.iterator().next());
Collection<? extends T> copy = attemptKnownSetCopy(collection);
if (copy == null)
copy = attemptClone(collection);
Show replies by date