Package com.google.common.primitives
Class SignedBytes
- java.lang.Object
-
- com.google.common.primitives.SignedBytes
-
@GwtCompatible public final class SignedBytes extends java.lang.Object
Static utility methods pertaining tobyteprimitives that interpret values as signed. The corresponding methods that treat the values as unsigned are found inUnsignedBytes, and the methods for which signedness is not an issue are inBytes.See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
-
-
Field Summary
Fields Modifier and Type Field Description static byteMAX_POWER_OF_TWOThe largest power of two that can be represented as a signedbyte.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static bytecheckedCast(long value)Returns thebytevalue that is equal tovalue, if possible.static intcompare(byte a, byte b)Compares the two specifiedbytevalues.static java.lang.Stringjoin(java.lang.String separator, byte... array)Returns a string containing the suppliedbytevalues separated byseparator.static java.util.Comparator<byte[]>lexicographicalComparator()Returns a comparator that compares twobytearrays lexicographically.static bytemax(byte... array)Returns the greatest value present inarray.static bytemin(byte... array)Returns the least value present inarray.static bytesaturatedCast(long value)Returns thebytenearest in value tovalue.static voidsortDescending(byte[] array)Sorts the elements ofarrayin descending order.static voidsortDescending(byte[] array, int fromIndex, int toIndex)Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.
-
-
-
Field Detail
-
MAX_POWER_OF_TWO
public static final byte MAX_POWER_OF_TWO
The largest power of two that can be represented as a signedbyte.- Since:
- 10.0
- See Also:
- Constant Field Values
-
-
Method Detail
-
checkedCast
public static byte checkedCast(long value)
Returns thebytevalue that is equal tovalue, if possible.- Parameters:
value- any value in the range of thebytetype- Returns:
- the
bytevalue that equalsvalue - Throws:
java.lang.IllegalArgumentException- ifvalueis greater thanByte.MAX_VALUEor less thanByte.MIN_VALUE
-
saturatedCast
public static byte saturatedCast(long value)
Returns thebytenearest in value tovalue.- Parameters:
value- anylongvalue- Returns:
- the same value cast to
byteif it is in the range of thebytetype,Byte.MAX_VALUEif it is too large, orByte.MIN_VALUEif it is too small
-
compare
public static int compare(byte a, byte b)
Compares the two specifiedbytevalues. 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).- Parameters:
a- the firstbyteto compareb- the secondbyteto compare- Returns:
- a negative value if
ais less thanb; a positive value ifais greater thanb; or zero if they are equal
-
min
public static byte min(byte... array)
Returns the least value present inarray.- Parameters:
array- a nonempty array ofbytevalues- Returns:
- the value present in
arraythat is less than or equal to every other value in the array - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
max
public static byte max(byte... array)
Returns the greatest value present inarray.- Parameters:
array- a nonempty array ofbytevalues- Returns:
- the value present in
arraythat is greater than or equal to every other value in the array - Throws:
java.lang.IllegalArgumentException- ifarrayis empty
-
join
public static java.lang.String join(java.lang.String separator, byte... array)
Returns a string containing the suppliedbytevalues 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 ofbytevalues, possibly empty
-
lexicographicalComparator
public static java.util.Comparator<byte[]> lexicographicalComparator()
Returns a comparator that compares twobytearrays 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
public static void sortDescending(byte[] array)
Sorts the elements ofarrayin descending order.- Since:
- 23.1
-
sortDescending
public static void sortDescending(byte[] array, int fromIndex, int toIndex)
Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.- Since:
- 23.1
-
-