gov.nist.nlpir.irf.index
Class IrfHashtable

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--gov.nist.nlpir.irf.index.IrfHashtable

public class IrfHashtable
extends java.util.Dictionary
implements java.lang.Cloneable, java.io.Serializable

This class is a classic implementation of an hashtable class. It adds an important feature usual Hashtables don't provide: as storing in the table only uses hashCode() and equals(), and that these methods can be refined in each class to have a specific behavior, it is actually possible to access an object in the hashtable with different keys, provided these keys have the same hashCode and return true when they are compared with equals() . This is used in DualKeyContainer and in IdxIntern. Thus, there is a need for a method getActualKey() that returns the object actually stored as a key when you access the hashtable with a false key - a key not actually stored.

Inheritance couldn't be used to add this feature to java.util.Hashtable because the core of the Hashtable, ie the array of entries is private and not protected, so that extending Hashtable doesn't allow addition of this kind of feature.
It also contains a method that reveals the length of the base table array.

Version:
$Revision: 1.3 $
Author:
This software was produced by NIST, an agency of the U.S. government, and by statute is not subject to copyright in the United States. Recipients of this software assume all responsibilities associated with its operation, modification and maintenance.
See Also:
Serialized Form

Field Summary
private  int count
          The total number of entries in the hashtable.
private  float loadFactor
           
(package private) static long serialVersionUID
          serial version universal id - put here so Java does not insert one which may change due to revisions and make it impossible to deserialize earlier versions of serialized objects
protected  IrfHashtableEntry[] table
          The hashtable data -protected so that this class can be extended.
private  int threshold
           
 
Constructor Summary
IrfHashtable()
          Constructs a new, empty hashtable with a default capacity and load factor.
IrfHashtable(int initialCapacity)
          Constructs a new, empty hashtable with the specified initial capacity and default load factor.
IrfHashtable(int initialCapacity, float loadFactor)
          Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
 
Method Summary
 void clear()
          Clears this hashtable so that it contains no keys.
 java.lang.Object clone()
          Creates a shallow copy of this hashtable.
 boolean contains(java.lang.Object value)
          Tests if some key maps into the specified value in this hashtable.
 boolean containsKey(java.lang.Object key)
          Tests if the specified object is a key in this hashtable.
 java.util.Enumeration elements()
           
 java.lang.Object get(java.lang.Object key)
          Returns the value to which the specified key is mapped in this hashtable.
 java.lang.Object getActualKey(java.lang.Object key)
          Returns the object actually stored as a key for this possible key.
 boolean isEmpty()
          Tests if this hashtable maps no keys to values.
 java.util.Enumeration keys()
          Returns an enumeration of the keys in this hashtable.
 int length()
          Returns the size of the base array
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Maps the specified key to the specified value in this hashtable.
private  void readObject(java.io.ObjectInputStream s)
          readObject is called to restore the state of the hashtable from a stream.
protected  void rehash()
          Rehashes the contents of the hashtable into a hashtable with a larger capacity.
 java.lang.Object remove(java.lang.Object key)
          Removes the key (and its corresponding value) from this hashtable.
 int size()
          Returns the number of keys in this hashtable.
 java.lang.String toString()
          Returns a rather long string representation of this hashtable.
 java.util.Enumeration values()
          Returns an enumeration of the values in this hashtable.
private  void writeObject(java.io.ObjectOutputStream s)
          WriteObject is called to save the state of the hashtable to a stream.
 
Methods inherited from class java.lang.Object
, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

serialVersionUID

static final long serialVersionUID
serial version universal id - put here so Java does not insert one which may change due to revisions and make it impossible to deserialize earlier versions of serialized objects

table

protected transient IrfHashtableEntry[] table
The hashtable data -protected so that this class can be extended.

count

private transient int count
The total number of entries in the hashtable.

threshold

private int threshold

loadFactor

private float loadFactor
Constructor Detail

IrfHashtable

public IrfHashtable(int initialCapacity,
                    float loadFactor)
Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
Parameters:
initialCapacity - the initial capacity of the hashtable.
loadFactor - a number between 0.0 and 1.0.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero.

IrfHashtable

public IrfHashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity and default load factor.
Parameters:
initialCapacity - the initial capacity of the hashtable.

