Top Description Inners Fields Constructors Methods
com.google.common.primitives

public final Class UnsignedLongs

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

Static utility methods pertaining to long primitives that interpret values as unsigned (that is, any negative value x is treated as the positive value 2^64 + x). The methods for which signedness is not an issue are in Longs, as well as signed versions of methods for which signedness is an issue.

In addition, this class provides several static methods for converting a long to a String and a String to a long that treat the long as an unsigned number.

Users of these utilities must be extremely careful not to mix up signed and unsigned long values. When possible, it is recommended that the UnsignedLong wrapper 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.

Authors
Louis Wasserman, Brian Milch, Colin Evans
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
private static class

Field Summary

Modifier and TypeField and Description
public static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int

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
compare
(long
the first unsigned long to compare
a
,
long
the second unsigned long to compare
b
)

Compares the two specified long values, treating them as unsigned values between 0 and 2^64 - 1 inclusive.

public static long
decode(String stringValue)

Returns the unsigned long value represented by the given string.

public static long
divide(long
the dividend (numerator)
dividend
,
long
the divisor (denominator)
divisor
)

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.

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.

public static String
join(String
the text that should appear between consecutive values in the resulting string (but not at the start or end)
separator
,
long...
an array of unsigned long values, possibly empty
array
)

Returns a string containing the supplied unsigned long values separated by separator.

public static Comparator<long[]>
lexicographicalComparator()

Returns a comparator that compares two arrays of unsigned long values lexicographically.

public static long

Returns:

the value present in array that is greater than or equal to every other value in the array according to compare
max
(long...
a nonempty array of unsigned long values
array
)

Returns the greatest value present in array, treating values as unsigned.

public static long

Returns:

the value present in array that is less than or equal to every other value in the array according to compare
min
(long...
a nonempty array of unsigned long values
array
)

Returns the least value present in array, treating values as unsigned.

public static long
parseUnsignedLong(String string)

Returns the unsigned long value represented by the given decimal string.

public static long
parseUnsignedLong(String
the string containing the unsigned long representation to be parsed.
string
,
int
the radix to use while parsing string
radix
)

Returns the unsigned long value represented by a string with the given radix.

public static long
remainder(long
the dividend (numerator)
dividend
,
long
the divisor (denominator)
divisor
)

Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.

public static void
sort(long[] array)

Sorts the array, treating its elements as unsigned 64-bit integers.

public static void
sort(long[] array, int fromIndex, int toIndex)

Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 64-bit integers.

public static void
sortDescending(long[] array)

Sorts the elements of array in descending order, interpreting them as unsigned 64-bit integers.

