gov.nist.nlpir.irf.index.braf
Class HashBlock

java.lang.Object
  |
  +--gov.nist.nlpir.irf.index.braf.HashBlock

public class HashBlock
extends java.lang.Object
implements java.lang.Cloneable

This class is a classic implementation of a hash table, except that 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 PersistentDualKeyContainer and in IdxIntern. Thus, there is a need for a method getActualKey that returns the object actually stored as the key when you access the hashblock with a false key -a key not actually stored.
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.

Field Summary
private  int count
          The total number of entries in the hashblock.
private  boolean dirty
          Dirty bit.
private  float loadFactor
          The load factor for the hashblock.
(package private) static java.lang.Class proxyFeatureListClass
          This class object will be used for materialization of lists stored in the block.
private  int rehashThreshold
          Rehashes the hashblock when count exceeds this threshold.
protected  HashBlockEntry[] table
          The hash table data.
 
Constructor Summary
  HashBlock()
          Constructs a new, empty hashtable with a default capacity and load factor.
private HashBlock(HashBlockEntry[] table, int count, int rehashThreshold, float loadFactor)
          private constructor only used by the persistence mechanism, when coming back from disk.
  HashBlock(int initialCapacity)
          Constructs a new, empty hashtable with the specified initial capacity and default load factor.
  HashBlock(int initialCapacity, float loadFactor)
          Constructs a new, empty hashblock with the specified initial capacity and the specified load factor.
 
Method Summary
 void clear()
          Clears this hashblock so that it contains no keys.
 java.lang.Object clone()
          Creates a clone of this hashblock.
 boolean contains(java.lang.Object value)
          Tests if some key maps into the specified value in this HashBlock.
 boolean containsKey(java.lang.Object key)
          Tests if the specified object is a key in this hashtable.
 java.util.Enumeration elements()
          Returns an enumeration of the values in this hashtable.
 void finalize()
          As this HashBlock contains Proxies and has counted the references it has to them, it needs to remove those references before being finalized, and this is done in this method.
 java.lang.Object get(java.lang.Object key, int hash)
          Returns the value to which the specified key is mapped in this hashtable.
 java.lang.Object getActualKey(java.lang.Object key, int hash)
          Returns the object actually stored as a key for this possible key.
 boolean isDirty()
           
 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()
           
 java.lang.Object put(java.lang.Object key, int hash, java.lang.Object value)
          Maps the specified key to the specified value in this hashtable.
static HashBlock readFrom(BufferedRandomAccessFile in)
          Reads a HashBlock back from the file it was previously written with writeTo().
protected  void rehash()
          Rehashes the contents of the HashBlock into a HashBlock with a larger capacity.
 java.lang.Object remove(java.lang.Object key)
          Removes the key (and its corresponding value) from this hashtable.
 int size()
           
 java.lang.String toString()
          Returns a string representation of the hashblock.
 void writeTo(BufferedRandomAccessFile out)
          Writes this block to the file given in parameter.
 
Methods inherited from class java.lang.Object
, equals, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

table

protected transient HashBlockEntry[] table
The hash table data. Is protected so that this class can be extended.

count

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

rehashThreshold

private int rehashThreshold
Rehashes the hashblock when count exceeds this threshold.

loadFactor

private float loadFactor
The load factor for the hashblock.

proxyFeatureListClass

static java.lang.Class proxyFeatureListClass
This class object will be used for materialization of lists stored in the block.

dirty

private boolean dirty
Dirty bit. Only reflects the fact a ProxyFeatureList has been added. To see whether a key has changed or not, going through every key is currently the only possibility.
Constructor Detail

HashBlock

public HashBlock(int initialCapacity,
                 float loadFactor)
Constructs a new, empty hashblock 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.

HashBlock

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

HashBlock

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

HashBlock

private HashBlock(HashBlockEntry[] table,
                  int count,
                  int rehashThreshold,
                  float loadFactor)
private constructor only used by the persistence mechanism, when coming back from disk.
Method Detail

size

public final int size()
Returns:
the number of keys in this hashtable.

length

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

isEmpty

public final boolean isEmpty()
Tests if this hashtable maps no keys to values.
Returns:
true if this hashtable maps no keys to values;
false otherwise.

keys

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

elements

public final java.util.Enumeration elements()
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()

contains

public boolean contains(java.lang.Object value)
Tests if some key maps into the specified value in this HashBlock. 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,
                            int hash)
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.
See Also:
put(java.lang.Object, int, java.lang.Object)

rehash

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

put

public java.lang.Object put(java.lang.Object key,
                            int hash,
                            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 hashblock key. Must either be an IoAddrIntern or a DeIntern.
hash - hashCode of the key. As it has already been computed, it saves a few CPU cycles.
value - the value. During put, it should be ProxyFeatureList even if its Oid is actually enough.
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.
See Also:
Object.equals(java.lang.Object), get(java.lang.Object, int)

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.

clear

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

clone

public java.lang.Object clone()
Creates a clone of this hashblock. The keys and values themselves are not cloned.
Returns:
a clone of the hashtable.
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Returns a string representation of the hashblock.
Returns:
a string representation of the hashblock.
Overrides:
toString in class java.lang.Object

getActualKey

public java.lang.Object getActualKey(java.lang.Object key,
                                     int hash)
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)

isDirty

public boolean isDirty()

writeTo

public void writeTo(BufferedRandomAccessFile out)
             throws java.io.IOException
Writes this block to the file given in parameter.

readFrom

public static HashBlock readFrom(BufferedRandomAccessFile in)
                          throws java.io.IOException
Reads a HashBlock back from the file it was previously written with writeTo().

finalize

public void finalize()
As this HashBlock contains Proxies and has counted the references it has to them, it needs to remove those references before being finalized, and this is done in this method.
Overrides:
finalize in class java.lang.Object