javadoc.com.google.common.primitives.Shorts.html Maven / Gradle / Ivy
Shorts (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 Shorts
java.lang.Object
com.google.common.primitives.Shorts
@GwtCompatible(emulated=true)
public final class Shorts
- extends Object
Static utility methods pertaining to short
primitives, that are not
already found in either Short
or Arrays
.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
Field Summary | |
---|---|
static int |
BYTES
The number of bytes required to represent a primitive short
value. |
static short |
MAX_POWER_OF_TWO
The largest power of two that can be represented as a short . |
Method Summary | |
---|---|
static List<Short> |
asList(short... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]) . |
static short |
checkedCast(long value)
Returns the short value that is equal to value , if
possible. |
static int |
compare(short a,
short b)
Compares the two specified short values. |
static short[] |
concat(short[]... arrays)
Returns the values from each provided array combined into a single array. |
static boolean |
contains(short[] array,
short target)
Returns true if target is present as an element anywhere in
array . |
static short[] |
ensureCapacity(short[] array,
int minLength,
int padding)
Returns an array containing the same values as array , but
guaranteed to be of a specified minimum length. |
static short |
fromByteArray(byte[] bytes)
Returns the short value whose big-endian representation is
stored in the first 2 bytes of bytes ; equivalent to ByteBuffer.wrap(bytes).getShort() . |
static short |
fromBytes(byte b1,
byte b2)
Returns the short value whose byte representation is the given 2
bytes, in big-endian order; equivalent to Shorts.fromByteArray(new
byte[] {b1, b2}) . |
static int |
hashCode(short value)
Returns a hash code for value ; equal to the result of invoking
((Short) value).hashCode() . |
static int |
indexOf(short[] array,
short target)
Returns the index of the first appearance of the value target in
array . |
static int |
indexOf(short[] array,
short[] 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,
short... array)
Returns a string containing the supplied short values separated
by separator . |
static int |
lastIndexOf(short[] array,
short target)
Returns the index of the last appearance of the value target in
array . |
static Comparator<short[]> |
lexicographicalComparator()
Returns a comparator that compares two short arrays
lexicographically. |
static short |
max(short... array)
Returns the greatest value present in array . |
static short |
min(short... array)
Returns the least value present in array . |
static short |
saturatedCast(long value)
Returns the short nearest in value to value . |
static short[] |
toArray(Collection<Short> collection)
Copies a collection of Short instances into a new array of
primitive short values. |
static byte[] |
toByteArray(short value)
Returns a big-endian representation of value in a 2-element byte
array; equivalent to ByteBuffer.allocate(2).putShort(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
short
value.- See Also:
- Constant Field Values
MAX_POWER_OF_TWO
public static final short MAX_POWER_OF_TWO
- The largest power of two that can be represented as a
short
.- Since:
- 10.0
- See Also:
- Constant Field Values
Method Detail |
---|
hashCode
public static int hashCode(short value)
- Returns a hash code for
value
; equal to the result of invoking((Short) value).hashCode()
.- Parameters:
value
- a primitiveshort
value- Returns:
- a hash code for the value
checkedCast
public static short checkedCast(long value)
- Returns the
short
value that is equal tovalue
, if possible.- Parameters:
value
- any value in the range of theshort
type- Returns:
- the
short
value that equalsvalue
- Throws:
IllegalArgumentException
- ifvalue
is greater thanShort.MAX_VALUE
or less thanShort.MIN_VALUE
saturatedCast
public static short saturatedCast(long value)
- Returns the
short
nearest in value tovalue
.- Parameters:
value
- anylong
value- Returns:
- the same value cast to
short
if it is in the range of theshort
type,Short.MAX_VALUE
if it is too large, orShort.MIN_VALUE
if it is too small
compare
public static int compare(short a, short b)
- Compares the two specified
short
values. The sign of the value returned is the same as that of((Short) a).compareTo(b)
.- Parameters:
a
- the firstshort
to compareb
- the secondshort
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(short[] array, short target)
- Returns
true
iftarget
is present as an element anywhere inarray
.- Parameters:
array
- an array ofshort
values, possibly emptytarget
- a primitiveshort
value- Returns:
true
ifarray[i] == target
for some value ofi
indexOf
public static int indexOf(short[] array, short target)
- Returns the index of the first appearance of the value
target
inarray
.- Parameters:
array
- an array ofshort
values, possibly emptytarget
- a primitiveshort
value- Returns:
- the least index
i
for whicharray[i] == target
, or-1
if no such index exists.
indexOf
public static int indexOf(short[] array, short[] 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(short[] array, short target)
- Returns the index of the last appearance of the value
target
inarray
.- Parameters:
array
- an array ofshort
values, possibly emptytarget
- a primitiveshort
value- Returns:
- the greatest index
i
for whicharray[i] == target
, or-1
if no such index exists.
min
public static short min(short... array)
- Returns the least value present in
array
.- Parameters:
array
- a nonempty array ofshort
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 short max(short... array)
- Returns the greatest value present in
array
.- Parameters:
array
- a nonempty array ofshort
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 short[] concat(short[]... arrays)
- Returns the values from each provided array combined into a single array.
For example,
concat(new short[] {a, b}, new short[] {}, new short[] {c}
returns the array{a, b, c}
.- Parameters:
arrays
- zero or moreshort
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(short value)
- Returns a big-endian representation of
value
in a 2-element byte array; equivalent toByteBuffer.allocate(2).putShort(value).array()
. For example, the input value(short) 0x1234
would yield the byte array{0x12, 0x34}
.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 short fromByteArray(byte[] bytes)
- Returns the
short
value whose big-endian representation is stored in the first 2 bytes ofbytes
; equivalent toByteBuffer.wrap(bytes).getShort()
. For example, the input byte array{0x54, 0x32}
would yield theshort
value0x5432
.Arguably, it's preferable to use
ByteBuffer
; that library exposes much more flexibility at little cost in readability.- Throws:
IllegalArgumentException
- ifbytes
has fewer than 2 elements
fromBytes
@GwtIncompatible(value="doesn\'t work") public static short fromBytes(byte b1, byte b2)
- Returns the
short
value whose byte representation is the given 2 bytes, in big-endian order; equivalent toShorts.fromByteArray(new byte[] {b1, b2})
.- Since:
- 7.0
ensureCapacity
public static short[] ensureCapacity(short[] 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, short... array)
- Returns a string containing the supplied
short
values separated byseparator
. For example,join("-", (short) 1, (short) 2, (short) 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 ofshort
values, possibly empty
lexicographicalComparator
public static Comparator<short[]> lexicographicalComparator()
- Returns a comparator that compares two
short
arrays lexicographically. That is, it compares, usingcompare(short, short)
), 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,[] < [(short) 1] < [(short) 1, (short) 2] < [(short) 2]
.The returned comparator is inconsistent with
Object.equals(Object)
(since arrays support only identity equality), but it is consistent withArrays.equals(short[], short[])
.- Since:
- 2.0
- See Also:
- Lexicographical order article at Wikipedia
toArray
public static short[] toArray(Collection<Short> collection)
- Copies a collection of
Short
instances into a new array of primitiveshort
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 ofShort
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<Short> asList(short... 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
Short
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.