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

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Jul 30 05:10:14 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-07-30 05:10:13 -0400 (Wed, 30 Jul 2008)
New Revision: 6427

Modified:
   core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java
   core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java
   core/trunk/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java
Log:
Made immutable collections 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-30 09:01:40 UTC (rev 6426)
+++ core/trunk/src/main/java/org/jboss/cache/util/ImmutableListCopy.java	2008-07-30 09:10:13 UTC (rev 6427)
@@ -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.lang.reflect.Array;
 import java.util.AbstractList;
 import java.util.Collection;
@@ -25,10 +29,11 @@
  * @since 3.0
  */
 @Immutable
-public class ImmutableListCopy<E> extends AbstractList<E>
+public class ImmutableListCopy<E> extends AbstractList<E> implements Externalizable
 {
-   private final E[] elements;
-   private final int size;
+   private static final long serialVersionUID = 10929568968966L;
+   private E[] elements;
+   private int size;
 
    /**
     * Only one copy constructor since the list is immutable.
@@ -219,6 +224,35 @@
       return new ImmutableSubList<E>(fromIndex, toIndex);
    }
 
+   /**
+    * Format:
+    * - entry array size (int)
+    * - elements (Object)
+    *
+    * @param out stream to write to
+    * @throws IOException
+    */
+   public void writeExternal(ObjectOutput out) throws IOException
+   {
+      out.writeInt(size);
+      for (E e : elements) out.writeObject(e);
+   }
+
+   /**
+    * 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();
+      elements = (E[]) new Object[size];
+      for (int i = 0; i < size; i++) elements[i] = (E) in.readObject();
+   }
+
    private class ImmutableIterator implements ListIterator<E>
    {
       int cursor = 0;

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-30 09:01:40 UTC (rev 6426)
+++ core/trunk/src/main/java/org/jboss/cache/util/ImmutableMapCopy.java	2008-07-30 09:10:13 UTC (rev 6427)
@@ -26,6 +26,7 @@
 @Immutable
 public class ImmutableMapCopy<K, V> extends AbstractMap<K, V> implements Externalizable // externalizable for client code that may serialize this map
 {
+   private static final long serialVersionUID = 10929568968766L;
    private Entry<K, V>[] table;
    private int size;
 

Modified: core/trunk/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java	2008-07-30 09:01:40 UTC (rev 6426)
+++ core/trunk/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java	2008-07-30 09:10:13 UTC (rev 6427)
@@ -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.AbstractSet;
 import java.util.Collection;
 import java.util.Iterator;
@@ -23,10 +27,11 @@
  * @since 3.0
  */
 @Immutable
-public class ImmutableSetCopy<E> extends AbstractSet<E>
+public class ImmutableSetCopy<E> extends AbstractSet<E> implements Externalizable
 {
-   private final E[] elements;
-   private final int size;
+   private static final long serialVersionUID = 11929568968766L;
+   private E[] elements;
+   private int size;
 
    @SuppressWarnings("unchecked")
    public ImmutableSetCopy(Collection<E> set)
@@ -106,4 +111,33 @@
    {
       return size;
    }
+
+   /**
+    * Format:
+    * - entry array size (int)
+    * - elements (Object)
+    *
+    * @param out stream to write to
+    * @throws IOException
+    */
+   public void writeExternal(ObjectOutput out) throws IOException
+   {
+      out.writeInt(size);
+      for (E e : elements) out.writeObject(e);
+   }
+
+   /**
+    * 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();
+      elements = (E[]) new Object[size];
+      for (int i = 0; i < size; i++) elements[i] = (E) in.readObject();
+   }
 }




More information about the jbosscache-commits mailing list