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 a long value is within the range of an Integer.
    static void assertIntegerRange​(java.math.BigInteger value)
    check that a BigInteger value is within the range of an Integer .
    static void assertLongRange​(java.math.BigInteger value)
    assert that a BigInteger value is within the range of an Long.
    static boolean inIntegerRange​(long value)
    return whether a long value is within the range of an Integer.
    static boolean inIntegerRange​(java.math.BigInteger value)
    return whether a BigInteger value is within the range of an Integer.
    static boolean inLongRange​(java.math.BigInteger num)
    return whether a BigInteger value is within the range of an Long.
    static boolean isIntegerDifference​(int a, int b)
    returns true if b - a is an integer.
    static boolean isIntegerDifference​(long a, long b)
    returns true if b - a is an integer.
    static boolean isLongDifference​(long a, long b)
    returns true if b - 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 a BigInteger to an integer (or throws an exception if the value is out of range).
    static long toLong​(java.math.BigInteger value)
    "safely" narrows a BigInteger to a long(or throws an exception if the value is out of range).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • inLongRange

      public static boolean inLongRange​(java.math.BigInteger num)
      return whether a BigInteger value is within the range of an Long.
      Parameters:
      num - the value to check
      Returns:
      true if the value is within the range of an Long, false otherwise.
    • assertLongRange

      public static void assertLongRange​(java.math.BigInteger value)
      assert that a BigInteger value is within the range of an Long. If not, throw an exception.
      Parameters:
      value - the value to check
      Throws:
      RangeTests.OutOfRangeException - if value is not within range of a long.
    • toLong

      public static long toLong​(java.math.BigInteger value)
      "safely" narrows a BigInteger 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 - if value is not within range of a long.
    • inIntegerRange

      public static boolean inIntegerRange​(long value)
      return whether a long value is within the range of an Integer.
      Parameters:
      value - the value to check
      Returns:
      true if the value is within the range of an Integer, false otherwise.
    • inIntegerRange

      public static boolean inIntegerRange​(java.math.BigInteger value)
      return whether a BigInteger value is within the range of an Integer.
      Parameters:
      value - the value to check
      Returns:
      true if the value is within the range of an Integer, false otherwise.
    • assertIntegerRange

      public static void assertIntegerRange​(long value)
      assert that a long value is within the range of an Integer. If not, throw an exception.
      Parameters:
      value - the value to check
      Throws:
      RangeTests.OutOfRangeException - if value is not within range of an integer.
    • assertIntegerRange

      public static void assertIntegerRange​(java.math.BigInteger value)
      check that a BigInteger value is within the range of an Integer . If not, throw an exception.
      Parameters:
      value - the value to check
      Throws:
      RangeTests.OutOfRangeException - if value 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 - if value is not within range of an integer.
    • toInt

      public static int toInt​(java.math.BigInteger value)
      "safely" narrows a BigInteger 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 - if value is not within range of an integer.
    • isIntegerDifference

      public static boolean isIntegerDifference​(int a, int b)
      returns true if b - a is an integer. (This is not a given: Consider 2^31 - -2^31.)
      Parameters:
      a - one value
      b - the other value
      Returns:
      true if high - low is an integer, false otherwise.
    • isLongDifference

      public static boolean isLongDifference​(long a, long b)
      returns true if b - a is a long integer. (This is not a given: Consider 2^63 - -2^63.)
      Parameters:
      a - one value
      b - the other value
      Returns:
      true if high - low is a long integer, false otherwise.
    • isIntegerDifference

      public static boolean isIntegerDifference​(long a, long b)
      returns true if b - 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 value
      b - the other value
      Returns:
      true if high - low is an integer, false otherwise.