/* 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.named; import xbn.XBNObject; import xbn.AssertException; import xbn.array.AOSLookup; import xbn.array.AOSLHashtable; import xbn.array.UtilArray; import xbn.array.primitive.ListPA; import xbn.array.primitive.PARSStrict; import xbn.string.SOBStringBuffer; import xbn.string.UtilString; /**

Manage an array of any object implementing the Named interface. Using a AOSLookup, this class provides quick access to the array, with error protection.

Source code:  NamedArray.java.

This object holds both the array of Named objects, as well as the AOSLookup. NamedArray also implements Named, therefore having a name itself. Because of this, you could have a NamedArray of NamedArrays. Pretty cool, eh? :' )

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class NamedArray extends XBNObject implements Named { private UtilString uStr = new UtilString(); private String sName = null; private APNamed apn = null; private AOSLookup aosl = null; /**

Create a NamedArray.

Equal to NamedArray(a_named, true)

**/ public NamedArray(Named[] a_named) { this(a_named, true); } /**

Create a NamedArray.

Equal to NamedArray(null, a_named, true)

**/ public NamedArray(Named[] a_named, boolean b_listNames) { this(null, a_named, b_listNames); } /**

Create a NamedArray.

Equal to NamedArray(s_name, a_named, true)

**/ public NamedArray(String s_name, Named[] a_named) { this(s_name, a_named, true); } /**

Create a NamedArray.

@param s_name The name of this NamedArray. See getName. @param a_named The array of Named objects. Must be non-null, at least one element in length, no elements may be null, and every element's name must be unique. Note that the array is cloned, but all Named elements are unaltered (in other words, the Named elements are not altered, but the array they are contained in, is). See UtilNamed.getShallowClone. @param b_listNames Passed directly to the UtilArray.getAOSLFromPA, which creates the internal AOSLookup for this class. **/ public NamedArray(String s_name, Named[] a_named, boolean b_listNames) { sName = s_name; PASNamed pasn = new PASNamed(a_named, (new PARSStrict())); pasn.crashIfBad("xbn.named.NamedArray.constructor", "a_named"); aosl = (new UtilArray()).getAOSLFromPA(pasn, b_listNames); apn = new APNamed(a_named, true); } public NamedArray(String s_name, Named[] a_named, AOSLookup aos_lookup) { this(s_name, a_named, aos_lookup, true); } /**

Create a NamedArray manually.

**/ public NamedArray(String s_name, Named[] a_named, AOSLookup aos_lookup, boolean b_validate) { this(s_name, (new APNamed(a_named, true)), aos_lookup, b_validate); } /**

Create a NamedArray manually.

@param s_name The name of this NamedArray. @param a_named The array of Named objects. Must be non-null, and the names of these Named objects must map one-to-one with those existing in aos_lookup. @param aos_lookup The AOSLookup for a_named. Must be non-null, and the names contained in it must map one-to-one with those existing in a_named. This is only enforced when b_validate is true. @param b_validate If true, then verify that aos_lookup and a_named are legal for a NamedArray. If false, then you're on your own, as far as this object behaving as documented. If you are absolutely certain that things are okay, it will give you somewhat of a speed increase to make this false. **/ public NamedArray(String s_name, APNamed ap_named, AOSLookup aos_lookup, boolean b_validate) { if(b_validate) { try { ap_named.getPASNamed(new PARSStrict()).crashIfBad("xbn.named.NamedArray.constructor", "a_named"); } catch(NullPointerException npx) { throwAX("constructor: ap_named is null."); } try { aos_lookup.crashIfBadInternalObjs(sCNSTR); } catch(NullPointerException npx) { throwAX("constructor: aos_lookup is null."); } } sName = s_name; aosl = aos_lookup; apn = ap_named; } /**

Create an as-deep-as-possible copy of the provided NamedArray. Everything down-to-but-not-including the Named objects are deeply cloned.

If you need a deep copy, you must manually do the copying yourself, and use the manual constructor. Since Named is just an interface, it is not possible to clone it here.

@param named_array The NamedArray to copy. May not be null. **/ public NamedArray(NamedArray named_array) { try { sName = named_array.sName; } catch(NullPointerException npx) { throwAX("constructor: named_array is null."); } apn = named_array.getAPNamed().getAPNClone(); aosl = named_array.getAOSLookup().getAOSLClone(); } /**

Return the AOSLookup for direct manipulation.

**/ public final AOSLookup getAOSLookup() { return aosl; } /**

Return the AOSLHashtable for direct manipulation.

**/ public final AOSLHashtable getAOSLHashtable() { return getAOSLookup().getAOSLHashtable(); } /**

Get the APNamed for direct manipulation.

@return ap_named Exactly as provided to the constructor. **/ public final APNamed getAPNamed() { return apn; } /**

Get a listing of every name.

@return (new ListPA()).get(getAPNamed().getPASNamed()) **/ public final String getNameList() { return (new ListPA()).get(getAPNamed().getPASNamed()); } /**

Get a listing of every name.

@return (new ListPA(s_divider)).get(getAPNamed().getPASNamed()) **/ public final String getNameList(String s_divider) { return (new ListPA(s_divider)).get(getAPNamed().getPASNamed()); } /**

Get a listing of every name.

@return (new ListPA(s_prefix, s_postfix)).get(getAPNamed().getPASNamed()) **/ public final String getNameList(String s_prefix, String s_postfix) { return (new ListPA(s_prefix, s_postfix)).get(getAPNamed().getPASNamed()); } /**

Get a listing of every name.

@return (new ListPA(s_prefix, s_divider, s_postfix)).get(getAPNamed().getPASNamed()) **/ public final String getNameList(String s_prefix, String s_divider, String s_postfix) { return (new ListPA(s_prefix, s_divider, s_postfix)).get(getAPNamed().getPASNamed()); } /**

How many Named objects exist in this NamedArray?

@return getAPNamed().getLength() **/ public final int getLength() { return getAPNamed().getLength(); } /**

Does the provided name represent an actually-existing Named object?

@return getAOSLHashtable().containsKey(s_name) **/ public final boolean doesExist(String s_name) { return getAOSLHashtable().containsKey(s_name); } /**

Get the Named object at the requested array index.

@param i_dx Must be between 0 and (getCount - 1), inclusive. **/ public final int getArrIdx(String s_name) { try { return getAOSLHashtable().getUniqueString(s_name).getUnqArrIdx(); } catch(AssertException ax) { throwAX("getArrIdx: doesExist('" + s_name + "') equals false."); } //Never reached. Required for compile. return -1; } /**

Get the Named object at the requested array index.

@param i_dx Must be between 0 and (getCount - 1), inclusive. **/ public final Named getNamed(String s_name) { return getAPNamed().getNamed(getArrIdx(s_name)); } /**

Get the Named object at the requested array index.

@param i_dx Must be between 0 and (getCount - 1), inclusive. **/ public final Named getNamed(int i_dx) { return getAPNamed().getNamed(i_dx); } /**

Get the name of the Named object at the requested array index.

@param i_dx Must be valid. **/ public String getName(int i_dx) { return getNamed(i_dx).getName(); } //REQUIRED BY xbn.named.Named...START public String getName() { return sName; } //REQUIRED BY xbn.named.Named...END /**

Get some information about this NamedArray.

@return toString(true) **/ public String toString() { return toString(true); } /**

Throw an exception with getName() as part of the message.

Equal to throwAX(s_callingFunc +
         [UtilString].getConditional(" [getName()=", getName(), "]") +
         ":  " + s_errorMsg)

**/ protected void throwAXWithName(String s_callingFunc, String s_errorMsg) { throwAX(s_callingFunc + uStr.getConditional(" [getName()=", getName(), "]") + ": " + s_errorMsg); } /**

Get some information about this NVArray object, and optionally about the Atomic levels herein.

@param b_subTS If true, then display all toStrings for the sub-Named objects. If false, then display the names of all internal Named objects. **/ public String toString(boolean b_subTS) { String sName = uStr.getConditional("getName()=", getName(), ", "); SOBStringBuffer ssbSubTS = new SOBStringBuffer(sES); if(b_subTS) { (new UtilArray()).appendTSList(ssbSubTS, getAPNamed().getAONShallowClone(), null, " | ", null, null, null, null); } else { ssbSubTS.append("SUB TO STRINGS NOT DISPLAYED"); } return this.getClass().getName() + ": " + sName + "[" + ssbSubTS.toString() + "]"; } }