/* 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; /**
Random functions to analyze and manipulate ints.
Source code: UtilInt.java. Unit tests: xbn_junit.util.JUTUtilInt.java.
Create a UtilInt. This constructor does nothing.
**/ public UtilInt() { } /**Is the number between min and max, but not equal to either bound?. If so, return true. Otherwise return false.
Equal to isInRange(i_number, (i_min + 1), true, (i_max - 1), true)
Is the number between min and max, or equal to one of the bounds?. If so, return true. Otherwise return false.
Equal to isInRange(i_number, i_min, true, i_max, true)
Are the bounds for this range valid?
Equal to areRangeBoundsValid(i_min, true, i_max, true, true)
Are the bounds for this range valid?
Equal to areRangeBoundsValid(i_min, b_nfrcMin, i_max, b_nfrcMax, true)
Are the bounds for this range valid?
@param i_min The minimum bound of the range. Only used if b_nfrcMin is "true". @param b_nfrcMin If "true", then i_min is used. If "false", i_min is ignored. @param i_max The maximum bound of the range. Only used if b_nfrcMax is "true". @param b_nfrcMax If "true", then i_max is used. If "false", i_max is ignored. @param b_actuallyCheck If "true" then do check these bounds, and return true/false based on their validity. If "false", exit the function immediately (and return true). This is needed for external code that can guarantee that the bounds are valid, and don't want to redundantly check each time that isInRange is called. @return trueIs the number in the range?
Equal to isInRange(i_number, i_min, b_nfrcMin, i_max, b_nfrcMax, true)
Is the number in the range?
@param i_number The number to analyze. @param i_min The minimum bound of the range. If...(isGTOEMin(i_number, i_min, b_nfrcMin) && isLTOEMax(i_number, i_min, b_nfrcMin))
.
**/
public final boolean isInRange(int i_number, int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax, boolean b_validateRange) {
if(!areRangeBoundsValid(i_min, b_nfrcMin, i_max, b_nfrcMax, b_validateRange)) {
//They want to validate the range, and the range is
//bad.
throwAX("setRange: Illegal range. b_nfrcMin and b_nfrcMax are both true (are both enforced), but i_min is greater than i_max.");
}
return (isGTOEMin(i_number, i_min, b_nfrcMin) && isLTOEMax(i_number, i_max, b_nfrcMax));
}
/**
Is the number greater than or equal to the minimum-possible value?
@param i_number The number to analyze. @param i_min If b_nfrcMin is "true", this may be any number. If b_nfrcMin is "false" this is ignored. @return trueIs the number less than or equal to the maimum-possible value?
@param i_number The number to analyze. @param i_max If b_nfrcMax is "true", this may be any number. If b_nfrcMax is "false" this is ignored. @return trueHow many digits in length is the provided number?
An alternative method for determining the number of digits is to convert the number to a string, and then get the string's length (although you need to anticipate the possible dash, with negative numbers):
((new Integer(i_number)).toString()).length()
This was an intriguing thing for me to figure out. The answer to "how many digits does my number contain" is "how many times can you divide ten into it?..plus one". Take a look at the code.
@param i_number The number you want to know the amount of digits in. Trailing zeros (zeros on the left side of the number) are ignored, however, the number zero is considered a one-digit number. @return The number of digits in i_number. For example:What is the first array index containing an int not equal to element zero?
This is useful in unit tests that tests multiple versions of the same function, when you want to ensure that each returns the same value.
@param a_int The int array to analyze. May not be null or equal to zero elements in length. @return -1 If a_int.length is one element in length, or every element in the array is equal.