public static void
sortDescending(long[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 64-bit integers.

public static String
toString(long x)

Returns a string representation of x, where x is treated as unsigned.

public static String
toString(long
the value to convert to a string.
x
,
int
the radix to use while working with x
radix
)

Returns a string representation of x for the given radix, where x is treated as unsigned.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MAX_VALUEback to summary
public static final long MAX_VALUE

Constructor Detail

UnsignedLongsback to summary
private UnsignedLongs()

Method Detail

compareback to summary
public static int compare(long a, long b)

Compares the two specified long values, treating them as unsigned values between 0 and 2^64 - 1 inclusive.

Java 8 users: use Long#compareUnsigned(long, long) instead.

Parameters
a:long

the first unsigned long to compare

b:long

the second unsigned long to compare

Returns:int

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal

decodeback to summary
public static long decode(String stringValue)

Returns the unsigned long value represented by the given string.

Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:

  • 0xHexDigits
  • 0XHexDigits
  • #HexDigits
  • 0OctalDigits
Annotations
@CanIgnoreReturnValue
Exceptions
NumberFormatException:
if the string does not contain a valid unsigned long value
Since
13.0
divideback to summary
public 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:long

the dividend (numerator)

divisor:long

the divisor (denominator)

Exceptions
ArithmeticException:
if divisor is 0
flipback to summary
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.

joinback to summary
public static String join(String separator, long... array)

Returns a string containing the supplied unsigned long values separated by separator. For example, join("-", 1, 2, 3) returns the string "1-2-3".

Parameters
separator:String

the text that should appear between consecutive values in the resulting string (but not at the start or end)

array:long[]

an array of unsigned long values, possibly empty

lexicographicalComparatorback to summary
public static Comparator<long[]> lexicographicalComparator()

Returns a comparator that compares two arrays of unsigned long values lexicographically. That is, it compares, using compare(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 with Arrays#equals(long[], long[]).

maxback to summary
public static long max(long... array)

Returns the greatest value present in array, treating values as unsigned.

Parameters
array:long[]

a nonempty array of unsigned long values

Returns:long

the value present in array that is greater than or equal to every other value in the array according to compare

Exceptions
IllegalArgumentException:
if array is empty
minback to summary
public static long min(long... array)

Returns the least value present in array, treating values as unsigned.

Parameters
array:long[]

a nonempty array of unsigned long values

Returns:long

the value present in array that is less than or equal to every other value in the array according to compare

Exceptions
IllegalArgumentException:
if array is empty
parseUnsignedLongback to summary
public static long parseUnsignedLong(String string)

Returns the unsigned long value represented by the given decimal string.

Java 8 users: use Long#parseUnsignedLong(String) instead.

Annotations
@CanIgnoreReturnValue
Exceptions
NumberFormatException:
if the string does not contain a valid unsigned long value
NullPointerException:
if string is null (in contrast to Long#parseLong(String))
parseUnsignedLongback to summary
public static long parseUnsignedLong(String string, int radix)

Returns the unsigned long value represented by a string with the given radix.

Java 8 users: use Long#parseUnsignedLong(String, int) instead.

Parameters
string:String

the string containing the unsigned long representation to be parsed.

radix:int

the radix to use while parsing string

Annotations
@CanIgnoreReturnValue
Exceptions
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 string is null (in contrast to Long#parseLong(String))
remainderback to summary
public 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:long

the dividend (numerator)

divisor:long

the divisor (denominator)

Exceptions
ArithmeticException:
if divisor is 0
Since
11.0
sortback to summary
public static void sort(long[] array)

Sorts the array, treating its elements as unsigned 64-bit integers.

Since
23.1
sortback to summary
public static void sort(long[] array, int fromIndex, int toIndex)

Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 64-bit integers.

Since
23.1
sortDescendingback to summary
public static void sortDescending(long[] array)

Sorts the elements of array in descending order, interpreting them as unsigned 64-bit integers.

Since
23.1
sortDescendingback to summary
public static void sortDescending(long[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 64-bit integers.

Since
23.1
toStringback to summary
public static String toString(long x)

Returns a string representation of x, where x is treated as unsigned.

Java 8 users: use Long#toUnsignedString(long) instead.

toStringback to summary
public static String toString(long x, int radix)

Returns a string representation of x for the given radix, where x is treated as unsigned.

Java 8 users: use Long#toUnsignedString(long, int) instead.

Parameters
x:long

the value to convert to a string.

radix:int

the radix to use while working with x

Exceptions
IllegalArgumentException:
if radix is not between Character#MIN_RADIX and Character#MAX_RADIX.
com.google.common.primitives back to summary

pack-priv final Enum UnsignedLongs.LexicographicalComparator

extends Enum<UnsignedLongs.LexicographicalComparator>
implements Comparator<long[]>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Field Summary

Modifier and TypeField and Description
public static final UnsignedLongs.LexicographicalComparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(long[]
the first object to be compared.
left
,
long[]
the second object to be compared.
right
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static UnsignedLongs.LexicographicalComparator
public static UnsignedLongs.LexicographicalComparator[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

INSTANCEback to summary
public static final UnsignedLongs.LexicographicalComparator INSTANCE

Constructor Detail

LexicographicalComparatorback to summary
private LexicographicalComparator()

Method Detail

compareback to summary
public int compare(long[] left, long[] right)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
left:long[]

the first object to be compared.

right:long[]

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static UnsignedLongs.LexicographicalComparator valueOf(String name)
valuesback to summary
public static UnsignedLongs.LexicographicalComparator[] values()
com.google.common.primitives back to summary

private final Class UnsignedLongs.ParseOverflowDetection

extends Object
Class Inheritance
  • java.lang.Object
  • com.google.common.primitives.UnsignedLongs.ParseOverflowDetection

Field Summary

Modifier and TypeField and Description
pack-priv static final int[]
pack-priv static final long[]
pack-priv static final int[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxSafeDigitsback to summary
pack-priv static final int[] maxSafeDigits
maxValueDivsback to summary
pack-priv static final long[] maxValueDivs
maxValueModsback to summary
pack-priv static final int[] maxValueMods

Constructor Detail

ParseOverflowDetectionback to summary
private ParseOverflowDetection()

Method Detail

overflowInParseback to summary
pack-priv 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.