package edu.vt.marian.search; import java.util.*; import java.io.*; import edu.vt.marian.common.*; /** * Provides hash table functionality for FullIDs. This class is * really just a wrapper around the java.util.Hashtable class, with * one of the ubiquitous Debug objects embedded in it. *
* This method implicitly relies upon the hashCode method to be * defined in the object that is going to be used as a key object. * * @author Paul Mather * @version 0.9 * @see edu.vt.marian.common.FullID * @see edu.vt.marian.common.FullID#hashCode * @see java.util.Hashtable * @see edu.vt.marian.common.Debug */ public class FullIDTable extends java.util.Hashtable { /** The ubiquitous debugging object */ protected Debug debug; /** Return value to indicate success */ public final static int OK = 0; /** Return value indicating bad parameters were supplied to method */ public final static int BAD_PARAMS = -2; /** Return value to indicate passed stream object was null */ public final static int NULL_STREAM = 7; /** * Constant denoting the default initial StringBuffer size * for toString method if no estimate is provided. */ private static final int DEFAULT_TOSTRING_ESTIMATE = 16; /** * Create an empty hash table for FullIDs */ public FullIDTable(Debug debug) { super(); this.debug = debug; } /** * Create an empty hash table for FullIDs specifying initial * capacity and load factor. * @param initialCapacity initial size of hash table * @param loadFactor occupancy threshold before growth needed * @param debug debugging object */ public FullIDTable(int initialCapacity, float loadFactor, Debug debug) { super(initialCapacity, loadFactor); this.debug = debug; } /** * Create an empty hash table for FullIDs specifying initial * capacity, but with default load factor. * @param initialCapacity initial size of hash table * @param debug debugging object */ public FullIDTable(int initialCapacity, Debug debug) { super(initialCapacity); this.debug = debug; } /** * Test whether a given FullID is in the hash table. * @param fullID FullID object to test for existence in hash table * @return True if fullID is a key for something in the hash table; * false otherwise. */ public boolean isInTable( FullID fullID ) { return (this.get( fullID ) != null); } /** * Convert hash table to a printable string representation. * @return String containing object mappings * @see FullID#toString */ public String toString( int estimatedSize ) { Enumeration fullIDKeys; // All FullID keys Object key; // FullID or String key object from hash table StringBuffer stringFullIDHashTable ; boolean first = true; // Flag to denote this is the first mapping printed // Check that estimatedSize is reasonable if (estimatedSize < 0) { estimatedSize = DEFAULT_TOSTRING_ESTIMATE; debug.dumpTrace("FullIDStringTable.toStream: negative estimate passed"); } // Make a StringBuffer, initially of estimatedSize stringFullIDHashTable = new StringBuffer( estimatedSize ); // Initialise it with the opening brace stringFullIDHashTable.append('{'); for (fullIDKeys = this.keys(); fullIDKeys.hasMoreElements();) { // Get the next key key = fullIDKeys.nextElement(); if (key instanceof FullID) { // We are only interested in FullID keys because // we only want to string-ify FullID->Something // mappings. // Append key and hashed object to string representation. if (!first) { stringFullIDHashTable.append(','); } first = false; stringFullIDHashTable.append(key.toString()); stringFullIDHashTable.append('-'); stringFullIDHashTable.append('>'); stringFullIDHashTable.append(this.get( key )); } } stringFullIDHashTable.append('}'); return stringFullIDHashTable.toString(); } /** * Convert hash table to a printable string representation. * @return String containing object mappings * @see FullID#toString */ public String toString() { return toString( DEFAULT_TOSTRING_ESTIMATE ); } /** * Dump the FullIDTable to an output stream (file). This method * invokes the toStream() method of FullID in writing both the * key and hashed value to the output stream. The format of the * dump is (key, object)*. * @param pw output stream to which hash table should be dumped * @return OK if table dumped successfully; NULL_STREAM if pw was null. */ public int toStream(PrintWriter pw) { Enumeration fullIDKeys; FullID fullID, hashed; if (pw == null) { debug.dumpTrace("FullIDTable.toStream: parameter stream is null"); return NULL_STREAM; } // Dump hash table... for (fullIDKeys = this.keys(); fullIDKeys.hasMoreElements();) { // Get the next key and the value to which it hashes. fullID = (FullID) fullIDKeys.nextElement(); hashed = (FullID) this.get( fullID ); // Output key and hashed object to output stream. fullID.toStream( pw ); hashed.toStream( pw ); if (hashed == null) debug.dumpTrace( "FullIDTable.toStream: FullID hashed to null value" ); } return OK; } }