From 2067c96f3fc0304e2a5e3e165a339cabd569b0d7 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Wed, 17 Apr 2013 18:32:49 +0000 Subject: [PATCH] [COLLECTIONS-285] Add serialization support for TreeBidiMap, thanks to Christian Gruenberg. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1469020 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 + .../collections4/bidimap/TreeBidiMap.java | 52 ++++++++++++++---- .../TreeBidiMap.emptyCollection.version4.obj | Bin 0 -> 79 bytes .../TreeBidiMap.fullCollection.version4.obj | Bin 0 -> 360 bytes 4 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj create mode 100644 src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e69dda33e..1bfcf6299 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,9 @@ + + Added serialization support for "TreeBidiMap". + Change base package to org.apache.commons.collections4. diff --git a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java index 0c1486502..7ef4782bc 100644 --- a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java +++ b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java @@ -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.collections4.bidimap.TreeBidiMap.DataElement.VA * 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, V extends Comparable> implements OrderedBidiMap { +public class TreeBidiMap, V extends Comparable> + implements OrderedBidiMap, Serializable { static enum DataElement { KEY("key"), VALUE("value"); @@ -96,13 +99,15 @@ public class TreeBidiMap, V extends Comparable> imple } } - private Node[] rootNode; - private int nodeCount = 0; - private int modifications = 0; - private Set keySet; - private Set valuesSet; - private Set> entrySet; - private Inverse inverse = null; + private static final long serialVersionUID = 721969328361807L; + + private transient Node[] rootNode; + private transient int nodeCount = 0; + private transient int modifications = 0; + private transient Set keySet; + private transient Set valuesSet; + private transient Set> entrySet; + private transient Inverse inverse = null; //----------------------------------------------------------------------- /** @@ -1405,6 +1410,33 @@ public class TreeBidiMap, V extends Comparable> imple } } + /** + * 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 entry : entrySet()) { + stream.writeObject(entry.getKey()); + stream.writeObject(entry.getValue()); + } + } + //----------------------------------------------------------------------- /** * A view of this map. diff --git a/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj b/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj new file mode 100644 index 0000000000000000000000000000000000000000..68410c97b89893532cd6d979108b86768cfdf3c6 GIT binary patch literal 79 zcmZ4UmVvdnh`~6&C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dNtr2`xrqgOAw{XF dPC&MAVgUowgauQh1^t;B7%B?NSr~wz0sw+P8gc*t literal 0 HcmV?d00001 diff --git a/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj b/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj new file mode 100644 index 0000000000000000000000000000000000000000..5360724b1231f64792283f881531dbeaeed76cc5 GIT binary patch literal 360 zcmXX>Ia0(h5S+pX6h#7`Aaek`fSe*BBN}@L*|oApWy`zkeFX_Q58!M3fga(cd%9b5 z=;xQJd7?+1T*HyOUYbtFQNs-OUb-}(ernobH;f#cw@Ks~^$SP3`27BQzkMx;PVrb0 z(Osg79S;*G!f`+i*GWLsYfcb#Oo4nrA{It#{(qhiX a0BuYfBl>{0