Package com.google.common.primitives
Class UnsignedLongs
- java.lang.Object
-
- com.google.common.primitives.UnsignedLongs
-
@Beta @GwtCompatible public final class UnsignedLongs extends java.lang.Object
Static utility methods pertaining tolongprimitives that interpret values as unsigned (that is, any negative valuexis treated as the positive value2^64 + x). The methods for which signedness is not an issue are inLongs, as well as signed versions of methods for which signedness is an issue.In addition, this class provides several static methods for converting a
longto aStringand aStringto alongthat treat thelongas an unsigned number.Users of these utilities must be extremely careful not to mix up signed and unsigned
longvalues. When possible, it is recommended that theUnsignedLongwrapper class be used, at a small efficiency penalty, to enforce the distinction in the type system.See the Guava User Guide article on unsigned primitive utilities.
- Since:
- 10.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classUnsignedLongs.LexicographicalComparator
-
Field Summary
Fields Modifier and Type Field Description static longMAX_VALUEprivate static int[]maxSafeDigitsprivate static long[]maxValueDivsprivate static int[]maxValueMods
-
Constructor Summary
Constructors Modifier Constructor Description privateUnsignedLongs()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcompare(long a, long b)Compares the two specifiedlongvalues, treating them as unsigned values between0and2^64 - 1inclusive.static longdecode(java.lang.String stringValue)Returns the unsignedlongvalue represented by the given string.static longdivide(long dividend, long divisor)Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.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.static java.lang.Stringjoin(java.lang.String separator, long... array)Returns a string containing the supplied unsignedlongvalues separated byseparator.static java.util.Comparator<long[]>lexicographicalComparator()Returns a comparator that compares two arrays of unsignedlongvalues lexicographically.static longmax(long... array)Returns the greatest value present inarray, treating values as unsigned.static longmin(long... array)Returns the least value present inarray, treating values as unsigned.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 longparseUnsignedLong(java.lang.String string)Returns the unsignedlongvalue represented by the given decimal string.static longparseUnsignedLong(java.lang.String string, int radix)Returns the unsignedlongvalue represented by a string with the given radix.static longremainder(long dividend, long divisor)Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.static java.lang.StringtoString(long x)Returns a string representation of x, where x is treated as unsigned.static java.lang.StringtoString(long x, int radix)Returns a string representation ofxfor the given radix, wherexis treated as unsigned.
-
-
-
Field Detail
-
MAX_VALUE
public static final long MAX_VALUE
- 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 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.
-
compare
public static int compare(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
-
min
public static long min(long... array)
Returns the least value present inarray, treating values as unsigned.- Parameters:
array- a nonempty array of unsignedlongvalues- Returns:
- the value present in
arraythat is less than or equal to every other value in the array according tocompare(long, long) - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
max
public static long max(long... array)
Returns the greatest value present inarray, treating values as unsigned.- Parameters:
array- a nonempty array of unsignedlongvalues- Returns:
- the value present in
arraythat is greater than or equal to every other value in the array according tocompare(long, long) - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
join
public static java.lang.String join(java.lang.String separator, long... array)Returns a string containing the supplied unsignedlongvalues separated byseparator. For example,join("-", 1, 2, 3)returns the string"1-2-3".- Parameters:
separator- the text that should appear between consecutive values in the resulting string (but not at the start or end)array- an array of unsignedlongvalues, possibly empty
-
lexicographicalComparator
public static java.util.Comparator<long[]> lexicographicalComparator()
Returns a comparator that compares two arrays of unsignedlongvalues lexicographically. That is, it compares, usingcompare(long, long)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example,[] < [1L] < [1L, 2L] < [2L] < [1L << 63].The returned comparator is inconsistent with
Object.equals(Object)(since arrays support only identity equality), but it is consistent withArrays.equals(long[], long[]).
-
divide
public 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
public 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 string)
Returns the unsignedlongvalue represented by the given decimal string.- Throws:
java.lang.NumberFormatException- if the string does not contain a valid unsignedlongvaluejava.lang.NullPointerException- ifstringis null (in contrast toLong.parseLong(String))
-
decode
public static long decode(java.lang.String stringValue)
Returns the unsignedlongvalue represented by the given string. Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:0xHexDigits0XHexDigits#HexDigits0OctalDigits
- Throws:
java.lang.NumberFormatException- if the string does not contain a valid unsignedlongvalue- Since:
- 13.0
-
parseUnsignedLong
public static long parseUnsignedLong(java.lang.String string, int radix)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 parsingstring- Throws:
java.lang.NumberFormatException- if the string does not contain a valid unsignedlongwith the given radix, or ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.java.lang.NullPointerException- ifstringis 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.
-
toString
public static java.lang.String toString(long x)
Returns a string representation of x, where x is treated as unsigned.
-
toString
public static java.lang.String toString(long x, int radix)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:
java.lang.IllegalArgumentException- ifradixis not betweenCharacter.MIN_RADIXandCharacter.MAX_RADIX.
-
-