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

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Thu Aug 20 19:21:31 EDT 2009


Author: manik.surtani at jboss.com
Date: 2009-08-20 19:21:30 -0400 (Thu, 20 Aug 2009)
New Revision: 713

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

Modified: trunk/core/src/main/java/org/infinispan/util/FastCopyHashMap.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/FastCopyHashMap.java	2009-08-20 23:04:13 UTC (rev 712)
+++ trunk/core/src/main/java/org/infinispan/util/FastCopyHashMap.java	2009-08-20 23:21:30 UTC (rev 713)
@@ -21,6 +21,10 @@
  */
 package org.infinispan.util;
 
+import org.infinispan.marshall.Ids;
+import org.infinispan.marshall.Marshallable;
+import org.infinispan.marshall.exts.MapExternalizer;
+
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.AbstractCollection;
@@ -32,10 +36,6 @@
 import java.util.NoSuchElementException;
 import java.util.Set;
 
-import org.infinispan.marshall.Marshallable;
-import org.infinispan.marshall.Ids;
-import org.infinispan.marshall.exts.MapExternalizer;
-
 /**
  * A HashMap that is optimized for fast shallow copies.
  * <p/>
@@ -563,7 +563,7 @@
       return values;
    }
 
-   private final class Values extends AbstractCollection<V> {
+   public final class Values extends AbstractCollection<V> {
       public Iterator<V> iterator() {
          return new ValueIterator();
       }
@@ -586,7 +586,7 @@
       return keySet;
    }
 
-   private class KeySet extends AbstractSet<K> {
+   public class KeySet extends AbstractSet<K> {
       public Iterator<K> iterator() {
          return new KeyIterator();
       }
@@ -615,7 +615,7 @@
       return entrySet;
    }
 
-   private class EntrySet extends AbstractSet<Map.Entry<K, V>> {
+   public class EntrySet extends AbstractSet<Map.Entry<K, V>> {
       public Iterator<Map.Entry<K, V>> iterator() {
          return new EntryIterator();
       }

Modified: trunk/core/src/main/java/org/infinispan/util/Immutables.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/Immutables.java	2009-08-20 23:04:13 UTC (rev 712)
+++ trunk/core/src/main/java/org/infinispan/util/Immutables.java	2009-08-20 23:21:30 UTC (rev 713)
@@ -21,6 +21,13 @@
  */
 package org.infinispan.util;
 
+import org.infinispan.container.DataContainer;
+import org.infinispan.container.entries.InternalCacheEntry;
+import org.infinispan.container.entries.InternalCacheValue;
+import org.infinispan.marshall.Ids;
+import org.infinispan.marshall.MarshallUtil;
+import org.infinispan.marshall.Marshallable;
+
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -36,13 +43,6 @@
 import java.util.Map.Entry;
 import java.util.Set;
 
-import org.infinispan.container.DataContainer;
-import org.infinispan.container.entries.InternalCacheEntry;
-import org.infinispan.container.entries.InternalCacheValue;
-import org.infinispan.marshall.Ids;
-import org.infinispan.marshall.MarshallUtil;
-import org.infinispan.marshall.Marshallable;
-
 /**
  * Factory for generating immutable type wrappers.
  *
@@ -183,7 +183,7 @@
 
       return new ImmutableCollectionWrapper<T>(copy);
    }
-   
+
    /**
     * Wraps a collection with an immutable collection. There is no copying involved.
     *
@@ -215,17 +215,17 @@
 
       return new ImmutableReversibleOrderedSetWrapper<T>(copy);
    }
-   
+
    /**
     * Wraps a {@link Map.Entry}} with an immutable {@link Map.Entry}}. There is no copying involved.
     *
     * @param entry the mapping to wrap.
     * @return an immutable {@link Map.Entry}} wrapper that delegates to the original mapping.
     */
