Class SignedBytes
java.lang.Object
com.google.common.primitives.SignedBytes
Static utility methods pertaining to
byte
primitives that interpret values as signed. The
corresponding methods that treat the values as unsigned are found in UnsignedBytes
, and
the methods for which signedness is not an issue are in Bytes
.
See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
-
Field Summary
Modifier and TypeFieldDescriptionstatic final byte
The largest power of two that can be represented as a signedbyte
. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte
checkedCast
(long value) Returns thebyte
value that is equal tovalue
, if possible.static int
compare
(byte a, byte b) Compares the two specifiedbyte
values.static String
Returns a string containing the suppliedbyte
values separated byseparator
.static Comparator
<byte[]> Returns a comparator that compares twobyte
arrays lexicographically.static byte
max
(byte... array) Returns the greatest value present inarray
.static byte
min
(byte... array) Returns the least value present inarray
.static byte
saturatedCast
(long value) Returns thebyte
nearest in value tovalue
.static void
sortDescending
(byte[] array) Sorts the elements ofarray
in descending order.static void
sortDescending
(byte[] array, int fromIndex, int toIndex) Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order.
-
Field Details
-
MAX_POWER_OF_TWO
The largest power of two that can be represented as a signedbyte
.- Since:
- 10.0
- See Also:
-
-
Method Details
-
checkedCast
Returns thebyte
value that is equal tovalue
, if possible.- Parameters:
value
- any value in the range of thebyte
type- Returns:
- the
byte
value that equalsvalue
- Throws:
IllegalArgumentException
- ifvalue
is greater thanByte.MAX_VALUE
or less thanByte.MIN_VALUE
-
saturatedCast
Returns thebyte
nearest in value tovalue
.- Parameters:
value
- anylong
value- Returns:
- the same value cast to
byte
if it is in the range of thebyte
type,Byte.MAX_VALUE
if it is too large, orByte.MIN_VALUE
if it is too small
-
compare
Compares the two specifiedbyte
values. The sign of the value returned is the same as that of((Byte) a).compareTo(b)
.Note: this method behaves identically to
Byte.compare(byte, byte)
.- Parameters:
a
- the firstbyte
to compareb
- the secondbyte
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
-
min
Returns the least value present inarray
.- Parameters:
array
- a nonempty array ofbyte
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
Returns the greatest value present inarray
.- Parameters:
array
- a nonempty array ofbyte
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
-
join
Returns a string containing the suppliedbyte
values separated byseparator
. For example,join(":", 0x01, 0x02, -0x01)
returns the string"1:2:-1"
.- Parameters:
separator
- the text that should appear between consecutive values in the resulting string (but not at the start or end)array
- an array ofbyte
values, possibly empty
-
lexicographicalComparator
Returns a comparator that compares twobyte
arrays lexicographically. That is, it compares, usingcompare(byte, byte)
), 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,[] < [0x01] < [0x01, 0x80] < [0x01, 0x7F] < [0x02]
. Values are treated as signed.The returned comparator is inconsistent with
Object.equals(Object)
(since arrays support only identity equality), but it is consistent withArrays.equals(byte[], byte[])
.- Since:
- 2.0
-
sortDescending
Sorts the elements ofarray
in descending order.- Since:
- 23.1
-
sortDescending
Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order.- Since:
- 23.1
-