Class UnsignedLongs
- java.lang.Object
- 
- com.google.common.primitives.UnsignedLongs
 
- 
 @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
- Author:
- Louis Wasserman, Brian Milch, Colin Evans
 
- 
- 
Field SummaryFields Modifier and Type Field Description static longMAX_VALUE
 - 
Method SummaryAll 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.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.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 voidsort(long[] array)Sorts the array, treating its elements as unsigned 64-bit integers.static voidsort(long[] array, int fromIndex, int toIndex)Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned 64-bit integers.static voidsortDescending(long[] array)Sorts the elements ofarrayin descending order, interpreting them as unsigned 64-bit integers.static voidsortDescending(long[] array, int fromIndex, int toIndex)Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 64-bit integers.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_VALUEpublic static final long MAX_VALUE - See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
comparepublic static int compare(long a, long b) Compares the two specifiedlongvalues, treating them as unsigned values between0and2^64 - 1inclusive.Java 8 users: use Long.compareUnsigned(long, long)instead.- Parameters:
- a- the first unsigned- longto compare
- b- the second unsigned- longto compare
- Returns:
- a negative value if ais less thanb; a positive value ifais greater thanb; or zero if they are equal
 
 - 
minpublic static long min(long... array) Returns the least value present inarray, treating values as unsigned.- Parameters:
- array- a nonempty array of unsigned- longvalues
- 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- if- arrayis empty
 
 - 
maxpublic static long max(long... array) Returns the greatest value present inarray, treating values as unsigned.- Parameters:
- array- a nonempty array of unsigned- longvalues
- 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- if- arrayis empty
 
 - 
joinpublic 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 unsigned- longvalues, possibly empty
 
 - 
lexicographicalComparatorpublic 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[]).
 - 
sortpublic static void sort(long[] array) Sorts the array, treating its elements as unsigned 64-bit integers.- Since:
- 23.1
 
 - 
sortpublic static void sort(long[] array, int fromIndex, int toIndex) Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned 64-bit integers.- Since:
- 23.1
 
 - 
sortDescendingpublic static void sortDescending(long[] array) Sorts the elements ofarrayin descending order, interpreting them as unsigned 64-bit integers.- Since:
- 23.1
 
 - 
sortDescendingpublic static void sortDescending(long[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 64-bit integers.- Since:
- 23.1
 
 - 
dividepublic static long divide(long dividend, long divisor) Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.Java 8 users: use Long.divideUnsigned(long, long)instead.- Parameters:
- dividend- the dividend (numerator)
- divisor- the divisor (denominator)
- Throws:
- java.lang.ArithmeticException- if divisor is 0
 
 - 
remainderpublic static long remainder(long dividend, long divisor) Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.Java 8 users: use Long.remainderUnsigned(long, long)instead.- Parameters:
- dividend- the dividend (numerator)
- divisor- the divisor (denominator)
- Throws:
- java.lang.ArithmeticException- if divisor is 0
- Since:
- 11.0
 
 - 
parseUnsignedLong@CanIgnoreReturnValue public static long parseUnsignedLong(java.lang.String string) Returns the unsignedlongvalue represented by the given decimal string.Java 8 users: use Long.parseUnsignedLong(String)instead.- Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- longvalue
- java.lang.NullPointerException- if- stringis null (in contrast to- Long.parseLong(String))
 
 - 
parseUnsignedLong@CanIgnoreReturnValue public static long parseUnsignedLong(java.lang.String string, int radix) Returns the unsignedlongvalue represented by a string with the given radix.Java 8 users: use Long.parseUnsignedLong(String, int)instead.- Parameters:
- string- the string containing the unsigned- longrepresentation to be parsed.
- radix- the radix to use while parsing- string
- Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- longwith the given radix, or if- radixis not between- Character.MIN_RADIXand- Character.MAX_RADIX.
- java.lang.NullPointerException- if- stringis null (in contrast to- Long.parseLong(String))
 
 - 
decode@CanIgnoreReturnValue 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: - 0xHexDigits
- 0XHexDigits
- #HexDigits
- 0OctalDigits
 - Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- longvalue
- Since:
- 13.0
 
 - 
toStringpublic static java.lang.String toString(long x) Returns a string representation of x, where x is treated as unsigned.Java 8 users: use Long.toUnsignedString(long)instead.
 - 
toStringpublic static java.lang.String toString(long x, int radix) Returns a string representation ofxfor the given radix, wherexis treated as unsigned.Java 8 users: use Long.toUnsignedString(long, int)instead.- Parameters:
- x- the value to convert to a string.
- radix- the radix to use while working with- x
- Throws:
- java.lang.IllegalArgumentException- if- radixis not between- Character.MIN_RADIXand- Character.MAX_RADIX.
 
 
- 
 
-