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 class
RangeTests.OutOfRangeException
Thrown 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 void
assertIntegerRange(long value)
assert that along
value is within the range of anInteger
.static void
assertIntegerRange(java.math.BigInteger value)
check that aBigInteger
value is within the range of anInteger
.static void
assertLongRange(java.math.BigInteger value)
assert that aBigInteger
value is within the range of anLong
.static boolean
inIntegerRange(long value)
return whether along
value is within the range of anInteger
.static boolean
inIntegerRange(java.math.BigInteger value)
return whether aBigInteger
value is within the range of anInteger
.static boolean
inLongRange(java.math.BigInteger num)
return whether aBigInteger
value is within the range of anLong
.static boolean
isIntegerDifference(int a, int b)
returnstrue
ifb - a
is an integer.static boolean
isIntegerDifference(long a, long b)
returnstrue
ifb - a
is an integer.static boolean
isLongDifference(long a, long b)
returnstrue
ifb - a
is a long integer.static int
toInt(long value)
"safely" narrows a long to an integer (or throws an exception if the value is out of range).static int
toInt(java.math.BigInteger value)
"safely" narrows aBigInteger
to an integer (or throws an exception if the value is out of range).static long
toLong(java.math.BigInteger value)
"safely" narrows aBigInteger
to 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 aBigInteger
value is within the range of anLong
.- Parameters:
num
- the value to check- Returns:
true
if the value is within the range of anLong
,false
otherwise.
-
assertLongRange
public static void assertLongRange(java.math.BigInteger value)assert that aBigInteger
value is within the range of anLong
. If not, throw an exception.- Parameters:
value
- the value to check- Throws:
RangeTests.OutOfRangeException
- ifvalue
is not within range of a long.
-
toLong
public static long toLong(java.math.BigInteger value)"safely" narrows aBigInteger
to a long(or throws an exception if the value is out of range).- Parameters:
value
- the value to convert- Returns:
value
as an long.- Throws:
RangeTests.OutOfRangeException
- ifvalue
is not within range of a long.
-
inIntegerRange
public static boolean inIntegerRange(long value)return whether along
value is within the range of anInteger
.- Parameters:
value
- the value to check- Returns:
true
if the value is within the range of anInteger
,false
otherwise.
-
inIntegerRange
public static boolean inIntegerRange(java.math.BigInteger value)return whether aBigInteger
value is within the range of anInteger
.- Parameters:
value
- the value to check- Returns:
true
if the value is within the range of anInteger
,false
otherwise.
-
assertIntegerRange
public static void assertIntegerRange(long value)assert that along
value is within the range of anInteger
. If not, throw an exception.- Parameters:
value
- the value to check- Throws:
RangeTests.OutOfRangeException
- ifvalue
is not within range of an integer.
-
assertIntegerRange
public static void assertIntegerRange(java.math.BigInteger value)check that aBigInteger
value is within the range of anInteger
. If not, throw an exception.- Parameters:
value
- the value to check- Throws:
RangeTests.OutOfRangeException
- ifvalue
is 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:
value
as an integer.- Throws:
RangeTests.OutOfRangeException
- ifvalue
is not within range of an integer.
-
toInt
public static int toInt(java.math.BigInteger value)"safely" narrows aBigInteger
to an integer (or throws an exception if the value is out of range).- Parameters:
value
- the value to convert- Returns:
value
as an integer.- Throws:
RangeTests.OutOfRangeException
- ifvalue
is not within range of an integer.
-
isIntegerDifference
public static boolean isIntegerDifference(int a, int b)returnstrue
ifb - a
is an integer. (This is not a given: Consider2^31 - -2^31
.)- Parameters:
a
- one valueb
- the other value- Returns:
true
ifhigh - low
is an integer,false
otherwise.
-
isLongDifference
public static boolean isLongDifference(long a, long b)returnstrue
ifb - a
is a long integer. (This is not a given: Consider2^63 - -2^63
.)- Parameters:
a
- one valueb
- the other value- Returns:
true
ifhigh - low
is a long integer,false
otherwise.
-
isIntegerDifference
public static boolean isIntegerDifference(long a, long b)returnstrue
ifb - a
is 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:
true
ifhigh - low
is an integer,false
otherwise.
-