/* 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 strings.

Source code:  PrimitiveArrayString.java.

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public abstract class PrimitiveArrayString extends PrimitiveArray { /**

Create an PrimitiveArrayString.

Equal to PrimitiveArray(s_fqExtendingClass, par_string)

**/ protected PrimitiveArrayString(String s_fqExtendingClass, PARString par_string) { super(s_fqExtendingClass, par_string); } //Documented in super public final boolean areLmntsEqual(int i_idxThis, PrimitiveArray pa_other, int i_idxOther) { return areLmntsEqual(i_idxThis, (PrimitiveArrayString)pa_other, i_idxOther); } /**

Is an element in this PrimitiveArrayString equal to an element in another?

For documentation on this function, see PrimitiveArray.areLmntsEqual.

**/ public boolean areLmntsEqual(int i_idxThis, PrimitiveArrayString pas_other, int i_idxOther) { try { //Must get other first. throwLmntsEqualAioobx will crash //if it is passed a null pas_other. boolean bNull2 = pas_other.isNull(i_idxOther); boolean bNull1 = isNull(i_idxThis); if(bNull1 && bNull2) { //Both are null. return true; } if(bNull1 || bNull2) { //Exactly one is null. return false; } } catch(ArrayIndexOutOfBoundsException aioobx) { throwLmntsEqualAioobx(i_idxThis, "pas_other", pas_other, i_idxOther); } catch(NullPointerException npx) { throwAX("areLmntsEqual: pas_other is null."); } //Neither are null. return getString(i_idxThis).equals(pas_other.getString(i_idxOther)); } /**

Get the PARString for direct manipulation.

@return (PARString)getPrimitiveArrayRule(). **/ public final PARString getPARString() { return (PARString)getPrimitiveArrayRule(); } /**

Is the [object wrapped to look like an array of strings] 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.PrimitiveArrayString.crashIfInvalid. @param s_varName The descriptive name of the string array, for potential error messages only. @return true If all rules configured into the PARString are followed by the [object wrapped to look like an array of strings].
false If any rules are violated. **/ public final boolean isValid(String s_callingClsFnc, String s_varName) { if(wasValidated()) { return true; } if(!getPARString().isRestricted()) { //There are no restrictions. Everything is legal. declareNoViolation(); return true; } //To make the below code a bit more consice...START boolean bNullOk = getPARString().getPARDupNullLen().isNullOk(); RCLength rclArray = getPARString().getPARDupNullLen().getRCLength(); boolean bDupsOk = getPARString().getPARDupNullLen().areDupsOk(); boolean bContainmentOk = getPARString().getPARSContain().isContainmentOk(); boolean bOrderedContainment = getPARString().getPARSContain().mustBeOrdered(); boolean bContainOrderAscDesc = getPARString().getPARSContain().getOrderDirAscDesc(); boolean bNullLmntOk = getPARString().getPARSElement().isNullLmntOk(); RCLength rclLmnt = getPARString().getPARSElement().getRCLength(); 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 getPARString().getPARDupNullLen().getRCLength().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. final String sLMNT = "element "; final String sPRN_QT = " ('"; for(int i = 0; i < getLength(); i++) { if(!bNullLmntOk && getString(i) == null) { //A null element is bad. setPAViolation(new PAViolation(PAViolation.getType_SLMNT_NULL(), i)); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, sLMNT + i + " is null."); } return false; } //If the element is null, this is okee dokee. if(getString(i) != null && !rclLmnt.isValid(getString(i).length())) { setPAViolation(new PAViolation(PAViolation.getType_SLMNT_LENGTH(), i));; if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, "the length of element " + i + sPRN_QT + getString(i) + "', length " + getString(i).length() + ") is illegal according to getPARString().getPARSElement().getRCLength().isValid()."); } return false; } //Element i is either null-and-this-is-okay, or //non-null-and-its-length-is-valid if(getPARString().getPARSElement().hasIllegalStrings() && getPARString().getPARSElement().isIllegal(getString(i))) { setPAViolation(new PAViolation(PAViolation.getType_LMNT_ILLEGAL(), i)); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, sLMNT + i + " is an illegal string. getPARString().getPARSElement().isIllegal('" + getString(i) + "') equals true."); } return false; } //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((getString(i) == null && getString(j) == null) || (getString(i) != null && getString(j) != null && getString(i).equals(getString(j)))) { setPAViolation(new PAViolation(PAViolation.getType_LMNT_DUPLICATE(), i, j)); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, "elements " + i + " and " + j + " are equal: '" + getString(i) + "'."); } return false; } } //ELSE: It is okay for elements to equal one // another. if(!bContainmentOk) { //It is not okay for one element to be contained //in another. "Contained in" does not mean "equal to". if(getString(i) != null && getString(j) != null && !getString(i).equals(getString(j))) { //i and j are non-null, and unique. These two //strings can be analyzed to see if they are //contained in one another. if(!bOrderedContainment || (bOrderedContainment && bContainOrderAscDesc)) { //ASC: Low idxs can exist in high idxs, but not vice versa. //DESC: High idxs can contain low idxs, but not vice versa. if(getString(i).indexOf(getString(j)) != -1) { setPAViolation(new PAViolation(PAViolation.getType_SLMNT_CONTAINED(), i, j, getString(i).indexOf(getString(j)))); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, sLMNT + i + sPRN_QT + getString(i) + "') contains (but is not equal to) element " + j + sPRN_QT + getString(j) + "') at array index " + getPAViolation().getContainedAtIdx() + "."); } return false; } } if(!bOrderedContainment || (bOrderedContainment && !bContainOrderAscDesc)) { //DESC: High idxs can exist in low idxs, but not vice versa. //DESC: Low idxs can contain high idxs, but not vice versa. if(getString(j).indexOf(getString(i)) != -1) { setPAViolation(new PAViolation(PAViolation.getType_SLMNT_CONTAINED(), j, i, getString(j).indexOf(getString(i)))); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, sLMNT + i + sPRN_QT + getString(i) + "') is contained by (but is not equal to) element " + j + sPRN_QT + getString(j) + "') at array index " + getPAViolation().getContainedAtIdx() + "."); } return false; } } } //ELSE: i and/or j is null, or both are non- // null and equal to each other. Null // strings cannot be contained in any // other. } //ELSE: It is okay for elements to contain (not // equal) one another. if(getPARString().getPAROrderDir().isOrdered()) { //Ordering is required. if(getPARString().getPAROrderDir().getDirectionAscDesc()) { //Order direction is ascending if(getString(i).compareTo(getString(j)) > 0) { setPAViolation(new PAViolation(PAViolation.getType_LMNT_OOORDER(), i, j)); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, "elements " + i + " and " + j + " are both non-null, but " + i + " is greater than " + j + sPRN_QT + getString(i) + "'.compareTo('" + getString(j) + "') equals " + getString(i).compareTo(getString(j)) + ")."); } return false; } //i is less than j. GOOD. continue; } //Order direction is descending if(getString(i).compareTo(getString(j)) < 0) { setPAViolation(new PAViolation(PAViolation.getType_LMNT_OOORDER(), i, j)); if(bAXIfBad) { throwAXIllegal(s_callingClsFnc, s_varName, "elements " + i + " and " + j + " are both non-null, but " + i + " is less than " + j + sPRN_QT + getString(i) + "'.compareTo('" + getString(j) + "') equals " + getString(i).compareTo(getString(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; } }