[jbosscache-commits] JBoss Cache SVN: r6419 - core/trunk/src/main/java/org/jboss/cache/util.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Jul 29 06:21:39 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-07-29 06:21:39 -0400 (Tue, 29 Jul 2008)
New Revision: 6419

Modified:
   core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java
   core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java
Log:
Fixed range check and made externalizable

Modified: core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java	2008-07-29 10:16:53 UTC (rev 6418)
+++ core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java	2008-07-29 10:21:39 UTC (rev 6419)
@@ -157,7 +157,7 @@
 
    public final E get(int index)
    {
-      assertIndexInRange(index);
+      if (index >= size || index < 0) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
       return elements[index];
    }
 
@@ -219,11 +219,6 @@
       return new ImmutableSubList<E>(fromIndex, toIndex);
    }
 
-   private void assertIndexInRange(int index)
-   {
-      if (index >= size || index < 0) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
-   }
-
    private class ImmutableIterator implements ListIterator<E>
    {
       int cursor = 0;
@@ -314,15 +309,10 @@
       @SuppressWarnings("unchecked")
       public final E get(int index)
       {
-         rangeCheck(index);
+         if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
          return (E) ImmutableListCopy.this.get(index + offset);
       }
 
-      private void rangeCheck(int index)
-      {
-         if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ",Size: " + size);
-      }
-
       public final int size()
       {
          return size;
@@ -355,7 +345,8 @@
       @Override
       public final ListIterator<E> listIterator(final int index)
       {
-         rangeCheck(index);
+         if (index < 0 || (index != 0 && index >= size))
+            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
 
          return new ListIterator<E>()
          {

Modified: core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java	2008-07-29 10:16:53 UTC (rev 6418)
+++ core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java	2008-07-29 10:21:39 UTC (rev 6419)
@@ -2,6 +2,10 @@
 
 import net.jcip.annotations.Immutable;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.util.AbstractMap;
 import java.util.Map;
 import java.util.Set;
@@ -20,11 +24,18 @@
  * @since 3.0
  */
 @Immutable
-public class ImmutableMapCopy<K, V> extends AbstractMap<K, V>
+public class ImmutableMapCopy<K, V> extends AbstractMap<K, V> implements Externalizable // externalizable for client code that may serialize this map
 {
-   private final Entry<K, V>[] table;
-   private final int size;
+   private Entry<K, V>[] table;
+   private int size;
 
+   /**
+    * For Externalizable
+    */
+   public ImmutableMapCopy()
+   {
+   }
+
    @SuppressWarnings("unchecked")
    public ImmutableMapCopy(Map<K, V> map)
    {
@@ -66,6 +77,42 @@
       return new ImmutableSetCopy<Map.Entry<K, V>>(table);
    }
 
+   /**
+    * Format:
+    * - entry array size (int)
+    * - entry key-value pairs (Object, Object)
+    *
+    * @param out stream to write to
+    * @throws IOException
+    */
+   public void writeExternal(ObjectOutput out) throws IOException
+   {
+      out.writeInt(size);
+      for (Entry e : table)
+      {
+         out.writeObject(e.getKey());
+         out.writeObject(e.getValue());
+      }
+   }
+
+   /**
+    * See {@link #writeExternal(java.io.ObjectOutput)} for serialization format
+    *
+    * @param in stream
+    * @throws IOException
+    * @throws ClassNotFoundException
+    */
+   @SuppressWarnings("unchecked")
+   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+   {
+      size = in.readInt();
+      table = new Entry[size];
+      for (int i = 0; i < size; i++)
+      {
+         table[i] = new ImmutableEntry(in.readObject(), in.readObject());
+      }
+   }
+
    private static class ImmutableEntry<K, V> implements Map.Entry<K, V>
    {
       K k;
@@ -77,6 +124,12 @@
          v = entry.getValue();
       }
 
+      private ImmutableEntry(K k, V v)
+      {
+         this.k = k;
+         this.v = v;
+      }
+
       public K getKey()
       {
          return k;




More information about the jbosscache-commits mailing list