Made IntIntMap serializable, added an isEmpty method.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2120 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
andrzej
2007-07-03 03:18:55 +00:00
parent 4d8d0f3788
commit 977126b6df
@@ -20,6 +20,11 @@
package com.samskivert.util; package com.samskivert.util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
@@ -36,6 +41,7 @@ import java.util.Set;
// TODO: make this a proper map, perhaps even an extension of HashIntMap // TODO: make this a proper map, perhaps even an extension of HashIntMap
// or at least share a common abstract ancestor. // or at least share a common abstract ancestor.
public class IntIntMap public class IntIntMap
implements Serializable
{ {
public interface IntIntEntry extends IntMap.IntEntry<Integer> public interface IntIntEntry extends IntMap.IntEntry<Integer>
{ {
@@ -68,6 +74,11 @@ public class IntIntMap
this(DEFAULT_BUCKETS, DEFAULT_LOAD_FACTOR); this(DEFAULT_BUCKETS, DEFAULT_LOAD_FACTOR);
} }
public boolean isEmpty ()
{
return _size == 0;
}
public int size () public int size ()
{ {
return _size; return _size;
@@ -317,6 +328,46 @@ public class IntIntMap
}; };
} }
/**
* 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);
s.writeFloat(_loadFactor);
// write out size (number of mappings)
s.writeInt(_size);
// write out keys and values
for (IntIntEntry entry : entrySet()) {
s.writeInt(entry.getIntKey());
s.writeInt(entry.getIntValue());
}
}
/**
* Reconstitute the <tt>IntIntMap</tt> 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()];
_loadFactor = s.readFloat();
// read in size (number of mappings)
int size = s.readInt();
// read the keys and values
for (int i=0; i<size; i++) {
put(s.readInt(), s.readInt());
}
}
class Record implements IntIntEntry class Record implements IntIntEntry
{ {
public Record next; public Record next;
@@ -504,4 +555,7 @@ public class IntIntMap
protected float _loadFactor; protected float _loadFactor;
protected int _modCount = 0; protected int _modCount = 0;
/** Change this if the fields or inheritance hierarchy ever changes. */
private static final long serialVersionUID = 1;
} }