Package io.protostuff

Class UnsignedNumberUtil

java.lang.Object
io.protostuff.UnsignedNumberUtil

public final class UnsignedNumberUtil extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final long
     
    static final long
     
    private static final int[]
     
    private static final long[]
     
    private static final int[]
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    compareSigned(long a, long b)
    Compares the two specified long values.
    private static int
    compareUnsigned(long a, long b)
    Compares the two specified long values, treating them as unsigned values between 0 and 2^64 - 1 inclusive.
    private static long
    divide(long dividend, long divisor)
    Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
    private static int
    flip(int value)
     
    private static long
    flip(long a)
    A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on longs, that is, a <= b as unsigned longs if and only if flip(a) <= flip(b) as signed longs.
    private static boolean
    overflowInParse(long current, int digit, int radix)
    Returns true if (current * radix) + digit is a number too large to be represented by an unsigned long.
    static int
    Returns the unsigned int value represented by the given decimal string.
    private static int
    parseUnsignedInt(String string, int radix)
    Returns the unsigned int value represented by a string with the given radix.
    static long
    Returns the unsigned long value represented by the given decimal string.
    private static long
    parseUnsignedLong(String s, int radix)
    Returns the unsigned long value represented by a string with the given radix.
    private static long
    remainder(long dividend, long divisor)
    Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
    private static long
    toLong(int value)
    Returns the value of the given int as a long, when treated as unsigned.
    static String
    Returns a string representation of x, where x is treated as unsigned.
    private static String
    unsignedIntToString(int x, int radix)
    Returns a string representation of x for the given radix, where x is treated as unsigned.
    static String
    Returns a string representation of x, where x is treated as unsigned.
    private static String
    unsignedLongToString(long x, int radix)
    Returns a string representation of x for the given radix, where x is treated as unsigned.

    Methods inherited from class java.lang.Object

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

    • MAX_VALUE

      public static final long MAX_VALUE
      See Also:
    • INT_MASK

      static final long INT_MASK
      See Also:
    • maxValueDivs

      private static final long[] maxValueDivs
    • maxValueMods

      private static final int[] maxValueMods
    • maxSafeDigits

      private static final int[] maxSafeDigits
  • Constructor Details

    • UnsignedNumberUtil

      private UnsignedNumberUtil()
  • Method Details

    • flip

      private static int flip(int value)
    • flip

      private static long flip(long a)
      A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on longs, that is, a <= b as unsigned longs if and only if flip(a) <= flip(b) as signed longs.
    • toLong

      private static long toLong(int value)
      Returns the value of the given int as a long, when treated as unsigned.
    • parseUnsignedInt

      public static int parseUnsignedInt(String s)
      Returns the unsigned int value represented by the given decimal string.
      Throws:
      NumberFormatException - if the string does not contain a valid unsigned int value
      NullPointerException - if s is null (in contrast to Integer.parseInt(String))
    • parseUnsignedInt

      private static int parseUnsignedInt(String string, int radix)
      Returns the unsigned int value represented by a string with the given radix.
      Parameters:
      string - the string containing the unsigned integer representation to be parsed.
      radix - the radix to use while parsing s; must be between Character.MIN_RADIX and Character.MAX_RADIX.
      Throws:
      NumberFormatException - if the string does not contain a valid unsigned int, or if supplied radix is invalid.
      NullPointerException - if s is null (in contrast to Integer.parseInt(String))
    • unsignedIntToString

      public static String unsignedIntToString(int x)
      Returns a string representation of x, where x is treated as unsigned.
    • compareUnsigned

      private static int compareUnsigned(long a, long b)
      Compares the two specified long values, treating them as unsigned values between 0 and 2^64 - 1 inclusive.
      Parameters:
      a - the first unsigned long to compare
      b - the second unsigned long to compare
      Returns:
      a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
    • compareSigned

      private static int compareSigned(long a, long b)
      Compares the two specified long values. The sign of the value returned is the same as that of ((Long) a).compareTo(b).

      Note for Java 7 and later: this method should be treated as deprecated; use the equivalent Long.compare(long, long) method instead.

      Parameters:
      a - the first long to compare
      b - the second long to compare
      Returns:
      a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
    • unsignedIntToString

      private static String unsignedIntToString(int x, int radix)
      Returns a string representation of x for the given radix, where x is treated as unsigned.
      Parameters:
      x - the value to convert to a string.
      radix - the radix to use while working with x
      Throws:
      IllegalArgumentException - if radix is not between Character.MIN_RADIX and Character.MAX_RADIX.
    • divide

      private static long divide(long dividend, long divisor)
      Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
      Parameters:
      dividend - the dividend (numerator)
      divisor - the divisor (denominator)
      Throws:
      ArithmeticException - if divisor is 0
    • remainder

      private static long remainder(long dividend, long divisor)
      Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.
      Parameters:
      dividend - the dividend (numerator)
      divisor - the divisor (denominator)
      Throws:
      ArithmeticException - if divisor is 0
      Since:
      11.0
    • parseUnsignedLong

      public static long parseUnsignedLong(String s)
      Returns the unsigned long value represented by the given decimal string.
      Throws:
      NumberFormatException - if the string does not contain a valid unsigned long value
      NullPointerException - if s is null (in contrast to Long.parseLong(String))
    • parseUnsignedLong

      private static long parseUnsignedLong(String s, int radix)
      Returns the unsigned long value represented by a string with the given radix.
      Parameters:
      s - the string containing the unsigned long representation to be parsed.
      radix - the radix to use while parsing s
      Throws:
      NumberFormatException - if the string does not contain a valid unsigned long with the given radix, or if radix is not between Character.MIN_RADIX and Character.MAX_RADIX.
      NullPointerException - if s is null (in contrast to Long.parseLong(String))
    • overflowInParse

      private static boolean overflowInParse(long current, int digit, int radix)
      Returns true if (current * radix) + digit is a number too large to be represented by an unsigned long. This is useful for detecting overflow while parsing a string representation of a number. Does not verify whether supplied radix is valid, passing an invalid radix will give undefined results or an ArrayIndexOutOfBoundsException.
    • unsignedLongToString

      public static String unsignedLongToString(long x)
      Returns a string representation of x, where x is treated as unsigned.
    • unsignedLongToString

      private static String unsignedLongToString(long x, int radix)
      Returns a string representation of x for the given radix, where x is treated as unsigned.
      Parameters:
      x - the value to convert to a string.
      radix - the radix to use while working with x
      Throws:
      IllegalArgumentException - if radix is not between Character.MIN_RADIX and Character.MAX_RADIX.