-   public static Map.Entry immutableEntry(Map.Entry entry) {
-      return new ImmutableEntry(entry);
+   public static <K, V> Map.Entry<K, V> immutableEntry(Map.Entry<K, V> entry) {
+      return new ImmutableEntry<K, V>(entry);
    }
-   
+
    /**
     * Wraps a {@link InternalCacheEntry}} with an immutable {@link InternalCacheEntry}}. There is no copying involved.
     *
@@ -361,8 +361,8 @@
       }
    }
 
-   /** 
-    * Immutable version of Map.Entry for traversing immutable collections. 
+   /**
+    * Immutable version of Map.Entry for traversing immutable collections.
     */
    private static class ImmutableEntry<K, V> implements Entry<K, V>, Immutable {
       private K key;
@@ -408,9 +408,9 @@
          return getKey() + "=" + getValue();
       }
    }
-   
-   /** 
-    * Immutable version of InternalCacheEntry for traversing data containers. 
+
+   /**
+    * Immutable version of InternalCacheEntry for traversing data containers.
     */
    private static class ImmutableInternalCacheEntry implements InternalCacheEntry, Immutable {
       private final InternalCacheEntry entry;
@@ -527,21 +527,21 @@
       }
 
       public void setRemoved(boolean removed) {
-         throw new UnsupportedOperationException();         
+         throw new UnsupportedOperationException();
       }
 
       public void setValid(boolean valid) {
          throw new UnsupportedOperationException();
       }
-      
+
       public InternalCacheEntry clone() {
          return new ImmutableInternalCacheEntry(entry.clone());
       }
    }
-   
+
    private static class ImmutableInternalCacheValue implements InternalCacheValue, Immutable {
       private final ImmutableInternalCacheEntry entry;
-      
+
       ImmutableInternalCacheValue(ImmutableInternalCacheEntry entry) {
          this.entry = entry;
       }
@@ -687,12 +687,13 @@
       public String toString() {
          return map.toString();
       }
-      
+
       public static class Externalizer implements org.infinispan.marshall.Externalizer {
          public void writeObject(ObjectOutput output, Object subject) throws IOException {
-            MarshallUtil.marshallMap((Map) subject, output);      
+            MarshallUtil.marshallMap((Map) subject, output);
          }
 
+         @SuppressWarnings("unchecked")
          public Object readObject(ObjectInput input) throws IOException, ClassNotFoundException {
             Map map = new HashMap();
             MarshallUtil.unmarshallMap(map, input);

Modified: trunk/core/src/main/java/org/infinispan/util/ObjectDuplicator.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/util/ObjectDuplicator.java	2009-08-20 23:04:13 UTC (rev 712)
+++ trunk/core/src/main/java/org/infinispan/util/ObjectDuplicator.java	2009-08-20 23:21:30 UTC (rev 713)
@@ -1,6 +1,7 @@
 package org.infinispan.util;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -23,6 +24,12 @@
          return (Map<K, V>) ((HashMap) original).clone();
       if (original instanceof TreeMap)
          return (Map<K, V>) ((TreeMap) original).clone();
+      if (original.getClass().equals(Collections.emptyMap().getClass()))
+         return Collections.emptyMap();
+      if (original.getClass().equals(Collections.singletonMap("", "").getClass())) {
+         Map.Entry<K, V> e = original.entrySet().iterator().next();
+         return Collections.singletonMap(e.getKey(), e.getValue());
+      }
       return attemptClone(original);
    }
 
@@ -32,6 +39,14 @@
          return (Set<E>) ((HashSet) original).clone();
       if (original instanceof TreeSet)
          return (Set<E>) ((TreeSet) original).clone();
+      if (original instanceof FastCopyHashMap.EntrySet || original instanceof FastCopyHashMap.KeySet)
+         return new HashSet<E>(original);
+      if (original.getClass().equals(Collections.emptySet().getClass()))
+         return Collections.emptySet();
+      if (original.getClass().equals(Collections.singleton("").getClass()))
+         return Collections.singleton(original.iterator().next());
+      if (original.getClass().getSimpleName().contains("$"))
+         return new HashSet<E>(original);
 
       return attemptClone(original);
    }



More information about the infinispan-commits mailing list