/* 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.util; import xbn.XBNObject; /**
Defines the legal range for an integer. See Range.
Source code: RangeConfig.java. Unit tests: xbn_junit.JUTRangeConfig.java.
There are four elements of a range:
A range is always inclusive. That is, a number is considered legal when the value is between the minimum and maximum bounds or equal to them. If you need your range to be exclusive, then set the minimum bound to (minimum bound + 1) and maximum bound to (maximum bound - 1).
@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class RangeConfig extends XBNObject { private UtilInt uInt = new UtilInt(); private int iMin = -1; private boolean bEnforceMin = false; private int iMax = -1; private boolean bEnforceMax = false; /**Create a, completely unrestricted, RangeConfig object.
Equal to RangeConfig(-1, false, -1, false)
Create a RangeConfig.
Equal to RangeConfig(i_min, true, i_max, true)
Create a RangeConfig.
All parameters must be valid according to UtilInt.areRangeBoundsValid(i_min, b_nfrcMin, i_max, b_nfrcMax, true)
@param i_min The minimum bound. See getMin. @param b_nfrcMin The minimum bound. See isMinEnforced. @param i_max The minimum bound. See getMax. @param b_nfrcMax The minimum bound. See isMaxEnforced. **/ public RangeConfig(int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax) { if(!uInt.areRangeBoundsValid(i_min, b_nfrcMin, i_max, b_nfrcMax, true)) { throwAX("constructor: Illegal range. b_nfrcMin and b_nfrcMax are both true (read: are both enforced), but i_min is greater than i_max."); } //The range is legal. iMin = i_min; bEnforceMin = b_nfrcMin; iMax = i_max; bEnforceMax = b_nfrcMax; } /**Create a copy of the provided RangeConfig.
@param range_config The range config to copy. May not be null. **/ public RangeConfig(RangeConfig range_config) { try { iMin = range_config.iMin; } catch(NullPointerException npx) { throwAX("constructor: range_config is null."); } bEnforceMin = range_config.bEnforceMin; iMax = range_config.iMax; bEnforceMax = range_config.bEnforceMax; } /**Is the minimum bound enforced?
@return b_nfrcMin exactly as provided to the constructor. **/ public final boolean isMinEnforced() { return bEnforceMin; } /**Is the maximum bound enforced?
@return b_nfrcMax exactly as provided to the constructor. **/ public final boolean isMaxEnforced() { return bEnforceMax; } /**What is the minimum legal value for this range?
@return i_min exactly as provided to the constructor. **/ public final int getMin() { return iMin; } /**What is the maximum legal value for this range?
@return i_max exactly as provided to the constructor. **/ public final int getMax() { return iMax; } /**Is the potential value valid for this Range?
Equal to UtilInt.isInRange(i_potential, getMin(), isMinEnforced(), getMax(), isMaxEnforced(), false)
Are there any restrictions imposed by this RCLength?
@return(isMinEnforced() || isMaxEnforced())
**/
public boolean isRestricted() {
return (isMinEnforced() || isMaxEnforced());
}
/**
Get some information about this RangeConfig.
Equal to toString(true)
Get some information about this RangeConfig.
@param b_className If true, display the class name at the beginning of the returned string. If false, don't. **/ public String toString(boolean b_className) { if(!isRestricted()) { return this.getClass().getName() + ": UNRESTRICTED"; } //Something is being enforced. String sMinEnforced = "not enforced"; if(isMinEnforced()) { sMinEnforced = "enforced"; } String sMaxEnforced = "not enforced"; if(isMaxEnforced()) { sMaxEnforced = "enforced"; } String sCN = sES; if(b_className) { sCN = this.getClass().getName() + ": "; } return sCN + "getMin()=" + getMin() + " (" + sMinEnforced + "), getMax()=" + getMax() + " (" + sMaxEnforced + ")"; } }