Top Description Inners Fields Constructors Methods
com.google.common.primitives

public final Class SignedBytes

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

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.

Author
Kevin Bourrillion
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
public static final byte
MAX_POWER_OF_TWO

The largest power of two that can be represented as a signed byte.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static byte

Returns:

the byte value that equals value
checkedCast
(long
any value in the range of the byte type
value
)

Returns the byte value that is equal to value, if possible.

public static int

Returns:

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
compare
(byte
the first byte to compare
a
,
byte
the second byte to compare
b
)

Compares the two specified byte values.

public static String
join(String
the text that should appear between consecutive values in the resulting string (but not at the start or end)
separator
,
byte...
an array of byte values, possibly empty
array
)

Returns a string containing the supplied byte values separated by separator.

public static Comparator<byte[]>
lexicographicalComparator()

Returns a comparator that compares two byte arrays lexicographically.

public static byte

Returns:

the value present in array that is greater than or equal to every other value in the array
max
(byte...
a nonempty array of byte values
array
)

Returns the greatest value present in array.

public static byte

Returns:

the value present in array that is less than or equal to every other value in the array
min
(byte...
a nonempty array of byte values
array
)

Returns the least value present in array.

public static byte

Returns:

the same value cast to byte 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 small
saturatedCast
(long
any long value
value
)

Returns the byte nearest in value to value.

public static void
sortDescending(byte[] array)

Sorts the elements of array in descending order.

public static void
sortDescending(byte[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

MAX_POWER_OF_TWOback to summary
public static final byte MAX_POWER_OF_TWO

The largest power of two that can be represented as a signed byte.

Since
10.0

Constructor Detail

SignedBytesback to summary
private SignedBytes()

Method Detail

checkedCastback to summary
public static byte checkedCast(long value)

Returns the byte value that is equal to value, if possible.

Parameters
value:long

any value in the range of the byte type

Returns:byte

the byte value that equals value

Exceptions
IllegalArgumentException:
if value is greater than Byte#MAX_VALUE or less than Byte#MIN_VALUE
compareback to summary
public 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.

Parameters
a:byte

the first byte to compare

b:byte

the second byte to compare

Returns:int

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal

joinback to summary
public 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".

Parameters
separator:String

the text that should appear between consecutive values in the resulting string (but not at the start or end)

array:byte[]

an array of byte values, possibly empty

lexicographicalComparatorback to summary
public 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 java.util.Arrays#equals(byte[], byte[]).

Since
2.0
maxback to summary
public static byte max(byte... array)

Returns the greatest value present in array.

Parameters
array:byte[]

a nonempty array of byte values

Returns:byte

the value present in array that is greater than or equal to every other value in the array

Exceptions
IllegalArgumentException:
if array is empty
minback to summary
public static byte min(byte... array)

Returns the least value present in array.

Parameters
array:byte[]

a nonempty array of byte values

Returns:byte

the value present in array that is less than or equal to every other value in the array

Exceptions
IllegalArgumentException:
if array is empty
saturatedCastback to summary
public static byte saturatedCast(long value)

Returns the byte nearest in value to value.

Parameters
value:long

any long value

Returns:byte

the same value cast to byte 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 small

sortDescendingback to summary
public static void sortDescending(byte[] array)

Sorts the elements of array in descending order.

Since
23.1
sortDescendingback to summary
public static void sortDescending(byte[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.

Since
23.1
com.google.common.primitives back to summary

private final Enum SignedBytes.LexicographicalComparator

extends Enum<SignedBytes.LexicographicalComparator>
implements Comparator<byte[]>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Field Summary

Modifier and TypeField and Description
public static final SignedBytes.LexicographicalComparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(byte[]
the first object to be compared.
left
,
byte[]
the second object to be compared.
right
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static SignedBytes.LexicographicalComparator
public static SignedBytes.LexicographicalComparator[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

INSTANCEback to summary
public static final SignedBytes.LexicographicalComparator INSTANCE

Constructor Detail

LexicographicalComparatorback to summary
private LexicographicalComparator()

Method Detail

compareback to summary
public int compare(byte[] left, byte[] right)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
left:byte[]

the first object to be compared.

right:byte[]

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static SignedBytes.LexicographicalComparator valueOf(String name)
valuesback to summary
public static SignedBytes.LexicographicalComparator[] values()