Class SafeMath

java.lang.Object
com.aoapps.lang.math.SafeMath

public final class SafeMath extends Object
Math routines that check for overflow conditions.
Author:
AO Industries, Inc.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    avg(int... values)
    Computes the average of multiple values without overflow or underflow.
    static int
    avg(int value1, int value2)
    Computes the average of two values without overflow or underflow.
    static byte
    castByte(int value)
    Casts int to byte, looking for any underflow or overflow.
    static byte
    castByte(long value)
    Casts long to byte, looking for any underflow or overflow.
    static int
    castInt(long value)
    Casts long to int, looking for any underflow or overflow.
    static short
    castShort(int value)
    Casts int to short, looking for any underflow or overflow.
    static short
    castShort(long value)
    Casts long to short, looking for any underflow or overflow.
    static long
    multiply(long... values)
    Multiplies any number of longs, looking for any overflow.
    static long
    multiply(long value1, long value2)
    Deprecated.
    Please use Math.multiplyExact(long, long) as of Java 8.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • castByte

      public static byte castByte(int value) throws ArithmeticException
      Casts int to byte, looking for any underflow or overflow.
      Throws:
      ArithmeticException - for underflow or overflow
    • castByte

      public static byte castByte(long value) throws ArithmeticException
      Casts long to byte, looking for any underflow or overflow.
      Throws:
      ArithmeticException - for underflow or overflow
    • castShort

      public static short castShort(int value) throws ArithmeticException
      Casts int to short, looking for any underflow or overflow.
      Throws:
      ArithmeticException - for underflow or overflow
    • castShort

      public static short castShort(long value) throws ArithmeticException
      Casts long to short, looking for any underflow or overflow.
      Throws:
      ArithmeticException - for underflow or overflow
    • castInt

      public static int castInt(long value) throws ArithmeticException
      Casts long to int, looking for any underflow or overflow.
      Throws:
      ArithmeticException - for underflow or overflow
    • multiply

      @Deprecated public static long multiply(long value1, long value2)
      Deprecated.
      Please use Math.multiplyExact(long, long) as of Java 8.
      Multiplies two longs, looking for any overflow.
      Throws:
      ArithmeticException - for overflow
    • multiply

      public static long multiply(long... values)
      Multiplies any number of longs, looking for any overflow.
      Returns:
      The product or 1 when no values
      Throws:
      ArithmeticException - for overflow
      See Also:
    • avg

      public static int avg(int value1, int value2)
      Computes the average of two values without overflow or underflow.
    • avg

      public static int avg(int... values)
      Computes the average of multiple values without overflow or underflow.
      Throws:
      ArithmeticException - When values is empty (due to resulting division by zero)