@GwtCompatible public final class Longs extends Object
long
primitives, that are not already found in
either Long
or Arrays
.
See the Guava User Guide article on primitive utilities.
Modifier and Type | Field and Description |
---|---|
static int |
BYTES
The number of bytes required to represent a primitive
long value. |
static long |
MAX_POWER_OF_TWO
The largest power of two that can be represented as a
long . |
Modifier and Type | Method and Description |
---|---|
static List<Long> |
asList(long... backingArray)
Returns a fixed-size list backed by the specified array, similar to
Arrays.asList(Object[]) . |
static int |
compare(long a,
long b)
Compares the two specified
long values. |
static long[] |
concat(long[]... arrays)
Returns the values from each provided array combined into a single array.
|
static long |
constrainToRange(long value,
long min,
long max)
Returns the value nearest to
value which is within the closed range [min..max] . |
static boolean |
contains(long[] array,
long target)
Returns
true if target is present as an element anywhere in array . |
static long[] |
ensureCapacity(long[] array,
int minLength,
int padding)
Returns an array containing the same values as
array , but guaranteed to be of a
specified minimum length. |
static long |
fromByteArray(byte[] bytes)
Returns the
long value whose big-endian representation is stored in the first 8 bytes
of bytes ; equivalent to ByteBuffer.wrap(bytes).getLong() . |
static long |
fromBytes(byte b1,
byte b2,
byte b3,
byte b4,
byte b5,
byte b6,
byte b7,
byte b8)
Returns the
long value whose byte representation is the given 8 bytes, in big-endian
order; equivalent to Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8}) . |
static int |
hashCode(long value)
Returns a hash code for
value ; equal to the result of invoking ((Long)
value).hashCode() . |
static int |
indexOf(long[] array,
long target)
Returns the index of the first appearance of the value
target in array . |
static int |
indexOf(long[] array,
long[] target)
Returns the start position of the first occurrence of the specified
target within
array , or -1 if there is no such occurrence. |
static String |
join(String separator,
long... array)
Returns a string containing the supplied
long values separated by separator . |
static int |
lastIndexOf(long[] array,
long target)
Returns the index of the last appearance of the value
target in array . |
static Comparator<long[]> |
lexicographicalComparator()
Returns a comparator that compares two
long arrays lexicographically. |
static long |
max(long... array)
Returns the greatest value present in
array . |
static long |
min(long... array)
Returns the least value present in
array . |
static void |
reverse(long[] array)
Reverses the elements of
array . |
static void |
reverse(long[] array,
int fromIndex,
int toIndex)
Reverses the elements of
array between fromIndex inclusive and toIndex
exclusive. |
static void |
sortDescending(long[] array)
Sorts the elements of
array in descending order. |
static void |
sortDescending(long[] array,
int fromIndex,
int toIndex)
Sorts the elements of
array between fromIndex inclusive and toIndex
exclusive in descending order. |
static Converter<String,Long> |
stringConverter()
Returns a serializable converter object that converts between strings and longs using
Long.decode(java.lang.String) and Long.toString() . |
static long[] |
toArray(Collection<? extends Number> collection)
Returns an array containing each value of
collection , converted to a long value
in the manner of Number.longValue() . |
static byte[] |
toByteArray(long value)
Returns a big-endian representation of
value in an 8-element byte array; equivalent to
ByteBuffer.allocate(8).putLong(value).array() . |
static @Nullable Long |
tryParse(String string)
Parses the specified string as a signed decimal long value.
|
static @Nullable Long |
tryParse(String string,
int radix)
Parses the specified string as a signed long value using the specified radix.
|
public static final int BYTES
long
value.
Java 8 users: use Long.BYTES
instead.
public static final long MAX_POWER_OF_TWO
long
.public static int hashCode(long value)
value
; equal to the result of invoking ((Long)
value).hashCode()
.
This method always return the value specified by Long.hashCode()
in java, which
might be different from ((Long) value).hashCode()
in GWT because Long.hashCode()
in GWT does not obey the JRE contract.
Java 8 users: use Long.hashCode(long)
instead.
value
- a primitive long
valuepublic static int compare(long a, long b)
long
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.
a
- the first long
to compareb
- the second long
to comparea
is less than b
; a positive value if a
is
greater than b
; or zero if they are equalpublic static boolean contains(long[] array, long target)
true
if target
is present as an element anywhere in array
.array
- an array of long
values, possibly emptytarget
- a primitive long
valuetrue
if array[i] == target
for some value of i
public static int indexOf(long[] array, long target)
target
in array
.array
- an array of long
values, possibly emptytarget
- a primitive long
valuei
for which array[i] == target
, or -1
if no
such index exists.public static int indexOf(long[] array, long[] target)
target
within
array
, or -1
if there is no such occurrence.
More formally, returns the lowest index i
such that Arrays.copyOfRange(array,
i, i + target.length)
contains exactly the same elements as target
.
array
- the array to search for the sequence target
target
- the array to search for as a sub-sequence of array
public static int lastIndexOf(long[] array, long target)
target
in array
.array
- an array of long
values, possibly emptytarget
- a primitive long
valuei
for which array[i] == target
, or -1
if no
such index exists.public static long min(long... array)
array
.array
- a nonempty array of long
valuesarray
that is less than or equal to every other value in
the arrayIllegalArgumentException
- if array
is emptypublic static long max(long... array)
array
.array
- a nonempty array of long
valuesarray
that is greater than or equal to every other value
in the arrayIllegalArgumentException
- if array
is empty@Beta public static long constrainToRange(long value, long min, long max)
value
which is within the closed range [min..max]
.
If value
is within the range [min..max]
, value
is returned
unchanged. If value
is less than min
, min
is returned, and if value
is greater than max
, max
is returned.
value
- the long
value to constrainmin
- the lower bound (inclusive) of the range to constrain value
tomax
- the upper bound (inclusive) of the range to constrain value
toIllegalArgumentException
- if min > max
public static long[] concat(long[]... arrays)
concat(new long[] {a, b}, new long[] {}, new long[] {c}
returns the array {a, b, c}
.arrays
- zero or more long
arrayspublic static byte[] toByteArray(long value)
value
in an 8-element byte array; equivalent to
ByteBuffer.allocate(8).putLong(value).array()
. For example, the input value 0x1213141516171819L
would yield the byte array {0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19}
.
If you need to convert and concatenate several values (possibly even of different types),
use a shared ByteBuffer
instance, or use ByteStreams.newDataOutput()
to get a growable buffer.
public static long fromByteArray(byte[] bytes)
long
value whose big-endian representation is stored in the first 8 bytes
of bytes
; equivalent to ByteBuffer.wrap(bytes).getLong()
. For example, the
input byte array {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}
would yield the
long
value 0x1213141516171819L
.
Arguably, it's preferable to use ByteBuffer
; that library exposes much more
flexibility at little cost in readability.
IllegalArgumentException
- if bytes
has fewer than 8 elementspublic static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8)
long
value whose byte representation is the given 8 bytes, in big-endian
order; equivalent to Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})
.@Beta public static @Nullable Long tryParse(String string)
'-'
(
'\u002D'
) is recognized as the minus sign.
Unlike Long.parseLong(String)
, this method returns null
instead of throwing
an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns
null
if non-ASCII digits are present in the string.
Note that strings prefixed with ASCII '+'
are rejected, even under JDK 7, despite
the change to Long.parseLong(String)
for that version.
string
- the string representation of a long valuestring
, or null
if string
has a
length of zero or cannot be parsed as a long value@Beta public static @Nullable Long tryParse(String string, int radix)
'-'
('\u002D'
) is recognized as the minus sign.
Unlike Long.parseLong(String, int)
, this method returns null
instead of
throwing an exception if parsing fails. Additionally, this method only accepts ASCII digits,
and returns null
if non-ASCII digits are present in the string.
Note that strings prefixed with ASCII '+'
are rejected, even under JDK 7, despite
the change to Long.parseLong(String, int)
for that version.
string
- the string representation of an long valueradix
- the radix to use when parsingstring
using radix
, or null
if
string
has a length of zero or cannot be parsed as a long valueIllegalArgumentException
- if radix < Character.MIN_RADIX
or radix >
Character.MAX_RADIX
@Beta public static Converter<String,Long> stringConverter()
Long.decode(java.lang.String)
and Long.toString()
. The returned converter throws NumberFormatException
if the input string is invalid.
Warning: please see Long.decode(java.lang.String)
to understand exactly how strings are parsed.
For example, the string "0123"
is treated as octal and converted to the value
83L
.
public static long[] ensureCapacity(long[] array, int minLength, int padding)
array
, but guaranteed to be of a
specified minimum length. If array
already has a length of at least minLength
,
it is returned directly. Otherwise, a new array of size minLength + padding
is
returned, containing the values of array
, and zeroes in the remaining places.array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- an extra amount to "grow" the array by if growth is necessaryarray
, with guaranteed minimum length minLength
IllegalArgumentException
- if minLength
or padding
is negativepublic static String join(String separator, long... array)
long
values separated by separator
.
For example, join("-", 1L, 2L, 3L)
returns the string "1-2-3"
.separator
- the text that should appear between consecutive values in the resulting string
(but not at the start or end)array
- an array of long
values, possibly emptypublic static Comparator<long[]> lexicographicalComparator()
long
arrays 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]
.
The returned comparator is inconsistent with Object.equals(Object)
(since arrays
support only identity equality), but it is consistent with Arrays.equals(long[],
long[])
.
public static void sortDescending(long[] array)
array
in descending order.public static void sortDescending(long[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive in descending order.public static void reverse(long[] array)
array
. This is equivalent to Collections.reverse(Longs.asList(array))
, but is likely to be more efficient.public static void reverse(long[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive. This is equivalent to Collections.reverse(Longs.asList(array).subList(fromIndex, toIndex))
, but is likely to be more
efficient.IndexOutOfBoundsException
- if fromIndex < 0
, toIndex > array.length
, or
toIndex > fromIndex
public static long[] toArray(Collection<? extends Number> collection)
collection
, converted to a long
value
in the manner of Number.longValue()
.
Elements are copied from the argument collection as if by collection.toArray()
.
Calling this method is as thread-safe as calling that method.
collection
- a collection of Number
instancescollection
, in the same order, converted
to primitivesNullPointerException
- if collection
or any of its elements is nullCollection<Long>
before 12.0)public static List<Long> asList(long... backingArray)
Arrays.asList(Object[])
. The list supports List.set(int, Object)
, but any attempt to
set a value to null
will result in a NullPointerException
.
The returned list maintains the values, but not the identities, of Long
objects
written to or read from it. For example, whether list.get(0) == list.get(0)
is true for
the returned list is unspecified.
Note: when possible, you should represent your data as an ImmutableLongArray
instead, which has an asList
view.
backingArray
- the array to back the listCopyright © 2010–2019. All rights reserved.