diff --git a/data/test/HashBag.emptyCollection.version3.obj b/data/test/HashBag.emptyCollection.version3.obj new file mode 100644 index 000000000..e74098603 Binary files /dev/null and b/data/test/HashBag.emptyCollection.version3.obj differ diff --git a/data/test/HashBag.fullCollection.version3.obj b/data/test/HashBag.fullCollection.version3.obj new file mode 100644 index 000000000..9ec908180 Binary files /dev/null and b/data/test/HashBag.fullCollection.version3.obj differ diff --git a/data/test/TreeBag.emptyCollection.version3.obj b/data/test/TreeBag.emptyCollection.version3.obj new file mode 100644 index 000000000..feb0d433f Binary files /dev/null and b/data/test/TreeBag.emptyCollection.version3.obj differ diff --git a/data/test/TreeBag.fullCollection.version3.obj b/data/test/TreeBag.fullCollection.version3.obj new file mode 100644 index 000000000..987570e11 Binary files /dev/null and b/data/test/TreeBag.fullCollection.version3.obj differ diff --git a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java index f7b9542d8..b783a39e2 100644 --- a/src/java/org/apache/commons/collections/bag/AbstractMapBag.java +++ b/src/java/org/apache/commons/collections/bag/AbstractMapBag.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java,v 1.1 2003/12/02 23:36:12 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java,v 1.2 2003/12/03 00:49:38 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -57,6 +57,9 @@ */ package org.apache.commons.collections.bag; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.lang.reflect.Array; import java.util.Collection; import java.util.Collections; @@ -76,7 +79,7 @@ import org.apache.commons.collections.Bag; * the number of occurrences of that element in the bag. * * @since Commons Collections 3.0 - * @version $Revision: 1.1 $ $Date: 2003/12/02 23:36:12 $ + * @version $Revision: 1.2 $ $Date: 2003/12/03 00:49:38 $ * * @author Chuck Burdick * @author Michael A. Smith @@ -86,7 +89,7 @@ import org.apache.commons.collections.Bag; public abstract class AbstractMapBag implements Bag { /** The map to use to store the data */ - private final Map map; + private transient Map map; /** The current total size of the bag */ private int size; /** The modification count for fail fast iterators */ @@ -94,9 +97,18 @@ public abstract class AbstractMapBag implements Bag { /** The modification count for fail fast iterators */ private transient Set uniqueSet; + /** + * Constructor needed for subclass serialisation. + * + * @param map the map to assign + */ + protected AbstractMapBag() { + super(); + } + /** * Constructor that assigns the specified Map as the backing store. - * The map must be empty. + * The map must be empty and non-null. * * @param map the map to assign */ @@ -530,6 +542,32 @@ public abstract class AbstractMapBag implements Bag { return uniqueSet; } + //----------------------------------------------------------------------- + /** + * Write the map out using a custom routine. + */ + protected void doWriteObject(ObjectOutputStream out) throws IOException { + out.writeInt(map.size()); + for (Iterator it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry) it.next(); + out.writeObject(entry.getKey()); + out.writeInt(((MutableInteger) entry.getValue()).value); + } + } + + /** + * Read the map in using a custom routine. + */ + protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException { + this.map = map; + int entrySize = in.readInt(); + for (int i = 0; i < entrySize; i++) { + Object key = in.readObject(); + int value = in.readInt(); + map.put(key, new MutableInteger(value)); + } + } + //----------------------------------------------------------------------- /** * Returns true if the given object is not null, has the precise type diff --git a/src/java/org/apache/commons/collections/bag/HashBag.java b/src/java/org/apache/commons/collections/bag/HashBag.java index 574a1393b..087df0dcb 100644 --- a/src/java/org/apache/commons/collections/bag/HashBag.java +++ b/src/java/org/apache/commons/collections/bag/HashBag.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/HashBag.java,v 1.1 2003/12/02 23:36:12 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/HashBag.java,v 1.2 2003/12/03 00:49:38 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -57,6 +57,10 @@ */ package org.apache.commons.collections.bag; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.Collection; import java.util.HashMap; @@ -67,22 +71,25 @@ import org.apache.commons.collections.Bag; * data storage. This is the standard implementation of a bag. * * @since Commons Collections 3.0 - * @version $Revision: 1.1 $ $Date: 2003/12/02 23:36:12 $ + * @version $Revision: 1.2 $ $Date: 2003/12/03 00:49:38 $ * * @author Chuck Burdick * @author Stephen Colebourne */ -public class HashBag extends AbstractMapBag implements Bag { +public class HashBag extends AbstractMapBag implements Bag, Serializable { + /** Serial version lock */ + static final long serialVersionUID = -6561115435802554013L; + /** - * Constructs an empty HashBag. + * Constructs an empty HashBag. */ public HashBag() { super(new HashMap()); } /** - * Constructs a {@link Bag} containing all the members of the given collection. + * Constructs a bag containing all the members of the given collection. * * @param coll a collection to copy into this bag */ @@ -91,4 +98,21 @@ public class HashBag extends AbstractMapBag implements Bag { addAll(coll); } + //----------------------------------------------------------------------- + /** + * Write the bag out using a custom routine. + */ + private void writeObject(ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + super.doWriteObject(out); + } + + /** + * Read the bag in using a custom routine. + */ + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + super.doReadObject(new HashMap(), in); + } + } diff --git a/src/java/org/apache/commons/collections/bag/TreeBag.java b/src/java/org/apache/commons/collections/bag/TreeBag.java index a770cb75d..07f8334ba 100644 --- a/src/java/org/apache/commons/collections/bag/TreeBag.java +++ b/src/java/org/apache/commons/collections/bag/TreeBag.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/TreeBag.java,v 1.1 2003/12/02 23:36:12 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bag/TreeBag.java,v 1.2 2003/12/03 00:49:38 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -57,6 +57,10 @@ */ package org.apache.commons.collections.bag; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.Collection; import java.util.Comparator; import java.util.SortedMap; @@ -72,13 +76,16 @@ import org.apache.commons.collections.SortedBag; * iterator. * * @since Commons Collections 3.0 - * @version $Revision: 1.1 $ $Date: 2003/12/02 23:36:12 $ + * @version $Revision: 1.2 $ $Date: 2003/12/03 00:49:38 $ * * @author Chuck Burdick * @author Stephen Colebourne */ -public class TreeBag extends AbstractMapBag implements SortedBag { +public class TreeBag extends AbstractMapBag implements SortedBag, Serializable { + /** Serial version lock */ + static final long serialVersionUID = -7740146511091606676L; + /** * Constructs an empty TreeBag. */ @@ -107,6 +114,7 @@ public class TreeBag extends AbstractMapBag implements SortedBag { addAll(coll); } + //----------------------------------------------------------------------- public Object first() { return ((SortedMap) getMap()).firstKey(); } @@ -118,5 +126,24 @@ public class TreeBag extends AbstractMapBag implements SortedBag { public Comparator comparator() { return ((SortedMap) getMap()).comparator(); } + + //----------------------------------------------------------------------- + /** + * Write the bag out using a custom routine. + */ + private void writeObject(ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + out.writeObject(comparator()); + super.doWriteObject(out); + } + + /** + * Read the bag in using a custom routine. + */ + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + Comparator comp = (Comparator) in.readObject(); + super.doReadObject(new TreeMap(comp), in); + } } diff --git a/src/test/org/apache/commons/collections/bag/TestHashBag.java b/src/test/org/apache/commons/collections/bag/TestHashBag.java index ba25a3617..9b2074f88 100644 --- a/src/test/org/apache/commons/collections/bag/TestHashBag.java +++ b/src/test/org/apache/commons/collections/bag/TestHashBag.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/TestHashBag.java,v 1.1 2003/12/02 23:36:12 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/TestHashBag.java,v 1.2 2003/12/03 00:49:38 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -66,7 +66,7 @@ import org.apache.commons.collections.Bag; * Extension of {@link TestBag} for exercising the {@link HashBag} * implementation. * - * @version $Revision: 1.1 $ $Date: 2003/12/02 23:36:12 $ + * @version $Revision: 1.2 $ $Date: 2003/12/03 00:49:38 $ * * @author Chuck Burdick */ @@ -89,4 +89,19 @@ public class TestHashBag extends AbstractTestBag { return new HashBag(); } + public String getCompatibilityVersion() { + return "3"; + } + +// public void testCreate() throws Exception { +// Bag bag = makeBag(); +// writeExternalFormToDisk((Serializable) bag, "D:/dev/collections/data/test/HashBag.emptyCollection.version3.obj"); +// bag = makeBag(); +// bag.add("A"); +// bag.add("A"); +// bag.add("B"); +// bag.add("B"); +// bag.add("C"); +// writeExternalFormToDisk((Serializable) bag, "D:/dev/collections/data/test/HashBag.fullCollection.version3.obj"); +// } } diff --git a/src/test/org/apache/commons/collections/bag/TestTreeBag.java b/src/test/org/apache/commons/collections/bag/TestTreeBag.java index c0d1419a9..02c111a4e 100644 --- a/src/test/org/apache/commons/collections/bag/TestTreeBag.java +++ b/src/test/org/apache/commons/collections/bag/TestTreeBag.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/TestTreeBag.java,v 1.1 2003/12/02 23:36:12 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bag/TestTreeBag.java,v 1.2 2003/12/03 00:49:38 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -67,7 +67,7 @@ import org.apache.commons.collections.SortedBag; * Extension of {@link TestBag} for exercising the {@link TreeBag} * implementation. * - * @version $Revision: 1.1 $ $Date: 2003/12/02 23:36:12 $ + * @version $Revision: 1.2 $ $Date: 2003/12/03 00:49:38 $ * * @author Chuck Burdick */ @@ -112,4 +112,20 @@ public class TestTreeBag extends AbstractTestBag { assertEquals("Should get last key", "D", ((SortedBag)bag).last()); } + + public String getCompatibilityVersion() { + return "3"; + } + +// public void testCreate() throws Exception { +// Bag bag = makeBag(); +// writeExternalFormToDisk((Serializable) bag, "D:/dev/collections/data/test/TreeBag.emptyCollection.version3.obj"); +// bag = makeBag(); +// bag.add("A"); +// bag.add("A"); +// bag.add("B"); +// bag.add("B"); +// bag.add("C"); +// writeExternalFormToDisk((Serializable) bag, "D:/dev/collections/data/test/TreeBag.fullCollection.version3.obj"); +// } }