/*
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.RCLength;
/**
A PrimitiveArray for chars.
Source code: PrimitiveArrayChar.java.
@version 0.9b
@author Jeff Epstein, http://sourceforge.net/projects/xbnjava.
**/
public abstract class PrimitiveArrayChar extends PrimitiveArray {
/**
Create an PrimitiveArrayChar.
The first line of this constructor calls PrimitiveArray(s_fqExtendingClass, par_char)
@param par_char Defines the rules to be enforced by isValid. May not be null. See getPARChar.
**/
protected PrimitiveArrayChar(String s_fqExtendingClass, PARChar par_char) {
super(s_fqExtendingClass, par_char);
}
/**
Get the char at the requested array index.
@param i_dx The array index. Must range 0..[getLength() - 1]
, inclusive.
@exception AssertException If isNull equals true.
**/
public abstract char getChar(int i_dx);
/**
@return (new Character(getChar(i_dx))).toString()
**/
public final String getString(int i_dx) {
return (new Character(getChar(i_dx))).toString();
}
//Documented in super
public final boolean areLmntsEqual(int i_idxThis, PrimitiveArray pa_other, int i_idxOther) {
return areLmntsEqual(i_idxThis, (PrimitiveArrayChar)pa_other, i_idxOther);
}
/**
Is an element in this PrimitiveArrayChar equal to an element in another?
For documentation on this function, see PrimitiveArray.areLmntsEqual.
**/
public boolean areLmntsEqual(int i_idxThis, PrimitiveArrayChar pac_other, int i_idxOther) {
try {
//Must get other first. throwLmntsEqualAioobx will crash
//if it is passed a null pac_other.
int iOther = pac_other.getChar(i_idxOther);
return (getChar(i_idxThis) == iOther);
} catch(NullPointerException npx) {
throwAX("areLmntsEqual: pac_other is null.");
} catch(ArrayIndexOutOfBoundsException aioobx) {
throwLmntsEqualAioobx(i_idxThis, "pac_other", pac_other, i_idxOther);
}
//Never reached. Required for compile.
return false;
}
/**
Get the PARChar for direct manipulation.
@return (PARChar)getPrimitiveArrayRule().
**/
public final PARChar getPARChar() {
return (PARChar)getPrimitiveArrayRule();
}
/**
Is the [object wrapped to look like an array of chars] legal?
@param s_callingClsFnc The name of the class-dot-function for use in potential error messages only. This is the place where the error message should appear that it was generated from. For example: xbn.array.primitive.PrimitiveArrayChar.crashIfInvalid
.
@param s_varName The descriptive name of the char array, for potential error messages only.
@return true If all rules configured into the PARChar are followed by the [object wrapped to look like an array of chars].
false If any rules are violated. Note, when either s_callingClsFnc and/or s_varName are non-null, this instead will throw an AssertException.
**/
public final boolean isValid(String s_callingClsFnc, String s_varName) {
if(wasValidated()) {
return true;
}
if(!getPARChar().isRestricted()) {
//There are no restrictions. Everything is legal.
declareNoViolation();
return true;
}
//To make the below code a bit more consice...START
boolean bNullOk = getPARChar().getPARDupNullLen().isNullOk();
RCLength rclArray = getPARChar().getPARDupNullLen().getRCLength();
boolean bDupsOk = getPARChar().getPARDupNullLen().areDupsOk();
boolean bAXIfBad = (s_callingClsFnc != null || s_varName != null);
//To make the below code a bit more consice...END
if(!bNullOk) {
if(isNull()) {
setPAViolation(new PAViolation(PAViolation.getType_NULL()));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "isNull() equals true.");
}
return false;
}
} else if(isNull()) {
//A null array cannot be analyzed further.
declareNoViolation();
return true;
}
//The array is not null.
if(!rclArray.isValid(getLength())) {
setPAViolation(new PAViolation(PAViolation.getType_LENGTH()));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "the number of elements in the array (" + getLength() + ") is illegal according to getPARChar().getArrayRCL().isValid().");
}
return false;
} else if(getLength() < 1) {
//The array is zero elements in length, and this is okay.
//An empty array can, but does not need to be analyzed further.
declareNoViolation();
return true;
}
//The string array has at least one element.
for(int i = 0; i < getLength(); i++) {
if(getPARChar().hasIllegalChars()) {
//At least one char is considered illegal.
if(getPARChar().isIllegal(getChar(i))) {
setPAViolation(new PAViolation(PAViolation.getType_LMNT_ILLEGAL(), i));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "Element " + i + " ('" + getChar(i) + "') is illegal. getPARChar().isIllegal('" + getChar(i) + "') equals true.");
}
return false;
}
}
//Element i is not equal to any of the illegal characters, or there
//are no illegal chars.
//Compare this element with every element after this one (i + 1)
for(int j = (i + 1); j < getLength(); j++) {
if(!bDupsOk) {
//Duplicates are not good.
if(getChar(i) == getChar(j)) {
setPAViolation(new PAViolation(PAViolation.getType_LMNT_DUPLICATE(), i, j));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "elements " + i + " and " + j + " are equal ('" + getChar(i) + "').");
}
return false;
}
} //ELSE: It is okay for elements to equal one
// another.
//It is not possible for a character to contain
//another (given that if two characters are "equal"
//they are not considered "contained").
if(getPARChar().getPAROrderDir().isOrdered()) {
//Ordering is required.
boolean bOrderDirAD = getPARChar().getPAROrderDir().getDirectionAscDesc();
if(bOrderDirAD) {
//Order direction is ascending
if(getChar(i) > getChar(j)) {
setPAViolation(new PAViolation(PAViolation.getType_LMNT_OOORDER(), i, j));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "element " + i + " ('" + getChar(i) + "') is greater than " + j + " ('" + getChar(j) + "').");
}
return false;
}
//i is less than j. GOOD.
continue;
}
//Order direction is descending
if(getChar(i) < getChar(j)) {
setPAViolation(new PAViolation(PAViolation.getType_LMNT_OOORDER(), i, j));
if(bAXIfBad) {
throwAXIllegal(s_callingClsFnc, s_varName, "element " + i + " ('" + getChar(i) + "') is less than " + j + " ('" + getChar(j) + "').");
}
return false;
}
//i is greater than j. GOOD.
continue;
}
//Ordering is not required.
}
}
//We've gotten this far, everything is good!
declareNoViolation();
return true;
}
}