/* 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.primitive; import xbn.util.XBNLocked; import xbn.string.SOBString; import xbn.string.SOBStringBuffer; /**

Wraps around anything to make it appear like an array of primitives (including Strings).

Source code:  PrimitiveArray.java.

Note that this class extends XBNLocked, and not XBNObject. This is in anticipation of many extending classes needing to optionally protect/un-protect access to the thing that is being wrapped.

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public abstract class PrimitiveArray extends XBNLocked { private PrimitiveArrayRule par = null; private String sFQClassName = null; private String sFQClassNameFunc = null; private PAViolation pav = null; /**

Create a PrimitiveArray.

@param s_fqClassName The fully qualified name of the class extending this one. This is used when the default crashIfBad function is used. @param pa_rule The definition of the rule that the array-wrapped-by-this-PrimitiveArray must follow. May not be null. **/ protected PrimitiveArray(String s_fqClassName, PrimitiveArrayRule pa_rule) { throwAXIfNull(pa_rule, "pa_rule", sCNSTR); par = pa_rule; sFQClassName = s_fqClassName; sFQClassNameFunc = s_fqClassName + ".crashIfBad"; pav = null; } /**

Get the PrimitiveArrayRule for direct manipulation.

@return pa_rule Exactly as provided to constructor. **/ public final PrimitiveArrayRule getPrimitiveArrayRule() { return par; } /**

Get the element at the requested array index, as a string.

@param i_dx The array index. Must range 0..[getLength() - 1], inclusive. @param AssertException If isNull equals true. **/ public abstract String getString(int i_dx); /**

Get the element at the requested array index, as an SOBString.

@return (new SOBString(getString(i_dx))) **/ public SOBString getSOBString(int i_dx) { return(new SOBString(getString(i_dx))); } /**

Get the element at the requested array index, as an SOBStringBuffer.

@return (new SOBStringBuffer(getString(i_dx))) **/ public SOBStringBuffer getSOBStringBuffer(int i_dx) { return(new SOBStringBuffer(getString(i_dx))); } /**

How many items exist in this PrimitiveArray?

@exception AssertException If isNull equals true. **/ public abstract int getLength(); /**

What is the fully qualified class name of this PrimitiveArray?

@return s_fqClassName Exactly as provided to the constructor. **/ public final String getFQClassName() { return sFQClassName; } /**

Is the [thing being wrapped by this PrimitiveArray] null?

**/ public abstract boolean isNull(); /**

Is the [thing] at the requested array index null?

@param i_dx The array index. Must range 0..[getLength() - 1], inclusive @param AssertException If isNull equals true. **/ public abstract boolean isNull(int i_dx); /**

Are the two elements equal?

@return areLmntsEqual(i_idx1, this, i_idx2) **/ public final boolean areLmntsEqual(int i_idx1, int i_idx2) { return areLmntsEqual(i_idx1, this, i_idx2); } /**

Are the two elements equal?

@param i_idxThis The array index. Must range 0..[getLength() - 1], inclusive @param pa_other The PrimitiveArray to retrieve element i_idxOther from. May not be null. @param i_idxOther The array index of the desired element in pa_other. Must be valid for pa_other. **/ public abstract boolean areLmntsEqual(int i_idxThis, PrimitiveArray pa_other, int i_idxOther); /**

Is the array legal, according to the PrimitiveArrayRule?

@return isValid(null, null) **/ public final boolean isValid() { return isValid(null, null); } /**

Is the array legal, according to the PrimitiveArrayRule?

@param s_callingClsFnc The name of the class-plus-function, only for use in the potential error message. When either this and/or s_varName are non-null, an AssertException is thrown when the array is deemed invalid. When both parameters are null, false is returned when the array is invalid. @param s_varName The name of the array variable, only for use in the potential error message. @return true When the array is completely valid. Note: The moment this function returns true, wasValidated equals true.
false When the array is invalid, and both s_callingClsFnc and s_varName are null. **/ public abstract boolean isValid(String s_callingClsFnc, String s_varName); /**

Declare that this PrimitiveArray and its contents are truly valid.

Right before your version of isValid returns true, be sure to also call this function. See isValid.

This causes wasValidated to return true. There is no way to undo this without recreating the object.

Be careful when calling this function directly. You can call this function manually, but if the [thing being wrapped to look like an array] is not valid, you will get unpredictable results when using this class or its functions. Moral: Don't call this function unless you know what you're doing.

Equal to setPAViolation(new PAViolation())

**/ protected final void declareNoViolation() { setPAViolation(new PAViolation()); } protected final void setPAViolation(PAViolation pa_violation) { throwAXIfNull(pa_violation, "pa_violation", "setPAViolation"); pav = pa_violation; } public final PAViolation getPAViolation() { return pav; } /**

Was the array deemed valid?

@return (getPAViolation() != null && getPAViolation().hasNoViolation()) **/ public final boolean wasValidated() { return (getPAViolation() != null && getPAViolation().hasNoViolation()); } /**

If the char array is not legal, then an AssertException should be thrown with a descriptive error message. Otherwise, do nothing.

Equal to crashIfBad(getFQClassName() + ".crashIfBad", "[the object wrapped by this PrimitiveArray]")

**/ public final void crashIfBad() { crashIfBad(sFQClassNameFunc, "[the object wrapped by this PrimitiveArray]"); } /**

If the char array is not legal, then an AssertException should be thrown with a descriptive error message. Otherwise, do nothing.

Equal to isValid(s_callingClsFnc, s_varName)

Before isValid is called, it is verified that both parameters are non-null, and at least one character in length.

**/ public final void crashIfBad(String s_callingClsFnc, String s_varName) { throwAXIfBadStr(s_callingClsFnc, "s_callingClsFnc", "crashIfBad"); throwAXIfBadStr(s_varName, "s_varName", "crashIfBad"); boolean bUNRTRND = isValid(s_callingClsFnc, s_varName); } /**

Get a new array of strings, based upon all strings existing (via getString) in this PrimitiveArrayString. See getString

@return null If isNull is true.
An array of strings where each element is equal to getString([ARRAY_INDEX]) **/ public final String[] getNewAOString() { if(isNull()) { return null; } String[] as = new String[getLength()]; for(int i = 0; i < getLength(); i++) { as[i] = getString(i); } return as; } /**

Get a new array of SOBStrings. See getSOBString

@return null If isNull is true.
An array of SOBStrings where each element is equal to getSOBString([ARRAY_INDEX]) **/ public final SOBString[] getNewAOSOBString() { if(isNull()) { return null; } SOBString[] asob = new SOBString[getLength()]; for(int i = 0; i < getLength(); i++) { asob[i] = getSOBString(i); } return asob; } /**

Get a new array of SOBStringBuffers. See getSOBStringBuffer

@return null If isNull is true.
An array of SOBStringBuffers where each element is equal to getSOBStringBuffer([ARRAY_INDEX]) **/ public final SOBStringBuffer[] getNewAOSOBStringBuffer() { if(isNull()) { return null; } SOBStringBuffer[] ass = new SOBStringBuffer[getLength()]; for(int i = 0; i < getLength(); i++) { ass[i] = getSOBStringBuffer(i); } return ass; } /**

Get a listing of every item in this Primitive array.

@return (new ListPA()).get(this) **/ public String getList() { return (new ListPA()).get(this); } /**

Get a listing of every item in this Primitive array.

@return (new ListPA(s_divider)).get(this) **/ public String getList(String s_divider) { return (new ListPA(s_divider)).get(this); } /**

Get a listing of every item in this Primitive array.

@return (new ListPA(s_prefix, s_postfix)).get(this) **/ public String getList(String s_prefix, String s_postfix) { return (new ListPA(s_prefix, s_postfix)).get(this); } /**

Get a listing of every item in this Primitive array.

@return (new ListPA(s_prefix, s_divider, s_postfix)).get(this) **/ public String getList(String s_prefix, String s_divider, String s_postfix) { return (new ListPA(s_prefix, s_divider, s_postfix)).get(this); } /**

To be used by implementations of isValid. See isValid.

**/ protected final void throwAXIllegal(String s_callingClsFnc, String s_varName, String s_currently) { throwAXSpoof(s_callingClsFnc + ": " + s_varName + " is illegal according to [" + getPrimitiveArrayRule().toString() + "]. Currently, " + s_currently); } /**

To be used by implementations of areLmntsEqual. See areLmntsEqual.

**/ protected final void throwLmntsEqualAioobx(int i_idxThis, String s_paOtherName, PrimitiveArray pa_other, int i_idxOther) { String s = null; if(i_idxThis < 0 || i_idxThis >= getLength()) { s = "i_idxThis (" + i_idxThis + ") invalid. getLength()=" + getLength() + "."; } try { if(i_idxOther < 0 || i_idxOther >= pa_other.getLength()) { if(s != null) { s += " "; } s += "i_idxThis (" + i_idxThis + ") invalid. " + s_paOtherName + ".getLength()=" + pa_other.getLength() + "."; } } catch(NullPointerException npx) { throwAX("throwLmntsEqualAioobx: pa_other is null. Original parameters: i_idxThis=" + i_idxThis + ", s_paOtherName='" + s_paOtherName + "', i_idxOther=" + i_idxOther); } throwAX("areLmntsEqual: " + s); } }