javadoc.com.google.common.primitives.Longs.html Maven / Gradle / Ivy
Longs (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.primitives
Class Longs
java.lang.Object
com.google.common.primitives.Longs
@GwtCompatible(emulated=true)
public final class Longs
- extends Object
Static utility methods pertaining to long
primitives, that are not
already found in either Long
or Arrays
.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
Field Summary | |
---|---|
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 . |
Method Summary | |
---|---|
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 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 long[] |
toArray(Collection<Long> collection)
Copies a collection of Long instances into a new array of
primitive long values. |
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() . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
BYTES
public static final int BYTES
- The number of bytes required to represent a primitive
long
value.- See Also:
- Constant Field Values
MAX_POWER_OF_TWO
public static final long MAX_POWER_OF_TWO
- The largest power of two that can be represented as a
long
.- Since:
- 10.0
- See Also:
- Constant Field Values
Method Detail |
---|
hashCode
public static int hashCode(long value)
- Returns a hash code for
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 becauseLong.hashCode()
in GWT does not obey the JRE contract.- Parameters:
value
- a primitivelong
value- Returns:
- a hash code for the value
compare
public static int compare(long a, long b)
- Compares the two specified
long
values. The sign of the value returned is the same as that of((Long) a).compareTo(b)
.- Parameters:
a
- the firstlong
to compareb
- the secondlong
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
contains
public static boolean contains(long[] array, long target)
- Returns
true
iftarget
is present as an element anywhere inarray
.- Parameters:
array
- an array oflong
values, possibly emptytarget
- a primitivelong
value- Returns:
true
ifarray[i] == target
for some value ofi
indexOf
public static int indexOf(long[] array, long target)
- Returns the index of the first appearance of the value
target
inarray
.- Parameters:
array
- an array oflong
values, possibly emptytarget
- a primitivelong
value- Returns:
- the least index
i
for whicharray[i] == target
, or-1
if no such index exists.
indexOf
public static int indexOf(long[] array, long[] target)
- Returns the start position of the first occurrence of the specified
target
withinarray
, or-1
if there is no such occurrence.More formally, returns the lowest index
i
such thatjava.util.Arrays.copyOfRange(array, i, i + target.length)
contains exactly the same elements astarget
.- Parameters:
array
- the array to search for the sequencetarget
target
- the array to search for as a sub-sequence ofarray
lastIndexOf
public static int lastIndexOf(long[] array, long target)
- Returns the index of the last appearance of the value
target
inarray
.- Parameters:
array
- an array oflong
values, possibly emptytarget
- a primitivelong
value- Returns:
- the greatest index
i
for whicharray[i] == target
, or-1
if no such index exists.
min
public static long min(long... array)
- Returns the least value present in
array
.- Parameters:
array
- a nonempty array oflong
values- Returns:
- the value present in
array
that is less than or equal to every other value in the array - Throws:
IllegalArgumentException
- ifarray
is empty
max
public static long max(long... array)
- Returns the greatest value present in
array
.- Parameters:
array
- a nonempty array oflong
values- Returns:
- the value present in
array
that is greater than or equal to every other value in the array - Throws:
IllegalArgumentException
- ifarray
is empty
concat
public static long[] concat(long[]... arrays)
- Returns the values from each provided array combined into a single array.
For example,
concat(new long[] {a, b}, new long[] {}, new long[] {c}
returns the array{a, b, c}
.- Parameters:
arrays
- zero or morelong
arrays- Returns:
- a single array containing all the values from the source arrays, in order
toByteArray
@GwtIncompatible(value="doesn\'t work") public static byte[] toByteArray(long value)
- Returns a big-endian representation of
value
in an 8-element byte array; equivalent toByteBuffer.allocate(8).putLong(value).array()
. For example, the input value0x1213141516171819L
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 useByteStreams.newDataOutput()
to get a growable buffer.
fromByteArray
@GwtIncompatible(value="doesn\'t work") public static long fromByteArray(byte[] bytes)
- Returns the
long
value whose big-endian representation is stored in the first 8 bytes ofbytes
; equivalent toByteBuffer.wrap(bytes).getLong()
. For example, the input byte array{0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}
would yield thelong
value0x1213141516171819L
.Arguably, it's preferable to use
ByteBuffer
; that library exposes much more flexibility at little cost in readability.- Throws:
IllegalArgumentException
- ifbytes
has fewer than 8 elements
fromBytes
@GwtIncompatible(value="doesn\'t work") public 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 toLongs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})
.- Since:
- 7.0
ensureCapacity
public 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. Ifarray
already has a length of at leastminLength
, it is returned directly. Otherwise, a new array of sizeminLength + padding
is returned, containing the values ofarray
, and zeroes in the remaining places.- Parameters:
array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- an extra amount to "grow" the array by if growth is necessary- Returns:
- an array containing the values of
array
, with guaranteed minimum lengthminLength
- Throws:
IllegalArgumentException
- ifminLength
orpadding
is negative
join
public static String join(String separator, long... array)
- Returns a string containing the supplied
long
values separated byseparator
. For example,join("-", 1L, 2L, 3L)
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 oflong
values, possibly empty
lexicographicalComparator
public static Comparator<long[]> lexicographicalComparator()
- Returns a comparator that compares two
long
arrays 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]
.The returned comparator is inconsistent with
Object.equals(Object)
(since arrays support only identity equality), but it is consistent withArrays.equals(long[], long[])
.- Since:
- 2.0
- See Also:
- Lexicographical order article at Wikipedia
toArray
public static long[] toArray(Collection<Long> collection)
- Copies a collection of
Long
instances into a new array of primitivelong
values.Elements are copied from the argument collection as if by
collection.toArray()
. Calling this method is as thread-safe as calling that method.- Parameters:
collection
- a collection ofLong
objects- Returns:
- an array containing the same values as
collection
, in the same order, converted to primitives - Throws:
NullPointerException
- ifcollection
or any of its elements is null
asList
public static List<Long> asList(long... backingArray)
- Returns a fixed-size list backed by the specified array, similar to
Arrays.asList(Object[])
. The list supportsList.set(int, Object)
, but any attempt to set a value tonull
will result in aNullPointerException
.The returned list maintains the values, but not the identities, of
Long
objects written to or read from it. For example, whetherlist.get(0) == list.get(0)
is true for the returned list is unspecified.- Parameters:
backingArray
- the array to back the list- Returns:
- a list view of the array
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.