IrfHashtable

public IrfHashtable()
Constructs a new, empty hashtable with a default capacity and load factor.
Method Detail

size

public int size()
Returns the number of keys in this hashtable.
Returns:
the number of keys in this hashtable.
Overrides:
size in class java.util.Dictionary

length

public int length()
Returns the size of the base array
Returns:
the length of the base array for this hashtable.

isEmpty

public boolean isEmpty()
Tests if this hashtable maps no keys to values.
Returns:
true if this hashtable maps no keys to values; false otherwise.
Overrides:
isEmpty in class java.util.Dictionary

keys

public java.util.Enumeration keys()
Returns an enumeration of the keys in this hashtable.
Returns:
an enumeration of the keys in this hashtable.
Overrides:
keys in class java.util.Dictionary
See Also:
Enumeration, elements()

values

public java.util.Enumeration values()
Returns an enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially.
Returns:
an enumeration of the values in this hashtable.
See Also:
Enumeration, keys()

elements

public java.util.Enumeration elements()
Returns:
an enumeration of the elements in the IrfHashtable
Overrides:
elements in class java.util.Dictionary

contains

public boolean contains(java.lang.Object value)
Tests if some key maps into the specified value in this hashtable. This operation is more expensive than the containsKey method.
Parameters:
value - a value to search for.
Returns:
true if some key maps to the value argument in this hashtable; false otherwise.
Throws:
NullPointerException - if the value is null.
See Also:
containsKey(java.lang.Object)

containsKey

public boolean containsKey(java.lang.Object key)
Tests if the specified object is a key in this hashtable.
Parameters:
key - possible key.
Returns:
true if the specified object is a key in this hashtable; false otherwise.
See Also:
contains(java.lang.Object)

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which the specified key is mapped in this hashtable.
Parameters:
key - a key in the hashtable.
Returns:
the value to which the key is mapped in this hashtable; null if the key is not mapped to any value in this hashtable.
Overrides:
get in class java.util.Dictionary
See Also:
put(java.lang.Object, java.lang.Object)

rehash

protected void rehash()
Rehashes the contents of the hashtable into a hashtable with a larger capacity. This method is called automatically when the number of keys in the hashtable exceeds this hashtable's capacity and load factor.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

The value can be retrieved by calling the get method with a key that is equal to the original key. Equal here means that it has the same hash code and returns true to the method equals().

Parameters:
key - the hashtable key.
value - the value.
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.
Throws:
NullPointerException - if the key or value is null.
Overrides:
put in class java.util.Dictionary
See Also:
Object.equals(java.lang.Object), get(java.lang.Object)

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable.
Parameters:
key - the key that needs to be removed.
Returns:
the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping.
Overrides:
remove in class java.util.Dictionary

clear

public void clear()
Clears this hashtable so that it contains no keys.

clone

public java.lang.Object clone()
Creates a shallow copy of this hashtable. The keys and values themselves are not cloned. This is a relatively expensive operation.
Returns:
a clone of the hashtable.
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Returns a rather long string representation of this hashtable.
Returns:
a string representation of this hashtable.
Overrides:
toString in class java.lang.Object

writeObject

private void writeObject(java.io.ObjectOutputStream s)
                  throws java.io.IOException
WriteObject is called to save the state of the hashtable to a stream. Only the keys and values are serialized since the hash values may be different when the contents are restored. iterate over the contents and write out the keys and values.
Throws:
java.io.IOException - Various problems writing to a file

readObject

private void readObject(java.io.ObjectInputStream s)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
readObject is called to restore the state of the hashtable from a stream. Only the keys and values are serialized since the hash values may be different when the contents are restored. Read count elements and insert into the hashtable.
Throws:
java.lang.ClassNotFoundException - Deserialization can't create object
java.io.IOException - Various problems reading from a file

getActualKey

public java.lang.Object getActualKey(java.lang.Object key)
Returns the object actually stored as a key for this possible key. The returned object will thus have the same hash code as key, and they will return true if equals is called on them.
Parameters:
key - possible key.
Returns:
true if the specified object is a key in this hashtable; false otherwise.
See Also:
contains(java.lang.Object)