code: public static int safeToInt(long value) {
          if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
              throw new ArithmeticException("Calculation overflows an int: " + value);
          }
          return (int) value;
      }
docs: Safely convert a long to an int.
  
  @param value  the value to convert
  @return the int value
  @throws ArithmeticException if the result overflows an int