Make new Bag implementations Serializable
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131392 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2e14c5efe8
commit
fc487af406
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -57,6 +57,9 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections.bag;
|
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.lang.reflect.Array;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -76,7 +79,7 @@ import org.apache.commons.collections.Bag;
|
||||||
* the number of occurrences of that element in the bag.
|
* the number of occurrences of that element in the bag.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @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 Chuck Burdick
|
||||||
* @author Michael A. Smith
|
* @author Michael A. Smith
|
||||||
|
@ -86,7 +89,7 @@ import org.apache.commons.collections.Bag;
|
||||||
public abstract class AbstractMapBag implements Bag {
|
public abstract class AbstractMapBag implements Bag {
|
||||||
|
|
||||||
/** The map to use to store the data */
|
/** The map to use to store the data */
|
||||||
private final Map map;
|
private transient Map map;
|
||||||
/** The current total size of the bag */
|
/** The current total size of the bag */
|
||||||
private int size;
|
private int size;
|
||||||
/** The modification count for fail fast iterators */
|
/** The modification count for fail fast iterators */
|
||||||
|
@ -94,9 +97,18 @@ public abstract class AbstractMapBag implements Bag {
|
||||||
/** The modification count for fail fast iterators */
|
/** The modification count for fail fast iterators */
|
||||||
private transient Set uniqueSet;
|
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.
|
* 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
|
* @param map the map to assign
|
||||||
*/
|
*/
|
||||||
|
@ -530,6 +542,32 @@ public abstract class AbstractMapBag implements Bag {
|
||||||
return uniqueSet;
|
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
|
* Returns true if the given object is not null, has the precise type
|
||||||
|
|
|
@ -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
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -57,6 +57,10 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections.bag;
|
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.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@ -67,22 +71,25 @@ import org.apache.commons.collections.Bag;
|
||||||
* data storage. This is the standard implementation of a bag.
|
* data storage. This is the standard implementation of a bag.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @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 Chuck Burdick
|
||||||
* @author Stephen Colebourne
|
* @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 <Code>HashBag</Code>.
|
* Constructs an empty <code>HashBag</code>.
|
||||||
*/
|
*/
|
||||||
public HashBag() {
|
public HashBag() {
|
||||||
super(new HashMap());
|
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
|
* @param coll a collection to copy into this bag
|
||||||
*/
|
*/
|
||||||
|
@ -91,4 +98,21 @@ public class HashBag extends AbstractMapBag implements Bag {
|
||||||
addAll(coll);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* The Apache Software License, Version 1.1
|
||||||
|
@ -57,6 +57,10 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections.bag;
|
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.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
|
@ -72,13 +76,16 @@ import org.apache.commons.collections.SortedBag;
|
||||||
* iterator.
|
* iterator.
|
||||||
*
|
*
|
||||||
* @since Commons Collections 3.0
|
* @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 Chuck Burdick
|
||||||
* @author Stephen Colebourne
|
* @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 <code>TreeBag</code>.
|
* Constructs an empty <code>TreeBag</code>.
|
||||||
*/
|
*/
|
||||||
|
@ -107,6 +114,7 @@ public class TreeBag extends AbstractMapBag implements SortedBag {
|
||||||
addAll(coll);
|
addAll(coll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
public Object first() {
|
public Object first() {
|
||||||
return ((SortedMap) getMap()).firstKey();
|
return ((SortedMap) getMap()).firstKey();
|
||||||
}
|
}
|
||||||
|
@ -118,5 +126,24 @@ public class TreeBag extends AbstractMapBag implements SortedBag {
|
||||||
public Comparator comparator() {
|
public Comparator comparator() {
|
||||||
return ((SortedMap) getMap()).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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* 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}
|
* Extension of {@link TestBag} for exercising the {@link HashBag}
|
||||||
* implementation.
|
* 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
|
* @author Chuck Burdick
|
||||||
*/
|
*/
|
||||||
|
@ -89,4 +89,19 @@ public class TestHashBag extends AbstractTestBag {
|
||||||
return new HashBag();
|
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");
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
* 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}
|
* Extension of {@link TestBag} for exercising the {@link TreeBag}
|
||||||
* implementation.
|
* 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
|
* @author Chuck Burdick
|
||||||
*/
|
*/
|
||||||
|
@ -112,4 +112,20 @@ public class TestTreeBag extends AbstractTestBag {
|
||||||
assertEquals("Should get last key",
|
assertEquals("Should get last key",
|
||||||
"D", ((SortedBag)bag).last());
|
"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");
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue