/* 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.XBNObject; import xbn.array.AOSLHashtable; import xbn.array.APString; import xbn.util.RCLength; import xbn.util.Utility; import java.util.Hashtable; /**

Part of PARString configuring element-related items. See PARString.

Source code:  PARSElement.java

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class PARSElement extends XBNObject implements Cloneable { private boolean bLmntNullOk = false; private RCLength rclLmnt = null; private String[] asIllegal = null; private AOSLHashtable aoslh = null; private APString apsIllegal = null; /**

Create an unrestricted PARSElement.

Equal to PARSElement(true)

**/ public PARSElement() { this(true); } /**

Create a PARSElement.

Equal to PARSElement(b_lmntNullOk, (new RCLength()))

**/ public PARSElement(boolean b_lmntNullOk) { this(b_lmntNullOk, (new RCLength())); } /**

Create a PARSElement.

Equal to PARSElement(true, rcl_lmntLength)

**/ public PARSElement(RCLength rcl_lmntLength) { this(true, rcl_lmntLength); } /**

Create a PARSElement.

Equal to PARSElement(true, as_illegal)

**/ public PARSElement(String[] as_illegal) { this(true, as_illegal); } /**

Create a PARSElement with the element-null setting and RCLength.

Equal to PARSElement(b_lmntNullOk, rcl_lmntLength, (new String[] {}))

**/ public PARSElement(boolean b_lmntNullOk, RCLength rcl_lmntLength) { this(b_lmntNullOk, rcl_lmntLength, (new String[] {})); } /**

Create a PARSElement.

Equal to PARSElement(b_lmntNullOk, (new RCLength()), as_illegal)

**/ public PARSElement(boolean b_lmntNullOk, String[] as_illegal) { this(b_lmntNullOk, (new RCLength()), as_illegal); } /**

Create a PARSElement.

Equal to PARSElement(true, rcl_lmntLength, as_illegal)

**/ public PARSElement(RCLength rcl_lmntLength, String[] as_illegal) { this(true, rcl_lmntLength, as_illegal); } /**

Create a PARSElement.

@param b_lmntNullOk If true, then any element in the PrimitiveArrayString may be null. If false, then no element may be null. @param rcl_lmntLength The range that each element's length must conform to. May not be null. @param as_illegal The array of strings that no element may be equal to. May not be null. If zero elements in length, then no strings are considered illegal. If at least one element in length, then each element's length must be legal according to rcl_lmntLength.isValid. Duplicate strings in this array are ignored. **/ public PARSElement(boolean b_lmntNullOk, RCLength rcl_lmntLength, String[] as_illegal) { throwAXIfNull(rcl_lmntLength, "rcl_lmntLength", sCNSTR); throwAXIfNull(as_illegal, "as_illegal", sCNSTR); //This is optimized only if as_illegal contains no duplicates. Hashtable ht = new Hashtable(as_illegal.length, 1); if(as_illegal.length > 0) { for(int i = 0; i < as_illegal.length; i++) { if(as_illegal[i] == null || as_illegal[i].length() < 1) { throwAX("constructor: as_illegal[" + i + "] ('" + as_illegal[i] + "') must be non-null, and at least one character in length."); } if(!rcl_lmntLength.isValid(as_illegal[i].length())) { throwAX("constructor: as_illegal[" + i + "] ('" + as_illegal[i] + "') has an illegal length: [" + rcl_lmntLength.toString() + "]."); } if(!ht.containsKey(as_illegal[i])) { ht.put(as_illegal[i], (new Integer(i))); } } if(ht.size() != as_illegal.length) { //There are duplicates in as_illegal. Re-optimize the Hashtable. ht = (new Utility()).getOptimizedHT(ht); } aoslh = new AOSLHashtable(ht, bFALSE_IN_PRODUCTION); } else { aoslh = null; } bLmntNullOk = b_lmntNullOk; rclLmnt = rcl_lmntLength; asIllegal = as_illegal; apsIllegal = new APString(as_illegal); } /**

Is a character defined as illegal, should it be found in the char array?

@return b_lmntNullOk Exactly as provided to the constructor. **/ public final boolean isNullLmntOk() { return bLmntNullOk; } /**

What character is defined as illegal, should it be found in the char array?

@return rcl_lmntLength Exactly as provided to the constructor. **/ public final RCLength getRCLength() { return rclLmnt; } /**

Get the AOSLHashtable containing the strings considered illegal.

@return An AOSLHashtable containing only those strings in as_illegal (as provided to the constructor) that are unique. If as_illegal.length() was zero, then this function will return null. **/ public final AOSLHashtable getAOSLHIllegal() { return aoslh; } /**

Is the provided string illegal?

@param s_tr The string to determine illegality of. May not be null. @return false if getAOSLHIllegal is null.
getAOSLHIllegal().containsKey(s_tr) If otherwise. **/ public boolean isIllegal(String s_tr) { if(getAOSLHIllegal() == null) { return false; } //There is at least one illegal string. return getAOSLHIllegal().containsKey(s_tr); } /**

Are there strings considered illegal?

@return (getAPSIllegal().getLength() > 0) **/ public final boolean hasIllegalStrings() { return (getAPSIllegal().getLength() > 0); } /**

Get the array of strings considered illegal, as an APString.

@return (new APString(as_illegal)) Where ai_illegal is exactly as provided to the constructor. Note this object is only created once. **/ public final APString getAPSIllegal() { return apsIllegal; } /**

Does this PARSEIllegal have any restrictions?

@return (!isNullLmntOk() || getRCLength().isRestricted() || hasIllegalStrings()) **/ public boolean isRestricted() { return (!isNullLmntOk() || getRCLength().isRestricted() || hasIllegalStrings()); } /**

Get some information about this PARSEIllegal.

**/ public String toString() { return this.getClass().getName() + ": isNullLmntOk()=" + isNullLmntOk() + ", [" + getRCLength().toString() + "], getAPSIllegal()=[" + getAPSIllegal().toString() + "]"; } //Cloning...START /**

Get a full (deep) copy of this PARSElement.

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

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

**/ protected final Object clone() throws CloneNotSupportedException { return new PARSElement(bLmntNullOk, rclLmnt, asIllegal, aoslh, apsIllegal); } private PARSElement(boolean b_lmntNullOk, RCLength rcl_lmntLength, String[] as_illegal, AOSLHashtable aosl_hashtable, APString aps_illegal) { bLmntNullOk = b_lmntNullOk; rclLmnt = rcl_lmntLength; asIllegal = as_illegal; aoslh = aosl_hashtable.getAOSLHClone(); apsIllegal = aps_illegal.getAPSClone(); } //Cloning...END }