[infinispan-commits] Infinispan SVN: r737 - trunk/core/src/main/java/org/infinispan/util.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Wed Aug 26 06:52:42 EDT 2009


Author: manik.surtani at jboss.com
Date: 2009-08-26 06:52:42 -0400 (Wed, 26 Aug 2009)
New Revision: 737

Modified:
   trunk/core/src/main/java/org/infinispan/util/Immutables.java
Log:
[ISPN-166]  Immutables should be optimised for more set implementations

Modified: trunk/core/src/main/java/org/infinispan/util/Immutables.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/Immutables.java	2009-08-25 19:21:21 UTC (rev 736)
+++ trunk/core/src/main/java/org/infinispan/util/Immutables.java	2009-08-26 10:52:42 UTC (rev 737)
@@ -35,6 +35,7 @@
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -78,6 +79,9 @@
     * @return the immutable copy
     */
    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);
    }
 
@@ -130,6 +134,9 @@
     * @return an immutable set copy
     */
    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 = ObjectDuplicator.duplicateSet(set);
       if (copy == null)
          // Set uses Collection copy-ctor
@@ -158,6 +165,13 @@
     * @return an immutable map copy
     */
    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 = ObjectDuplicator.duplicateMap(map);
 
       if (copy == null)
@@ -175,6 +189,10 @@
     * @return an immutable copy
     */
    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 = ObjectDuplicator.duplicateCollection(collection);
       if (copy == null)
          copy = attemptCopyConstructor(collection, Collection.class);



More information about the infinispan-commits mailing list