From 339828bc6195aa54d3eb1c135674e021fb0d2703 Mon Sep 17 00:00:00 2001 From: mdb Date: Mon, 26 Nov 2001 19:23:48 +0000 Subject: [PATCH] Made HashIntMap serializable and moved test code into a test driver. git-svn-id: https://samskivert.googlecode.com/svn/trunk@471 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/HashIntMap.java | 89 +++++++++++-------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java index deb71114..f7e6385f 100644 --- a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java @@ -1,5 +1,5 @@ // -// $Id: HashIntMap.java,v 1.4 2001/09/15 17:22:11 mdb Exp $ +// $Id: HashIntMap.java,v 1.5 2001/11/26 19:23:48 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -20,7 +20,16 @@ package com.samskivert.util; -import java.util.*; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Set; /** * An int map is like a regular map, but with integers as keys. We avoid @@ -284,6 +293,47 @@ public class HashIntMap return values().iterator(); } + /** + * Save the state of this instance to a stream (i.e., serialize it). + */ + private void writeObject (ObjectOutputStream s) + throws IOException + { + // write out number of buckets + s.writeInt(_buckets.length); + + // write out size (number of mappings) + s.writeInt(_size); + + // write out keys and values + for (Iterator i = entrySet().iterator(); i.hasNext(); ) { + Entry e = (Entry)i.next(); + s.writeInt(((Integer)e.getKey()).intValue()); + s.writeObject(e.getValue()); + } + } + + /** + * Reconstitute the HashIntMap instance from a stream (i.e., + * deserialize it). + */ + private void readObject (ObjectInputStream s) + throws IOException, ClassNotFoundException + { + // read in number of buckets and allocate the bucket array + _buckets = new Record[s.readInt()]; + + // read in size (number of mappings) + int size = s.readInt(); + + // read the keys and values + for (int i=0; i