@CheckReturnValue @GwtCompatible public final class SignedBytes extends Object
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.
Modifier and Type | Field and Description |
---|---|
static byte |
MAX_POWER_OF_TWO
The largest power of two that can be represented as a signed
byte . |
Modifier and Type | Method and Description |
---|---|
static byte |
checkedCast(long value)
Returns the
byte value that is equal to value , if possible. |
static int |
compare(byte a,
byte b)
Compares the two specified
byte values. |
static String |
join(String separator,
byte... array)
Returns a string containing the supplied
byte values separated by separator . |
static Comparator<byte[]> |
lexicographicalComparator()
Returns a comparator that compares two
byte arrays lexicographically. |
static byte |
max(byte... array)
Returns the greatest value present in
array . |
static byte |
min(byte... array)
Returns the least value present in
array . |
static byte |
saturatedCast(long value)
Returns the
byte nearest in value to value . |
public static final byte MAX_POWER_OF_TWO
The largest power of two that can be represented as a signed byte
.
public static byte checkedCast(long value)
Returns the byte
value that is equal to value
, if possible.
value
- any value in the range of the byte
typebyte
value that equals value
IllegalArgumentException
- if value
is greater than Byte.MAX_VALUE
or less than Byte.MIN_VALUE
public static byte saturatedCast(long value)
Returns the byte
nearest in value to value
.
value
- any long
valuebyte
if it is in the range of the byte
type, Byte.MAX_VALUE
if it is too large, or Byte.MIN_VALUE
if it is too smallpublic static int compare(byte a, byte b)
Compares the two specified byte
values. The sign of the value returned is the same as that of ((Byte) a).compareTo(b)
.
Note: this method behaves identically to the JDK 7 method Byte.compare(byte, byte)
.
a
- the first byte
to compareb
- the second byte
to comparea
is less than b
; a positive value if a
is greater than b
; or zero if they are equalpublic static byte min(byte... array)
Returns the least value present in array
.
array
- a nonempty array of byte
valuesarray
that is less than or equal to every other value in the arrayIllegalArgumentException
- if array
is emptypublic static byte max(byte... array)
Returns the greatest value present in array
.
array
- a nonempty array of byte
valuesarray
that is greater than or equal to every other value in the arrayIllegalArgumentException
- if array
is emptypublic static String join(String separator, byte... array)
Returns a string containing the supplied byte
values separated by separator
. For example, join(":", 0x01, 0x02, -0x01)
returns the string "1:2:-1"
.
separator
- the text that should appear between consecutive values in the resulting string (but not at the start or end)array
- an array of byte
values, possibly emptypublic static Comparator<byte[]> lexicographicalComparator()
Returns a comparator that compares two byte
arrays lexicographically. That is, it compares, using compare(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 with Arrays.equals(byte[], byte[])
.