/* 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.PARString; import xbn.array.primitive.PASString; /**

An ArrayProtector for an array of strings.

Source code:  APString.java

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class APString extends ArrayProtector { private String[] as = null; private String[] asShallowClone = null; /**

Create an APString.

Equal to APString(a_string, false)

**/ public APString(String[] a_string) { this(a_string, false); } /**

Create an APString.

@param a_string The array of strings to protect. @param b_shallowCloneAOS Should a_string be (shallow-ly) cloned to start off--here in this constructor? This recreates a_string as a shallow clone of itself, to prevent external code from altering it. **/ public APString(String[] a_string, boolean b_shallowCloneAOS) { //Must assign a_string to as before getting the shallow clone. //Otherwise the shallow clone will clone as, which would be //*null*. as = a_string; if(b_shallowCloneAOS) { as = getAOSShallowClone(); //Now asShallowClone is set, too. } } /** @return (a_string == null) Where a_string is exactly as provided to the constructor. **/ public final boolean isNull() { return (as == null); } /**

Is the string at the provided array index null?

@return (as[i_dx] == null) **/ public final boolean isNull(int i_dx) { try { return (as[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_string.length Where a_string is exactly as provided to the constructor. @exception AssertException If isNull is true. **/ public final int getLength() { try { return as.length; } catch(NullPointerException npx) { throwAX("getLength: isNull() equals true."); } //Never reached. Required for compile. return -1; } /**

Get the string at the requested array index.

@param i_dx The array index. Must range 0..[getLength() - 1], inclusive. @return a_string[i_dx] Where a_string is exactly as provided to the constructor. @exception AssertException If isNull is true. **/ public final String getString(int i_dx) { try { return as[i_dx]; } catch(NullPointerException npx) { throwAX("getString: isNull() equals true."); } catch(IndexOutOfBoundsException ioobx) { throwAX("getString: i_dx (" + i_dx + ") is invalid. getLength()=" + getLength() + "."); } //Never reached. Required for compile. return null; } /**

Add the string-array-being-protected to the provided VWString.

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

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

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

@return (new UtilArray()).getAOOShallowClone(a_string) Where a_string 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 String[] getAOSShallowClone() { if(asShallowClone != null || isNull()) { return as; } as = (new UtilArray()).getAOSShallowClone(as); return as; } /**

Get a new PASString wrapping around the string-array-being-protected.

@return getPASString(new PARString()) **/ public final PASString getPASString() { return getPASString(new PARString()); } /**

Get a new PASString wrapping around the string-array-being-protected.

@return (new PASString(getAOSShallowClone(), par_string, true)) **/ public final PASString getPASString(PARString par_string) { return (new PASString(getAOSShallowClone(), par_string, true)); } public String toString() { StringBuffer sb = new StringBuffer(this.getClass().getName()).append(": "); if(isNull()) { sb.append("isNull()=true"); return sb.toString(); } //It's not null. sb.append("['").append(getPASString().getList("', '")).append("']"); return sb.toString(); } //Cloning...START /**

Get a full (deep) copy of this APString.

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

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

I know this uses a "shallow" clone of the string array. Does it matter? Is it no different than manually creating new string copies?

@return (new APString(getAOSShallowClone())) **/ protected final Object clone() throws CloneNotSupportedException { return (new APString(getAOSShallowClone())); } //Cloning...END final String[] getAOString() { return as; } }