You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/04/17 20:32:49 UTC

svn commit: r1469020 - in /commons/proper/collections/trunk/src: changes/ main/java/org/apache/commons/collections4/bidimap/ test/resources/data/test/

Author: tn
Date: Wed Apr 17 18:32:49 2013
New Revision: 1469020

URL: http://svn.apache.org/r1469020
Log:
[COLLECTIONS-285] Add serialization support for TreeBidiMap, thanks to Christian Gruenberg.

Added:
    commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj   (with props)
    commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj   (with props)
Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1469020&r1=1469019&r2=1469020&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Wed Apr 17 18:32:49 2013
@@ -22,6 +22,9 @@
   <body>
 
   <release version="4.0" date="TBA" description="Next release">
+    <action issue="COLLECTIONS-285" dev="tn" type="add" due-to="Christian Gruenberg">
+      Added serialization support for "TreeBidiMap".
+    </action>
     <action issue="COLLECTIONS-452" dev="tn" type="update">
       Change base package to org.apache.commons.collections4.
     </action>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java?rev=1469020&r1=1469019&r2=1469020&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java Wed Apr 17 18:32:49 2013
@@ -16,6 +16,10 @@
  */
 package org.apache.commons.collections4.bidimap;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.util.AbstractSet;
 import java.util.Collection;
 import java.util.ConcurrentModificationException;
@@ -69,12 +73,11 @@ import static org.apache.commons.collect
  * not allow setValue() and will throw an
  * UnsupportedOperationException on attempts to call that method.
  *
- * TODO: serialization does not work anymore
- *
  * @since 3.0 (previously DoubleOrderedMap v2.0)
  * @version $Id$
  */
-public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> implements OrderedBidiMap<K, V> {
+public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>>
+    implements OrderedBidiMap<K, V>, Serializable {
 
     static enum DataElement {
         KEY("key"), VALUE("value");
@@ -96,13 +99,15 @@ public class TreeBidiMap<K extends Compa
         }
     }
 
-    private Node<K, V>[] rootNode;
-    private int nodeCount = 0;
-    private int modifications = 0;
-    private Set<K> keySet;
-    private Set<V> valuesSet;
-    private Set<Map.Entry<K, V>> entrySet;
-    private Inverse inverse = null;
+    private static final long serialVersionUID = 721969328361807L;
+
+    private transient Node<K, V>[] rootNode;
+    private transient int nodeCount = 0;
+    private transient int modifications = 0;
+    private transient Set<K> keySet;
+    private transient Set<V> valuesSet;
+    private transient Set<Map.Entry<K, V>> entrySet;
+    private transient Inverse inverse = null;
 
     //-----------------------------------------------------------------------
     /**
@@ -1405,6 +1410,33 @@ public class TreeBidiMap<K extends Compa
         }
     }
 
+    /**
+     * Reads the content of the stream.
+     */
+    @SuppressWarnings("unchecked") // This will fail at runtime if the stream is incorrect
+    private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException{
+        stream.defaultReadObject();
+        rootNode = new Node[2];
+        int size = stream.readInt();
+        for(int i = 0; i < size; i++){
+            K k =(K) stream.readObject();
+            V v =(V) stream.readObject();
+            put(k, v);
+        }
+    }
+
+    /**
+     * Writes the content to the stream for serialization.
+     */
+    private void writeObject(final ObjectOutputStream stream) throws IOException{
+        stream.defaultWriteObject();
+        stream.writeInt(this.size());
+        for (final Entry<K, V> entry : entrySet()) {
+            stream.writeObject(entry.getKey());
+            stream.writeObject(entry.getValue());
+        }
+    }
+    
     //-----------------------------------------------------------------------
     /**
      * A view of this map.

Added: commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj?rev=1469020&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj?rev=1469020&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/collections/trunk/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream