Package io.protostuff
Class UnsignedNumberUtil
- java.lang.Object
-
- io.protostuff.UnsignedNumberUtil
-
public final class UnsignedNumberUtil extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static long
INT_MASK
static long
MAX_VALUE
private static int[]
maxSafeDigits
private static long[]
maxValueDivs
private static int[]
maxValueMods
-
Constructor Summary
Constructors Modifier Constructor Description private
UnsignedNumberUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static int
compareSigned(long a, long b)
Compares the two specifiedlong
values.private static int
compareUnsigned(long a, long b)
Compares the two specifiedlong
values, treating them as unsigned values between0
and2^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 ifflip(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
parseUnsignedInt(java.lang.String s)
Returns the unsignedint
value represented by the given decimal string.private static int
parseUnsignedInt(java.lang.String string, int radix)
Returns the unsignedint
value represented by a string with the given radix.static long
parseUnsignedLong(java.lang.String s)
Returns the unsignedlong
value represented by the given decimal string.private static long
parseUnsignedLong(java.lang.String s, int radix)
Returns the unsignedlong
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 givenint
as along
, when treated as unsigned.static java.lang.String
unsignedIntToString(int x)
Returns a string representation of x, where x is treated as unsigned.private static java.lang.String
unsignedIntToString(int x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.static java.lang.String
unsignedLongToString(long x)
Returns a string representation of x, where x is treated as unsigned.private static java.lang.String
unsignedLongToString(long x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.
-
-
-
Field Detail
-
MAX_VALUE
public static final long MAX_VALUE
- See Also:
- Constant Field Values
-
INT_MASK
static final long INT_MASK
- See Also:
- Constant Field Values
-
maxValueDivs
private static final long[] maxValueDivs
-
maxValueMods
private static final int[] maxValueMods
-
maxSafeDigits
private static final int[] maxSafeDigits
-
-
Method Detail
-
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 ifflip(a) <= flip(b)
as signed longs.
-
toLong
private static long toLong(int value)
Returns the value of the givenint
as along
, when treated as unsigned.
-
parseUnsignedInt
public static int parseUnsignedInt(java.lang.String s)
Returns the unsignedint
value represented by the given decimal string.- Throws:
java.lang.NumberFormatException
- if the string does not contain a valid unsignedint
valuejava.lang.NullPointerException
- ifs
is null (in contrast toInteger.parseInt(String)
)
-
parseUnsignedInt
private static int parseUnsignedInt(java.lang.String string, int radix)
Returns the unsignedint
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 parsings
; must be betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.- Throws:
java.lang.NumberFormatException
- if the string does not contain a valid unsignedint
, or if supplied radix is invalid.java.lang.NullPointerException
- ifs
is null (in contrast toInteger.parseInt(String)
)
-
unsignedIntToString
public static java.lang.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 specifiedlong
values, treating them as unsigned values between0
and2^64 - 1
inclusive.- Parameters:
a
- the first unsignedlong
to compareb
- the second unsignedlong
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
-
compareSigned
private static int compareSigned(long a, long b)
Compares the two specifiedlong
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 firstlong
to compareb
- the secondlong
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
-
unsignedIntToString
private static java.lang.String unsignedIntToString(int x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.- Parameters:
x
- the value to convert to a string.radix
- the radix to use while working withx
- Throws:
java.lang.IllegalArgumentException
- ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.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:
java.lang.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:
java.lang.ArithmeticException
- if divisor is 0- Since:
- 11.0
-
parseUnsignedLong
public static long parseUnsignedLong(java.lang.String s)
Returns the unsignedlong
value represented by the given decimal string.- Throws:
java.lang.NumberFormatException
- if the string does not contain a valid unsignedlong
valuejava.lang.NullPointerException
- ifs
is null (in contrast toLong.parseLong(String)
)
-
parseUnsignedLong
private static long parseUnsignedLong(java.lang.String s, int radix)
Returns the unsignedlong
value represented by a string with the given radix.- Parameters:
s
- the string containing the unsignedlong
representation to be parsed.radix
- the radix to use while parsings
- Throws:
java.lang.NumberFormatException
- if the string does not contain a valid unsignedlong
with the given radix, or ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.java.lang.NullPointerException
- ifs
is null (in contrast toLong.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 java.lang.String unsignedLongToString(long x)
Returns a string representation of x, where x is treated as unsigned.
-
unsignedLongToString
private static java.lang.String unsignedLongToString(long x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.- Parameters:
x
- the value to convert to a string.radix
- the radix to use while working withx
- Throws:
java.lang.IllegalArgumentException
- ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.
-
-