xbn.util
Class UtilInt

java.lang.Object
  |
  +--xbn.XBNObject
        |
        +--xbn.util.UtilInt

public class UtilInt
extends XBNObject

Random functions to analyze and manipulate ints.

Source code:  UtilInt.java.  Unit tests:  xbn_junit.util.JUTUtilInt.java.


Fields inherited from class xbn.XBNObject
bFALSE_IN_PRODUCTION, bTRUE_IN_PRODUCTION, sCNSTR, sES, sLINE_SEP
 
Constructor Summary
UtilInt()
          Create a UtilInt.
 
Method Summary
 boolean areRangeBoundsValid(int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax)
          Are the bounds for this range valid?
 boolean areRangeBoundsValid(int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax, boolean b_actuallyCheck)
          Are the bounds for this range valid?
 boolean areRangeBoundsValid(int i_min, int i_max)
          Are the bounds for this range valid?
 int getDigitLength(int i_number)
          How many digits in length is the provided number?
 int getIdxNEToLmnt0(int[] a_int)
          What is the first array index containing an int not equal to element zero?
 boolean isBetween(int i_number, int i_min, int i_max)
          Is the number between min and max, but not equal to either bound?.
 boolean isBetweenInclusive(int i_number, int i_min, int i_max)
          Is the number between min and max, or equal to one of the bounds?.
 boolean isGTOEMin(int i_number, int i_min, boolean b_nfrcMin)
          Is the number greater than or equal to the minimum-possible value?
 boolean isInRange(int i_number, int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax)
          Is the number in the range?
 boolean isInRange(int i_number, int i_min, boolean b_nfrcMin, int i_max, boolean b_nfrcMax, boolean b_validateRange)
          Is the number in the range?
 boolean isLTOEMax(int i_number, int i_max, boolean b_nfrcMax)
          Is the number less than or equal to the maimum-possible value?
 
Methods inherited from class xbn.XBNObject
getXMsgPrefix, sop, sopl, sopl, throwAX, throwAXIfBadStr, throwAXIfNull, throwAXSpoof
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UtilInt

public UtilInt()

Create a UtilInt. This constructor does nothing.

Method Detail

isBetween

public final boolean isBetween(int i_number,
                               int i_min,
                               int i_max)

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)


isBetweenInclusive

public final boolean isBetweenInclusive(int i_number,
                                        int i_min,
                                        int i_max)

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)


areRangeBoundsValid

public final boolean areRangeBoundsValid(int i_min,
                                         int i_max)

Are the bounds for this range valid?

Equal to areRangeBoundsValid(i_min, true, i_max, true, true)


areRangeBoundsValid

public final boolean areRangeBoundsValid(int i_min,
                                         boolean b_nfrcMin,
                                         int i_max,
                                         boolean b_nfrcMax)

Are the bounds for this range valid?

Equal to areRangeBoundsValid(i_min, b_nfrcMin, i_max, b_nfrcMax, true)


areRangeBoundsValid

public final boolean areRangeBoundsValid(int i_min,
                                         boolean b_nfrcMin,
                                         int i_max,
                                         boolean b_nfrcMax,
                                         boolean b_actuallyCheck)

Are the bounds for this range valid?

Parameters:
i_min - The minimum bound of the range. Only used if b_nfrcMin is "true".
b_nfrcMin - If "true", then i_min is used. If "false", i_min is ignored.
i_max - The maximum bound of the range. Only used if b_nfrcMax is "true".
b_nfrcMax - If "true", then i_max is used. If "false", i_max is ignored.
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.
Returns:
true
  • If b_actuallyCheck is false.
  • If b_nfrcMin or b_nfrcMax are false.
  • If both b_nfrcMin and b_nfrcMax are true, and i_min is less than or equal to i_max.

false Under any other conditions.

isInRange

public final boolean isInRange(int i_number,
                               int i_min,
                               boolean b_nfrcMin,
                               int i_max,
                               boolean b_nfrcMax)

Is the number in the range?

Equal to isInRange(i_number, i_min, b_nfrcMin, i_max, b_nfrcMax, true)


isInRange

public final boolean isInRange(int i_number,
                               int i_min,
                               boolean b_nfrcMin,
                               int i_max,
                               boolean b_nfrcMax,
                               boolean b_validateRange)

Is the number in the range?

Parameters:
i_number - The number to analyze.
i_min - The minimum bound of the range. If...
  • ...b_nfrcMin is true and b_nfrcMax is false, this may be any value.
  • ...b_nfrcMin is true and b_nfrcMax is true, this must be less than or equal to i_max.
  • ...b_nfrcMin is false, this value is ignored.
b_nfrcMin - If "true", then i_min is used. If "false", i_min is ignored.
i_max - The maximum bound of the range. If...
  • ...b_nfrcMin is false and b_nfrcMax is true, this may be any value.
  • ...b_nfrcMin is true and b_nfrcMax is true, this must be greater than or equal to i_min.
  • ...b_nfrcMax is false, this value is ignored.
b_nfrcMax - If "true", then i_max is used. If "false", i_max is ignored.
b_validateRange - If "true" then validate the range (with areRangeBoundsValid) before analyzing i_number. If the bounds are bad, throw an AssertException. If "false", then it is expected that the range has been validated before this function is called. Use at your own risk, as unpredictable results may occur.
Returns:
(isGTOEMin(i_number, i_min, b_nfrcMin) && isLTOEMax(i_number, i_min, b_nfrcMin)).

isGTOEMin

public final boolean isGTOEMin(int i_number,
                               int i_min,
                               boolean b_nfrcMin)

Is the number greater than or equal to the minimum-possible value?

Parameters:
i_number - The number to analyze.
i_min - If b_nfrcMin is "true", this may be any number. If b_nfrcMin is "false" this is ignored.
Returns:
true
  • If b_nfrcMin is false.
  • If b_nfrcMin is true, and i_number is greater than or equal to i_min.

false If b_nfrcMin is true and i_number is less than i_min.

isLTOEMax

public final boolean isLTOEMax(int i_number,
                               int i_max,
                               boolean b_nfrcMax)

Is the number less than or equal to the maimum-possible value?

Parameters:
i_number - The number to analyze.
i_max - If b_nfrcMax is "true", this may be any number. If b_nfrcMax is "false" this is ignored.
Returns:
true
  • If b_nfrcMax is false.
  • If b_nfrcMax is true, and i_number is less than or equal to i_max.

false If b_nfrcMax is true and i_number is greater than i_max.

getDigitLength

public int getDigitLength(int i_number)

How 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.

Parameters:
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.
Returns:
The number of digits in i_number. For example:
  • 10: Returns 2.
  • -94: Returns 2.
  • 000037: Returns 2.
  • 0: Returns 1
  • 687482: Returns 6

getIdxNEToLmnt0

public final int getIdxNEToLmnt0(int[] a_int)

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.

Parameters:
a_int - The int array to analyze. May not be null or equal to zero elements in length.
Returns:
-1 If a_int.length is one element in length, or every element in the array is equal.
The array index representing the first element in a_int whose value is different than element zero.



Copyright 1997-2003, Jeff Epstein, All Rights Reserved. See top of source code files for copyright notice.

 

http://sourceforge.net/projects/xbnjava

 

SourceForge.net Logo