You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/12/03 01:49:38 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/bag TestTreeBag.java TestHashBag.java

scolebourne    2003/12/02 16:49:38

  Modified:    collections/src/java/org/apache/commons/collections/bag
                        TreeBag.java AbstractMapBag.java HashBag.java
               collections/src/test/org/apache/commons/collections/bag
                        TestTreeBag.java TestHashBag.java
  Added:       collections/data/test TreeBag.fullCollection.version3.obj
                        HashBag.emptyCollection.version3.obj
                        HashBag.fullCollection.version3.obj
                        TreeBag.emptyCollection.version3.obj
  Log:
  Make new Bag implementations Serializable
  
  Revision  Changes    Path
  1.2       +30 -3     jakarta-commons/collections/src/java/org/apache/commons/collections/bag/TreeBag.java
  
  Index: TreeBag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/bag/TreeBag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TreeBag.java	2 Dec 2003 23:36:12 -0000	1.1
  +++ TreeBag.java	3 Dec 2003 00:49:38 -0000	1.2
  @@ -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;
  @@ -77,8 +81,11 @@
    * @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 <code>TreeBag</code>.
        */
  @@ -107,6 +114,7 @@
           addAll(coll);
       }
   
  +    //-----------------------------------------------------------------------
       public Object first() {
           return ((SortedMap) getMap()).firstKey();
       }
  @@ -117,6 +125,25 @@
   
       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);
       }
       
   }
  
  
  
  1.2       +42 -4     jakarta-commons/collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java
  
  Index: AbstractMapBag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/bag/AbstractMapBag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractMapBag.java	2 Dec 2003 23:36:12 -0000	1.1
  +++ AbstractMapBag.java	3 Dec 2003 00:49:38 -0000	1.2
  @@ -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;
  @@ -86,7 +89,7 @@
   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 */
  @@ -95,8 +98,17 @@
       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 @@
           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 
  
  
  
  1.2       +29 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/bag/HashBag.java
  
  Index: HashBag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/bag/HashBag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HashBag.java	2 Dec 2003 23:36:12 -0000	1.1
  +++ HashBag.java	3 Dec 2003 00:49:38 -0000	1.2
  @@ -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;
   
  @@ -72,17 +76,20 @@
    * @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 <Code>HashBag</Code>.
  +     * Constructs an empty <code>HashBag</code>.
        */
       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 @@
           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.1                  jakarta-commons/collections/data/test/TreeBag.fullCollection.version3.obj
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/collections/data/test/HashBag.emptyCollection.version3.obj
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/collections/data/test/HashBag.fullCollection.version3.obj
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/collections/data/test/TreeBag.emptyCollection.version3.obj
  
  	<<Binary file>>
  
  
  1.2       +18 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/bag/TestTreeBag.java
  
  Index: TestTreeBag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/bag/TestTreeBag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestTreeBag.java	2 Dec 2003 23:36:12 -0000	1.1
  +++ TestTreeBag.java	3 Dec 2003 00:49:38 -0000	1.2
  @@ -112,4 +112,20 @@
         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");
  +//   }
   }
  
  
  
  1.2       +17 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/bag/TestHashBag.java
  
  Index: TestHashBag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/bag/TestHashBag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestHashBag.java	2 Dec 2003 23:36:12 -0000	1.1
  +++ TestHashBag.java	3 Dec 2003 00:49:38 -0000	1.2
  @@ -89,4 +89,19 @@
           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");
  +//    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org