Class UnsignedNumberUtil
java.lang.Object
io.protostuff.UnsignedNumberUtil
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final longstatic final longprivate static final int[]private static final long[]private static final int[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intcompareSigned(long a, long b) Compares the two specifiedlongvalues.private static intcompareUnsigned(long a, long b) Compares the two specifiedlongvalues, treating them as unsigned values between0and2^64 - 1inclusive.private static longdivide(long dividend, long divisor) Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.private static intflip(int value) private static longflip(long a) A (self-inverse) bijection which converts the ordering on unsigned longs to the ordering on longs, that is,a <= bas unsigned longs if and only ifflip(a) <= flip(b)as signed longs.private static booleanoverflowInParse(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 intReturns the unsignedintvalue represented by the given decimal string.private static intparseUnsignedInt(String string, int radix) Returns the unsignedintvalue represented by a string with the given radix.static longReturns the unsignedlongvalue represented by the given decimal string.private static longparseUnsignedLong(String s, int radix) Returns the unsignedlongvalue represented by a string with the given radix.private static longremainder(long dividend, long divisor) Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.private static longtoLong(int value) Returns the value of the givenintas along, when treated as unsigned.static StringunsignedIntToString(int x) Returns a string representation of x, where x is treated as unsigned.private static StringunsignedIntToString(int x, int radix) Returns a string representation ofxfor the given radix, wherexis treated as unsigned.static StringunsignedLongToString(long x) Returns a string representation of x, where x is treated as unsigned.private static StringunsignedLongToString(long x, int radix) Returns a string representation ofxfor the given radix, wherexis treated as unsigned.
-
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 <= bas unsigned longs if and only ifflip(a) <= flip(b)as signed longs. -
toLong
private static long toLong(int value) Returns the value of the givenintas along, when treated as unsigned. -
parseUnsignedInt
Returns the unsignedintvalue represented by the given decimal string.- Throws:
NumberFormatException- if the string does not contain a valid unsignedintvalueNullPointerException- ifsis null (in contrast toInteger.parseInt(String))
-
parseUnsignedInt
Returns the unsignedintvalue 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_RADIXandCharacter.MAX_RADIX.- Throws:
NumberFormatException- if the string does not contain a valid unsignedint, or if supplied radix is invalid.NullPointerException- ifsis null (in contrast toInteger.parseInt(String))
-
unsignedIntToString
Returns a string representation of x, where x is treated as unsigned. -
compareUnsigned
private static int compareUnsigned(long a, long b) Compares the two specifiedlongvalues, treating them as unsigned values between0and2^64 - 1inclusive.- Parameters:
a- the first unsignedlongto compareb- the second unsignedlongto compare- Returns:
- a negative value if
ais less thanb; a positive value ifais greater thanb; or zero if they are equal
-
compareSigned
private static int compareSigned(long a, long b) Compares the two specifiedlongvalues. 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 firstlongto compareb- the secondlongto compare- Returns:
- a negative value if
ais less thanb; a positive value ifais greater thanb; or zero if they are equal
-
unsignedIntToString
Returns a string representation ofxfor the given radix, wherexis treated as unsigned.- Parameters:
x- the value to convert to a string.radix- the radix to use while working withx- Throws:
IllegalArgumentException- ifradixis not betweenCharacter.MIN_RADIXandCharacter.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
Returns the unsignedlongvalue represented by the given decimal string.- Throws:
NumberFormatException- if the string does not contain a valid unsignedlongvalueNullPointerException- ifsis null (in contrast toLong.parseLong(String))
-
parseUnsignedLong
Returns the unsignedlongvalue represented by a string with the given radix.- Parameters:
s- the string containing the unsignedlongrepresentation to be parsed.radix- the radix to use while parsings- Throws:
NumberFormatException- if the string does not contain a valid unsignedlongwith the given radix, or ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.NullPointerException- ifsis 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
Returns a string representation of x, where x is treated as unsigned. -
unsignedLongToString
Returns a string representation ofxfor the given radix, wherexis treated as unsigned.- Parameters:
x- the value to convert to a string.radix- the radix to use while working withx- Throws:
IllegalArgumentException- ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.
-