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 000000000..68410c97b Binary files /dev/null and b/src/test/resources/data/test/TreeBidiMap.emptyCollection.version4.obj differ 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 000000000..5360724b1 Binary files /dev/null and b/src/test/resources/data/test/TreeBidiMap.fullCollection.version4.obj differ