/* 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.PAIInt; import java.util.Arrays; /**

An ArrayProtector for chars.

Source code:  APInt.java

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class APInt extends ArrayProtector { private int[] ai = null; private boolean bCloneMade = false; /**

Create an APInt.

**/ public APInt(int[] a_int) { this(a_int, false); } /**

Create an APInt.

**/ public APInt(int[] a_int, boolean b_shallowCloneAOS) { ai = a_int; if(b_shallowCloneAOS) { ai = getAOIClone(); //Now aiClone is set, too. } } /** @return (a_int == null) Where a_int is exactly as provided to the constructor. **/ public final boolean isNull() { return (ai == null); } /** @return a_int.length Where a_int is exactly as provided to the constructor. @exception AssertException If isNull is true. **/ public final int getLength() { try { return ai.length; } catch(NullPointerException npx) { throwAX("getLength: isNull() equals true."); } //Never reached. Required for compile. return -1; } /**

Get the int at the requested array index.

@return a_int[i_dx] Where a_int is exactly as provided to the constructor. **/ public final int getInt(int i_dx) { try { return ai[i_dx]; } catch(NullPointerException npx) { throwAX("getInt: isNull() equals true."); } catch(IndexOutOfBoundsException ioobx) { throwAX("getInt: i_dx (" + i_dx + ") is invalid. getLength()=" + getLength() + "."); } //Never reached. Required for compile. return ' '; } public final String getString(int i_dx) { return (new Integer(getInt(i_dx))).toString(); } /**

Does the provided int exist in the sorted int-array-being-protected?

@return (binarySearch(i_nt) > -1) **/ public final boolean doesExistInSrtd(int i_nt) { return (binarySearch(i_nt) > -1); } /**

Return the array index at which the provided int exists.

You must ensure that the a_char constructor parameter is properly sorted. Otherwise this function provides unpredictable results

@return Arrays.binarySearch([INT-ARRAY-BEING-PROTECTED], i_nt) @exception AssertException If isNull equals true. **/ public final int binarySearch(int i_nt) { try { return Arrays.binarySearch(ai, i_nt); } catch(NullPointerException npx) { throwAX("binarySearch: isNull() equals true."); } //Never reached. Required for compile. return -1; } public final boolean isNull(int i_dx) { return false; } /**

Add the int-array-being-protected to the provided VWInt.

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

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

Get a full (deep) copy of the int-array-being-wrapped.

@return (new UtilArray()).getAOIClone(a_int) Where a_int 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 int[] getAOIClone() { if(bCloneMade) { return ai; } ai = (new UtilArray()).getAOIClone(ai); bCloneMade = true; return ai; } /**

Get a new PAIInt wrapping around the char-array-being-protected.

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

Get a full (deep) copy of this APInt.

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

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

@return (new APInt(a_int)) Where a_int is exactly as provided to the constructor. **/ protected final Object clone() throws CloneNotSupportedException { return new APInt(ai); } //Cloning...END final int[] getAOInt() { return ai; } }