/* 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.ListPA; import xbn.array.primitive.LPAConfig; import xbn.array.primitive.PARDNLGoodUnq; import xbn.array.primitive.PARDupNullLen; import xbn.array.primitive.PARSGoodUnq; import xbn.array.primitive.PARString; import xbn.util.RCLength; /**
Holds metadata about an array of strings. For easy creation of an AOSLookup, see AOSLCreator.
Source code: AOSLookup.java
Note that the original array of strings is not held in this object. You need to hold it externally.
Take this array of strings: ["aaa", "ccc", "bbb", "bbb", "aaa", "ddd", "ccc", "bbb"]
The central piece of information in this AOSLookup is the unique array of UniqueStrings (wrapped by a UniqueStringAP):
getString() |
getUnqArrIdx() |
getInstanceCount() |
aaa | 0 | 2 |
ccc | 1 | 2 |
bbb | 2 | 3 |
ddd | 3 | 1 |
This array contains metadata about each unique string existing in the string array, in the same order as the first instance found. The getUnqArrIdx
value is simply the array index of itself (useless here, but read on...).
The second object is the absolute array of UniqueStrings (wrapped by a UniqueStringAP), in the same order as the string array, and with the same duplication:
getString() |
getUnqArrIdx() |
getInstanceCount() |
aaa | 0 | 2 |
ccc | 1 | 2 |
bbb | 2 | 3 |
bbb | 2 | 3 |
aaa | 0 | 2 |
ddd | 3 | 1 |
ccc | 1 | 2 |
bbb | 2 | 3 |
These UniqueString objects are are not new instances. They are references to the original object, as exists in the unique array of UniqueStrings. It is in this array in which the getUnqArrIdx
value becomes useful.
The getInstanceCount
value is extra information. It's not critical to AOSLookup.
The final object is an AOSLHashtable. An AOSLHashtable wraps around a Hashtable, where the key is a unique string, and the value is the UniqueString object representing it:
Key (unique string) |
Value (UniqueString) |
aaa | [aaa, 0, 2] |
bbb | [bbb, 2, 3] |
ccc | [ccc, 1, 2] |
ddd | [ddd, 3, 1] |
Again, these UniqueString value objects are references to the original objects, as exist in the unique array of UniqueStrings. Note that the ordering presented here is made up. A Hashtable keeps its ordering behind the scenes.
With these three objects, you can quickly access any piece of information from any other.
From the string value, you can retrieve the unique array index with:
getAOSLHashtable().getUniqueString(__THE_STRING__).getUnqArrIdx()
From the unique array index, you can retrieve the string value:
getUSAPUnique().getUniqueString(__THE_UNQ_ARR_IDX__).getString()
From the absolute array index, you can retrieve the string value...
getUSAPAbsolute().getUniqueString(__THE_ABS_ARR_IDX__).getString()
...or the unique array index:
getUSAPAbsolute().getUniqueString(__THE_ABS_ARR_IDX__).getUnqArrIdx()
Create an AOSLookup.unique UniqueStringAP, and absolute UniqueStringAP.
Equal to AOSLookup(aosl_hashtable, usap_unique, usap_absolute, b_listUnqStrs, true)
Create an AOSLookup.unique UniqueStringAP, absolute UniqueStringAP, and list-unique-string setting.
Equal to AOSLookup(aosl_hashtable, usap_unique, usap_absolute, b_listUnqStrs, b_validateParams, true)
Create an AOSLookup.unique UniqueStringAP, absolute UniqueStringAP, and list-unique-string and validate-parameters settings.
@param aosl_hashtable The AOSLHashtable. May not be null. @param usap_unique The UniqueStringAP representing the unique strings in the string array. May not be null, each element must be non-null. @param usap_absolute The UniqueStringAP representing the absolute strings in the string array. May not be null, and no element may be null. @param b_listUnqStrs Should getUnqStrList and appendUSLToSB actually list the strings (absolutely), or should you get a "strings not listed" message? @param b_validateParams ................. **/ public AOSLookup(AOSLHashtable aosl_hashtable, UniqueStringAP usap_unique, UniqueStringAP usap_absolute, boolean b_listUnqStrs, boolean b_validateParams) { aoslh = aosl_hashtable; usapUnique = usap_unique; usapAbsolute = usap_absolute; bListUnqStrs = b_listUnqStrs; if(b_validateParams) { crashIfBadInternalObjs(sCNSTR); } } /**Get the AOSLHashtable for direct manipulation.
@return aosl_hashtable Exactly as provided to the constructor **/ public final AOSLHashtable getAOSLHashtable() { return aoslh; } /**Get the unique UniqueStringAP for direct manipulation.
See the section regarding this object in the overall documentation.
@return usap_unique Exactly as provided to the constructor **/ public final UniqueStringAP getUSAPUnique() { return usapUnique; } /**Get the absolute UniqueStringAP for direct manipulation.
See the section regarding this object in the overall documentation.
@return usap_absolute Exactly as provided to the constructor **/ public final UniqueStringAP getUSAPAbsolute() { return usapAbsolute; } /**Should getUnqStrList and appendUSLToSB print out the list of strings?
@return b_listUnqStrs Exactly as provided to the constructor **/ public final boolean doListUnqStrs() { return bListUnqStrs; } /**Get the list of strings in the original array as a list.
@returngetUnqStrList("")
**/
public final String getUnqStrList() {
return getUnqStrList(sES);
}
/**
Get the list of strings in the original array as a list.
@returngetUnqStrList(s_prefix, "")
**/
public final String getUnqStrList(String s_prefix) {
return getUnqStrList(s_prefix, sES);
}
/**
Get the list of strings in the original array as a list.
@return The resulting value contained in the StringBuffer, after callingappendUSLToSB((new StringBuffer("")), s_prefix, "")
**/
public final String getUnqStrList(String s_prefix, String s_whenNoList) {
StringBuffer sb = new StringBuffer(sES);
appendUSLToSB(sb, s_prefix, s_whenNoList);
return sb.toString();
}
/**
Append the list of strings to the provided StringBuffer.
Equal to appendUSLToSB(str_buffer, "")
Append the list of strings to the provided StringBuffer.
Equal to appendUSLToSB(str_buffer, s_prefix, "")
Append the list of strings to the provided StringBuffer.
@param s_prefix What should be first appended to str_buffer, regardless of the doListUnqStrs setting. If null, no prefix is desired. @param s_whenNoList If doListUnqStrs equals false, then this is the string appended to str_buffer (after s_prefix). **/ public final void appendUSLToSB(StringBuffer str_buffer, String s_prefix, String s_whenNoList) { if(s_prefix != null) { str_buffer.append(s_prefix); } if(doListUnqStrs()) { LPAConfig lpac = new LPAConfig("['", "', '", "']", "null"); (new ListPA(lpac)).append(str_buffer, getUSAPUnique().getUniqueStringPAS()); } else { str_buffer.append(s_whenNoList); } } /**Verifies that all internal objects are correctly formatted, and crashes if they are not.
Equal to crashIfBadInternalObjs("crashIfBadInternalObjs")
Verifies that all internal objects are correctly formatted, and crashes if they are not.
This class verifies that ..............
@param s_callingClsFnc The class-plus-function name from which potential error messages should appear like they're coming from. **/ public final void crashIfBadInternalObjs(String s_callingClsFnc) { throwAXIfNull(usapUnique, "usap_unique", s_callingClsFnc); throwAXIfNull(usapAbsolute, "usap_absolute", s_callingClsFnc); throwAXIfNull(aoslh, "aosl_hashtable", s_callingClsFnc); CompareArrays ca = new CompareArrays(new CACExact()); UniqueStringPAS pasUnq = usapUnique.getUniqueStringPAS(new PARSGoodUnq(new PARDNLGoodUnq(new RCLength(1, -1, false)))); pasUnq.crashIfBad("xbn.array.AOSLookup." + s_callingClsFnc, "usap_unique"); UniqueStringPAS pasAbsolute = usapAbsolute.getUniqueStringPAS(new PARString(new PARDupNullLen(true, false, (new RCLength(1, -1, false))))); pasAbsolute.crashIfBad("xbn.array.AOSLookup." + s_callingClsFnc, "usap_absolute"); ca.crashIfBadValues("xbn.array.AOSLookup." + s_callingClsFnc, "usap_unique", "usap_absolute", pasUnq, pasAbsolute); } //Cloning...START /**Get a full (deep) copy of this AOSLookup.
@return(AOSLookup)clone()
**/
public final AOSLookup getAOSLClone() {
try {
return (AOSLookup)clone();
} catch(CloneNotSupportedException cnsx) {
throwAX("getAOSLClone [SHOULD NEVER HAPPEN!]: " + cnsx.toString());
}
//Never reached. Required for compile.
return null;
}
/**
Get a full (deep) copy of this AOSLookup as an Object.
**/ protected Object clone() throws CloneNotSupportedException { AOSLHashtable aoslh2 = aoslh.getAOSLHClone(); UniqueStringAP usapUnq2 = usapUnique.getUSAPClone(); UniqueStringAP usapAbs2 = usapAbsolute.getUSAPClone(); return (new AOSLookup(aoslh2, usapUnq2, usapAbs2, bListUnqStrs)); } //Cloning...END }