Make DualHashBidiMap serialiizable

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131267 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-10-09 20:21:32 +00:00
parent 2f1df4d36c
commit c676de25d3
3 changed files with 83 additions and 23 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.1 2003/10/06 23:47:17 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.2 2003/10/09 20:21:32 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -73,7 +73,7 @@ import org.apache.commons.collections.decorators.AbstractMapEntryDecorator;
* <code>createMap</code> method.
*
* @since Commons Collections 3.0
* @version $Id: AbstractDualBidiMap.java,v 1.1 2003/10/06 23:47:17 scolebourne Exp $
* @version $Id: AbstractDualBidiMap.java,v 1.2 2003/10/09 20:21:32 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -99,22 +99,19 @@ public abstract class AbstractDualBidiMap implements BidiMap {
protected transient Set entrySet = null;
/**
* Creates an empty map.
* Creates an empty map, initialised by <code>createMap</code>.
* <p>
* The maps passed in are not validated, so subclasses need to ensure
* that they are non-null, empty and compatible.
*
* @param normalMap the normal direction map
* @param reverseMap the reverse direction map
* The map array must be populated by the subclass.
*/
protected AbstractDualBidiMap(Map normalMap, Map reverseMap) {
protected AbstractDualBidiMap() {
super();
maps[0] = normalMap;
maps[1] = reverseMap;
maps[0] = createMap();
maps[1] = createMap();
}
/**
* Constructs a map that decorates the specified maps.
* Constructs a map that decorates the specified maps,
* used by the subclass <code>createBidiMap</code> implementation.
*
* @param normalMap the normal direction map
* @param reverseMap the reverse direction map
@ -127,6 +124,15 @@ public abstract class AbstractDualBidiMap implements BidiMap {
this.inverseBidiMap = inverseBidiMap;
}
/**
* Creates a new instance of the map used by the subclass to store data.
* <p>
* Do not change any instance variables from this method.
*
* @return the map to be used for internal storage
*/
protected abstract Map createMap();
/**
* Creates a new instance of the subclass.
*

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/DualHashBidiMap.java,v 1.1 2003/10/06 23:47:17 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/DualHashBidiMap.java,v 1.2 2003/10/09 20:21:32 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,6 +57,10 @@
*/
package org.apache.commons.collections;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@ -64,18 +68,21 @@ import java.util.Map;
* Implementation of <code>BidiMap</code> that uses two <code>HashMap</code> instances.
*
* @since Commons Collections 3.0
* @version $Id: DualHashBidiMap.java,v 1.1 2003/10/06 23:47:17 scolebourne Exp $
* @version $Id: DualHashBidiMap.java,v 1.2 2003/10/09 20:21:32 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
*/
public class DualHashBidiMap extends AbstractDualBidiMap {
public class DualHashBidiMap extends AbstractDualBidiMap implements Serializable {
/** Ensure serialization compatability */
private static final long serialVersionUID = 721969328361808L;
/**
* Creates an empty <code>HashBidiMap</code>
*/
public DualHashBidiMap() {
super(new HashMap(), new HashMap());
super();
}
/**
@ -85,7 +92,7 @@ public class DualHashBidiMap extends AbstractDualBidiMap {
* @param map the map whose mappings are to be placed in this map
*/
public DualHashBidiMap(Map map) {
super(new HashMap(), new HashMap());
super();
putAll(map);
}
@ -100,6 +107,15 @@ public class DualHashBidiMap extends AbstractDualBidiMap {
super(normalMap, reverseMap, inverseBidiMap);
}
/**
* Creates a new instance of the map used by the subclass to store data.
*
* @return the map to be used for internal storage
*/
protected Map createMap() {
return new HashMap();
}
/**
* Creates a new instance of this object.
*
@ -112,5 +128,17 @@ public class DualHashBidiMap extends AbstractDualBidiMap {
return new DualHashBidiMap(normalMap, reverseMap, inverseMap);
}
// Serialization
//-----------------------------------------------------------------------
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(maps[0]);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
Map map = (Map) in.readObject();
putAll(map);
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestBidiMap.java,v 1.6 2003/10/07 22:20:57 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestBidiMap.java,v 1.7 2003/10/09 20:21:32 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -63,7 +63,7 @@ import java.util.Map;
/**
* JUnit tests.
*
* @version $Revision: 1.6 $ $Date: 2003/10/07 22:20:57 $
* @version $Revision: 1.7 $ $Date: 2003/10/09 20:21:32 $
*
* @author Matthew Hawthorne
*/
@ -132,6 +132,13 @@ public abstract class TestBidiMap extends AbstractTestMap {
return false;
}
/**
* Override as DualHashBidiMap didn't exist until version 3.
*/
protected String getCompatibilityVersion() {
return "3";
}
// BidiPut
//-----------------------------------------------------------------------
public void testBidiPut() {
@ -338,10 +345,29 @@ public abstract class TestBidiMap extends AbstractTestMap {
protected BidiMap makeEmptyBidiMap() {
return main.makeEmptyBidiMap().inverseBidiMap();
}
protected BidiMap makeFullBidiMap() {
return main.makeFullBidiMap().inverseBidiMap();
}
protected String getCompatibilityVersion() {
return main.getCompatibilityVersion();
}
protected boolean isAllowNullKey() {
return main.isAllowNullKey();
}
protected boolean isAllowNullValue() {
return main.isAllowNullValue();
}
protected boolean isPutAddSupported() {
return main.isPutAddSupported();
}
protected boolean isPutChangeSupported() {
return main.isPutChangeSupported();
}
protected boolean isRemoveSupported() {
return main.isRemoveSupported();
}
}
}