/* 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.PACChar; import java.util.Arrays; /**
An ArrayProtector for chars.
Source code: APChar.java
Create an APChar.
**/ public APChar(char[] a_char) { this(a_char, false); } /**Create an APChar.
**/ public APChar(char[] a_char, boolean b_cloneAOC) { ac = a_char; if(b_cloneAOC) { ac = getAOCClone(); //Now acShallowClone is set, too. } } /** @return(a_char == null)
Where a_char is exactly as provided to the constructor.
**/
public final boolean isNull() {
return (ac == null);
}
/**
@return a_char.length
Where a_char is exactly as provided to the constructor.
@exception AssertException If isNull is true.
**/
public final int getLength() {
try {
return ac.length;
} catch(NullPointerException npx) {
throwAX("getLength: isNull() equals true.");
}
//Never reached. Required for compile.
return -1;
}
/**
Get the char at the requested array index.
@returna_char[i_dx]
Where a_char is exactly as provided to the constructor.
**/
public final char getChar(int i_dx) {
try {
return ac[i_dx];
} catch(NullPointerException npx) {
throwAX("getChar: isNull() equals true.");
} catch(IndexOutOfBoundsException ioobx) {
throwAX("getChar: i_dx (" + i_dx + ") is invalid. getLength()=" + getLength() + ".");
}
//Never reached. Required for compile.
return ' ';
}
public final String getString(int i_dx) {
return (new Character(getChar(i_dx))).toString();
}
public final boolean isNull(int i_dx) {
return false;
}
/**
Does the provided character exist in the sorted char-array-being-protected?
@return(binarySearch(c_har) > -1)
**/
public final boolean doesExistInSrtd(char c_har) {
return (binarySearch(c_har) > -1);
}
/**
Return the array index at which the provided character exists.
You must ensure that the a_char constructor parameter is properly sorted. Otherwise this function provides unpredictable results
@returnArrays.binarySearch([CHAR-ARRAY-BEING-PROTECTED], c_har)
@exception AssertException If isNull equals true.
**/
public final int binarySearch(char c_har) {
try {
return Arrays.binarySearch(ac, c_har);
} catch(NullPointerException npx) {
throwAX("binarySearch: isNull() equals true.");
}
//Never reached. Required for compile.
return -1;
}
/**
Add the char-array-being-protected to the provided VWChar.
If isNull equals true, nothing is done. If false, then ac_char.addArray([INTERNAL CHAR ARRAY])
is called.
Get a full (deep) copy of the char-array-being-wrapped.
@return(new UtilArray()).getAOCClone(a_char)
Where a_char 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 char[] getAOCClone() {
if(bCloneMade) {
return ac;
}
ac = (new UtilArray()).getAOCClone(ac);
bCloneMade = true;
return ac;
}
/**
Get a new PACChar wrapping around the char-array-being-protected.
@return(new PACChar(getAOCClone(), true))
**/
public final PACChar getPACChar() {
return (new PACChar(getAOCClone(), true));
}
//Cloning...START
/**
Get a full (deep) copy of this APChar.
@return(APChar)clone()
**/
public final APChar getAPCClone() {
try {
return (APChar)clone();
} catch(CloneNotSupportedException cnsx) {
throwAX("getAPCClone [SHOULD NEVER HAPPEN!]: " + cnsx.toString());
}
//Never reached. Required for compile.
return null;
}
/**
Get a full (deep) copy of this APChar as an Object.
**/ protected final Object clone() throws CloneNotSupportedException { return (new APChar(getAOCClone())); } //Cloning...END 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(getPACChar().getList("', '")).append("']"); return sb.toString(); } final char[] getAOChar() { return ac; } }