/* 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.array.primitive.PASAOObject; import xbn.string.SOBStringBuffer; import xbn.string.StringOrBuffer; /**

An ArrayProtector for Objects.

Source code:  APObject.java

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class APObject extends ArrayProtector { private Object[] ao = null; private Object[] aoShallowClone = null; /**

Create an APObject.

Equal to APObject(a_object, false)

**/ public APObject(Object[] a_object) { this(a_object, false); } /**

Create an APObject.

@param a_object The array of objects to protect. @param b_shallowCloneAOO Should a_object be (shallow-ly) cloned to start off--here in this constructor? This recreates a_object as a shallow clone of itself, to prevent external code from altering it. **/ public APObject(Object[] a_object, boolean b_shallowCloneAOO) { ao = a_object; if(b_shallowCloneAOO) { ao = getAOOShallowClone(); //Now aoShallowClone is set, too. } } /** @return (a_object == null) Where a_object is exactly as provided to the constructor. **/ public final boolean isNull() { return (ao == null); } /**

Is the object at the provided array index null?

@return (ao[i_dx] == null) **/ public final boolean isNull(int i_dx) { try { return (ao[i_dx] == null); } catch(NullPointerException npx) { throwAX("isNull(int): isNull() equals true."); } catch(IndexOutOfBoundsException ioobx) { throwAX("isNull: i_dx (" + i_dx + ") is invalid. getLength()=" + getLength() + "."); } //Never reached. Required for compile. return false; } /** @return a_object.length Where a_object is exactly as provided to the constructor. @exception AssertException If isNull is true. **/ public final int getLength() { try { return ao.length; } catch(NullPointerException npx) { throwAX("getLength: isNull() equals true."); } //Never reached. Required for compile. return -1; } /**

Get the object at the requested array index.

@return a_object[i_dx] Where a_object is exactly as provided to the constructor. **/ public final Object getObject(int i_dx) { try { return ao[i_dx]; } catch(NullPointerException npx) { throwAX("getObject: isNull() equals true."); } catch(IndexOutOfBoundsException ioobx) { throwAX("getObject: i_dx must be between zero and [getLength() - 1] (" + (getLength() - 1) + "), inclusive. Currently " + i_dx + "."); } //Never reached. Required for compile. return null; } /**

Get a new PASString representing the toStrings() of the internally-held array of Objects.

@return (new PASAOObject([the internally-held array of Objects], true)) **/ public final PASAOObject getPASAOObject() { return (new PASAOObject(ao, true)); } /**

Get a new PASString representing the toStrings() of the internally-held array of Objects.

Equal to getPASAOObject().appendTSList(str_orBfr, s_prefix, s_divider, s_postfix, s_ifNull, s_ifEmpty, s_ifLmntNull);

**/ public final void appendTSList(StringOrBuffer str_orBfr, String s_prefix, String s_divider, String s_postfix, String s_ifNull, String s_ifEmpty, String s_ifLmntNull) { getPASAOObject().appendTSList(str_orBfr, s_prefix, s_divider, s_postfix, s_ifNull, s_ifEmpty, s_ifLmntNull); } /**

Add the array-being-protected to the provided VWObject.

If isNull equals true, nothing is done. If false, then ac_object.addArray([INTERNAL ARRAY]) is called.

@param ac_object The VWObject to add to. May not be null. **/ public final void addToVWObject(VWObject ac_object) { if(isNull()) { return; } try { ac_object.addArray(ao); } catch(NullPointerException npx) { throwAX("addToVWObject: ac_object is null."); } } /**

Get a copy of the object-array-being-wrapped, with references to the original object elements.

@return (new UtilArray()).getAOOShallowClone(a_object) Where a_object is exactly as provided to the constructor. Note that this function only creates the clone once. Once created, the copy is then internally stored, and returned for all subsequent calls. **/ public final synchronized Object[] getAOOShallowClone() { if(aoShallowClone != null || isNull()) { return ao; } ao = (new UtilArray()).getAOOShallowClone(ao); return ao; } public String toString() { StringBuffer sb = new StringBuffer(this.getClass().getName()).append(": "); (new UtilArray()).appendTSList((new SOBStringBuffer(sb)), ao, "[", " | ", "]", "isNull()=true", "length()=0", null); return sb.toString(); } public final String getString(int i_dx) { return getObject(i_dx).toString(); } //Cloning...START /**

Get a full (deep) copy of this APObject.

@return (APObject)clone() **/ public final APObject getAPOClone() { try { return (APObject)clone(); } catch(CloneNotSupportedException cnsx) { throwAX("getAPOClone [SHOULD NEVER HAPPEN!]: " + cnsx.toString()); } //Never reached. Required for compile. return null; } /**

Get a full (deep) copy of this APObject as an Object.

**/ protected Object clone() throws CloneNotSupportedException { return new APObject(ao); } //Cloning...END final Object[] getAOObject() { return ao; } }