Package edu.gvsu.kurmasz.warszawa.util
Class RangeTests
java.lang.Object
edu.gvsu.kurmasz.warszawa.util.RangeTests
public class RangeTests
extends java.lang.Object
Methods to determine whether the value of a "wide" data type falls within the
range of a "narrower" data type.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRangeTests.OutOfRangeExceptionThrown when the value of a wide data type doesn't fit in the narrower data type. -
Constructor Summary
Constructors Constructor Description RangeTests() -
Method Summary
Modifier and Type Method Description static voidassertIntegerRange(long value)assert that alongvalue is within the range of anInteger.static voidassertIntegerRange(java.math.BigInteger value)check that aBigIntegervalue is within the range of anInteger.static voidassertLongRange(java.math.BigInteger value)assert that aBigIntegervalue is within the range of anLong.static booleaninIntegerRange(long value)return whether alongvalue is within the range of anInteger.static booleaninIntegerRange(java.math.BigInteger value)return whether aBigIntegervalue is within the range of anInteger.static booleaninLongRange(java.math.BigInteger num)return whether aBigIntegervalue is within the range of anLong.static booleanisIntegerDifference(int a, int b)returnstrueifb - ais an integer.static booleanisIntegerDifference(long a, long b)returnstrueifb - ais an integer.static booleanisLongDifference(long a, long b)returnstrueifb - ais a long integer.static inttoInt(long value)"safely" narrows a long to an integer (or throws an exception if the value is out of range).static inttoInt(java.math.BigInteger value)"safely" narrows aBigIntegerto an integer (or throws an exception if the value is out of range).static longtoLong(java.math.BigInteger value)"safely" narrows aBigIntegerto a long(or throws an exception if the value is out of range).
-
Constructor Details
-
RangeTests
public RangeTests()
-
-
Method Details
-
inLongRange
public static boolean inLongRange(java.math.BigInteger num)return whether aBigIntegervalue is within the range of anLong.- Parameters:
num- the value to check- Returns:
trueif the value is within the range of anLong,falseotherwise.
-
assertLongRange
public static void assertLongRange(java.math.BigInteger value)assert that aBigIntegervalue is within the range of anLong. If not, throw an exception.- Parameters:
value- the value to check- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of a long.
-
toLong
public static long toLong(java.math.BigInteger value)"safely" narrows aBigIntegerto a long(or throws an exception if the value is out of range).- Parameters:
value- the value to convert- Returns:
valueas an long.- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of a long.
-
inIntegerRange
public static boolean inIntegerRange(long value)return whether alongvalue is within the range of anInteger.- Parameters:
value- the value to check- Returns:
trueif the value is within the range of anInteger,falseotherwise.
-
inIntegerRange
public static boolean inIntegerRange(java.math.BigInteger value)return whether aBigIntegervalue is within the range of anInteger.- Parameters:
value- the value to check- Returns:
trueif the value is within the range of anInteger,falseotherwise.
-
assertIntegerRange
public static void assertIntegerRange(long value)assert that alongvalue is within the range of anInteger. If not, throw an exception.- Parameters:
value- the value to check- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of an integer.
-
assertIntegerRange
public static void assertIntegerRange(java.math.BigInteger value)check that aBigIntegervalue is within the range of anInteger. If not, throw an exception.- Parameters:
value- the value to check- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of an integer.
-
toInt
public static int toInt(long value)"safely" narrows a long to an integer (or throws an exception if the value is out of range).- Parameters:
value- the value to convert- Returns:
valueas an integer.- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of an integer.
-
toInt
public static int toInt(java.math.BigInteger value)"safely" narrows aBigIntegerto an integer (or throws an exception if the value is out of range).- Parameters:
value- the value to convert- Returns:
valueas an integer.- Throws:
RangeTests.OutOfRangeException- ifvalueis not within range of an integer.
-
isIntegerDifference
public static boolean isIntegerDifference(int a, int b)returnstrueifb - ais an integer. (This is not a given: Consider2^31 - -2^31.)- Parameters:
a- one valueb- the other value- Returns:
trueifhigh - lowis an integer,falseotherwise.
-
isLongDifference
public static boolean isLongDifference(long a, long b)returnstrueifb - ais a long integer. (This is not a given: Consider2^63 - -2^63.)- Parameters:
a- one valueb- the other value- Returns:
trueifhigh - lowis a long integer,falseotherwise.
-
isIntegerDifference
public static boolean isIntegerDifference(long a, long b)returnstrueifb - ais an integer. Such a check is important if one is specifying an array size based on long integer bounds. (Array sizes and indices must be integers.)- Parameters:
a- one valueb- the other value- Returns:
trueifhigh - lowis an integer,falseotherwise.
-