Class MathUtils


  • public final class MathUtils
    extends java.lang.Object
    Contains methods for performing basic numeric operations which handle overflow errors by rounding down to java.lang.Long.MAX_VALUE or up to java.lang.Long.MIN_VALUE.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MathUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long safeAbs​(long v)
      Returns the absolute value of a long value.
      static long safeAdd​(long x, long y)
      Returns the sum of the arguments, rounding up or down if the result overflows a long.
      static long safeDivide​(long x, long y)
      Returns the result of dividing the first argument by the second, rounding down if the result overflows a long.
      static long safeMinus​(long v)  
      static long safeMultiply​(long x, long y)
      Returns the product of the arguments, rounding up or down if the result overflows a long.
      static long safeSubtract​(long x, long y)
      Returns the difference of the arguments, rounding up or down if the result overflows a long.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MathUtils

        private MathUtils()
    • Method Detail

      • safeAdd

        public static long safeAdd​(long x,
                                   long y)
        Returns the sum of the arguments, rounding up or down if the result overflows a long.

        e.g.:
        safeAdd(Long.MAX_Value, 1) returns Long.MAX_VALUE.
        safeAdd(Long.MIN_Value, -1) returns Long.MIN_VALUE.

        See Also:
        Math.addExact(long, long)
      • safeSubtract

        public static long safeSubtract​(long x,
                                        long y)
        Returns the difference of the arguments, rounding up or down if the result overflows a long.

        e.g.:
        safeSubtract(Long.MAX_Value, -1) returns Long.MAX_VALUE.
        safeSubtract(Long.MIN_Value, 1) returns Long.MIN_VALUE.

        See Also:
        Math.subtractExact(long, long)
      • safeMultiply

        public static long safeMultiply​(long x,
                                        long y)
        Returns the product of the arguments, rounding up or down if the result overflows a long.

        e.g.:
        safeMultiply(Long.MAX_Value, 1) returns Long.MAX_VALUE.
        safeMultiply(Long.MIN_Value, 1) returns Long.MIN_VALUE.

        See Also:
        Math.multiplyExact(long, long)
      • safeDivide

        public static long safeDivide​(long x,
                                      long y)
        Returns the result of dividing the first argument by the second, rounding down if the result overflows a long.
      • safeAbs

        public static long safeAbs​(long v)
        Returns the absolute value of a long value.

        safeAbs(Long.MIN_VALUE) returns Long.MAX_VALUE whereas Math.abs(Long.MIN_Value) returns Long.MIN_VALUE.

        See Also:
        Math.abs(long)
      • safeMinus

        public static long safeMinus​(long v)