Class MathUtil

java.lang.Object
io.netty.util.internal.MathUtil

public final class MathUtil extends Object
Math utility methods.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compare(int x, int y)
    Compares two int values.
    static int
    compare(long x, long y)
    Compare two long values.
    static int
    Fast method of finding the next power of 2 greater than or equal to the supplied value.
    static boolean
    isOutOfBounds(int index, int length, int capacity)
    Determine if the requested index and length will fit within capacity.
    static int
    Fast method of finding the next power of 2 greater than or equal to the supplied value.

    Methods inherited from class java.lang.Object

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

    • MathUtil

      private MathUtil()
  • Method Details

    • findNextPositivePowerOfTwo

      public static int findNextPositivePowerOfTwo(int value)
      Fast method of finding the next power of 2 greater than or equal to the supplied value.

      If the value is <= 0 then 1 will be returned. This method is not suitable for Integer.MIN_VALUE or numbers greater than 2^30.

      Parameters:
      value - from which to search for next power of 2
      Returns:
      The next power of 2 or the value itself if it is a power of 2
    • safeFindNextPositivePowerOfTwo

      public static int safeFindNextPositivePowerOfTwo(int value)
      Fast method of finding the next power of 2 greater than or equal to the supplied value.

      This method will do runtime bounds checking and call findNextPositivePowerOfTwo(int) if within a valid range.

      Parameters:
      value - from which to search for next power of 2
      Returns:
      The next power of 2 or the value itself if it is a power of 2.

      Special cases for return values are as follows:

      • <= 0 -> 1
      • >= 2^30 -> 2^30
    • isOutOfBounds

      public static boolean isOutOfBounds(int index, int length, int capacity)
      Determine if the requested index and length will fit within capacity.
      Parameters:
      index - The starting index.
      length - The length which will be utilized (starting from index).
      capacity - The capacity that index + length is allowed to be within.
      Returns:
      false if the requested index and length will fit within capacity. true if this would result in an index out of bounds exception.
    • compare

      public static int compare(int x, int y)
      Compares two int values.
      Parameters:
      x - the first int to compare
      y - the second int to compare
      Returns:
      the value 0 if x == y; -1 if x < y; and 1 if x > y
    • compare

      public static int compare(long x, long y)
      Compare two long values.
      Parameters:
      x - the first long to compare.
      y - the second long to compare.
      Returns:
      • 0 if x == y
      • > 0 if x > y
      • < 0 if x < y