Author: manik.surtani(a)jboss.com
Date: 2008-07-30 05:12:38 -0400 (Wed, 30 Jul 2008)
New Revision: 6428
Modified:
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableListCopy.java
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java
Log:
Made immutable collections externalizable
Modified: core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableListCopy.java
===================================================================
---
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableListCopy.java 2008-07-30
09:10:13 UTC (rev 6427)
+++
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableListCopy.java 2008-07-30
09:12:38 UTC (rev 6428)
@@ -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;
@@ -22,12 +26,14 @@
* <p/>
*
* @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
+ * @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.
@@ -156,7 +162,7 @@
public final E get(int index)
{
- assertIndexInRange(index);
+ if (index >= size || index < 0) throw new
IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
return elements[index];
}
@@ -218,11 +224,35 @@
return new ImmutableSubList<E>(fromIndex, toIndex);
}
- private void assertIndexInRange(int index)
+ /**
+ * Format:
+ * - entry array size (int)
+ * - elements (Object)
+ *
+ * @param out stream to write to
+ * @throws IOException
+ */
+ public void writeExternal(ObjectOutput out) throws IOException
{
- if (index >= size || index < 0) throw new
IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
+ 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;
@@ -305,7 +335,7 @@
ImmutableSubList(int fromIndex, int toIndex)
{
if (fromIndex < 0 || toIndex > ImmutableListCopy.this.size || fromIndex
> toIndex)
- throw new IllegalArgumentException("fromIndex(" + fromIndex +
"), toIndex(" + toIndex + "), size (" + ImmutableListCopy.this.size +
")");
+ throw new IllegalArgumentException("fromIndex(" + fromIndex +
"), toIndex(" + toIndex + "), size (" + ImmutableListCopy.this.size +
"), List=" + ImmutableListCopy.this.toString());
offset = fromIndex;
size = toIndex - fromIndex;
}
Modified: core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java
===================================================================
---
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java 2008-07-30
09:10:13 UTC (rev 6427)
+++
core/branches/2.2.X/src/main/java/org/jboss/cache/util/ImmutableSetCopy.java 2008-07-30
09:12:38 UTC (rev 6428)
@@ -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;
@@ -20,12 +24,14 @@
*
* @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
* @see org.jboss.cache.util.ImmutableListCopy
+ * @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)
@@ -105,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();
+ }
}
Show replies by date