/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.array; import xbn.XBNObject; import xbn.array.primitive.PASString; import xbn.array.primitive.PAIInt; import xbn.array.primitive.PARIStrict; import xbn.util.Utility; import java.util.Enumeration; import java.util.Hashtable; /**
Holds metadata for an array of (possibly unique) string values in a Hashtable, for use in an AOSLookup. See java.util.Hashtable and AOSLookup.
Source code: AOSLHashtable.java
This object wraps around a Hashtable, which is expected to have keys that are the strings from the array, and values which are UniqueStrings, containing metadata about the key string.
@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class AOSLHashtable extends XBNObject implements Cloneable { private Hashtable ht = null; private PASString pass = null; /**Create an AOSLHashtable.
Equal to AOSLHashtable(h_ashtable, false)
Create an AOSLHashtable.
@param h_ashtable The hashtable to wrap around. May not be null. @param b_verifyHashtable If true, then h_ashtable is verified to ensure that the keys are a set of unique strings, the values are of type UniqueString, the key and UniqueString.getString are equal, and all the UniqueString.getUnqArrIdx are unique, zero or greater, and that every value between zero and [whatever the maximum value is] exists. If any of these requirements are not met, then an AssertException is thrown. If this parameter is false, then it is assumed that h_ashtable's keys and values are valid. Use false with care. **/ public AOSLHashtable(Hashtable h_ashtable, boolean b_verifyHashtable) { throwAXIfNull(h_ashtable, "h_ashtable", sCNSTR); ht = h_ashtable; if(b_verifyHashtable) { Enumeration e = keys(); VWInt vwi = new VWInt(); while(e.hasMoreElements()) { String sKey = (String)e.nextElement(); UniqueString us = null; try { us = ((UniqueString)ht.get(sKey)); } catch(ClassCastException ccx) { throwAX("constructor: h_ashtable.get(" + sKey + ") is not of type xbn.array.UniqueString. Currently, '" + ht.get(sKey).getClass().getName() + "'."); } if(!sKey.equals(us.getString())) { throwAX("constructor: Key named '" + sKey + "' has a UniqueString object as its value, but UniqueString.getString() equals '" + us.getString() + "'. They must be equal."); } vwi.add(us.getUnqArrIdx()); } PAIInt paii = new PAIInt(vwi.getAOInt(), (new PARIStrict())); paii.crashIfBad("xbn.array.AOSLHashtable.constructor", "[The UniqueString.getUnqArrIdx() int values found in each UniqueString value in h_ashtable]"); int iMin = paii.getMinimum(); int iMax = paii.getMaximum(); if(iMin != 0) { throwAX("constructor: The minimum unique array index (UniqueString.getUnqArrIdx()) must equal zero (meaning the first element in the array being represented). Currently equals " + iMin + "."); } if(h_ashtable.size() != (iMax + 1)) { throwAX("constructor: h_ashtable.size() equals " + h_ashtable.size() + ", but the maximum UniqueString.getUnqArrIdx() value equals " + iMax + ". The maximum value must equal (h_ashtable.size() - 1) (" + (h_ashtable.size() - 1) + ")"); } } } /**Is the provided string an existing key in the Hashtable?
@param s_tr The string to analyze. @return[h_ashtable].containsKey(s_tr)
Where h_ashtable is exactly as provided to the constructor.
**/
public boolean containsKey(String s_tr) {
throwAXIfNull(s_tr, "s_tr", "containsKey");
return ht.containsKey(s_tr);
}
/**
Get the UniqueString object having a certain key.
@param s_key The string value of the key, which points to the desired UniqueString object. @return((UniqueString)h_ashtable.get(s_key))
Where h_ashtable is exactly as provided to the constructor.
**/
public UniqueString getUniqueString(String s_key) {
try {
UniqueString us = ((UniqueString)ht.get(s_key));
if(us == null) {
throwAX("getUniqueString: containsKey(s_key) equals false. s_key equals '" + s_key + "'.");
}
return us;
} catch(ClassCastException ccx) {
throwAX("getUniqueString: ht.get(s_key) is not of type xbn.array.UniqueString. Currently, '" + ht.get(s_key).getClass().getName() + "'. s_key equals '" + s_key + "'. Note: Setting the second constructor parameter (b_verifyHashtable) to true will verify that the values in the Hashtable are UniqueStrings, as they need to be.");
}
//Never reached. Needed for complie.
return null;
}
/**
How many elements exist in the Hashtable?
@returnh_ashtable.size
Where h_ashtable is exactly as provided to the constructor.
**/
public int size() {
return ht.size();
}
/**
Get an Enumeration containing every key in the Hashtable.
@returnh_ashtable.keys
Where h_ashtable is exactly as provided to the constructor.
**/
public Enumeration keys() {
return ht.keys();
}
/**
Get an Enumeration containing every element in the Hashtable.
@returnh_ashtable.elements
Where h_ashtable is exactly as provided to the constructor.
**/
public Enumeration elements() {
return ht.elements();
}
/**
Get a PASString containing a (newly created) string array of the names in this AOSLookup.
Note the order of the string array is exactly the same as was provided to the constructor.
**/ public final synchronized PASString getPASString() { if(pass != null) { return pass; } //We want the names in order of array index. We need to put the names into an array and order them before printing them out. String[] asNames = new String[size()]; Enumeration eKeys = keys(); Enumeration eValues = elements(); while(eKeys.hasMoreElements()) { asNames[((UniqueString)eValues.nextElement()).getUnqArrIdx()] = eKeys.nextElement().toString(); } pass = new PASString(asNames); return pass; } //Cloning...START /**Get a full (deep) clone of the internally held Hashtable.
@return(new Utility().getOptimizedHT(h_ashtable))
Where h_ashtable is exactly as provided to the constructor.
**/
public final Hashtable getHTClone() {
return (new Utility()).getOptimizedHT(ht);
}
/**
Get a full (deep) copy of this AOSLHashtable.
@return(AOSLHashtable)clone()
**/
public final AOSLHashtable getAOSLHClone() {
try {
return (AOSLHashtable)clone();
} catch(CloneNotSupportedException cnsx) {
throwAX("getAOSLHClone [SHOULD NEVER HAPPEN!]: " + cnsx.toString());
}
//Never reached. Required for compile.
return null;
}
/**
Get a full (deep) copy of this AOSLHashtable as an Object.
**/ protected Object clone() throws CloneNotSupportedException { return (new AOSLHashtable(getHTClone(), bFALSE_IN_PRODUCTION)); } //Cloning...END }