@GwtCompatible public final class Booleans extends Object
boolean primitives, that are not already found in either
Boolean or
Arrays.
See the Guava User Guide article on primitive utilities.
| Modifier and Type | Method and Description |
|---|---|
static List |
asList(boolean... backingArray)
Returns a fixed-size list backed by the specified array, similar to
Arrays.
|
static int |
compare(boolean a, boolean b)
Compares the two specified
boolean values in the standard way (
false is considered less than
true).
|
static boolean[] |
concat(boolean[]... arrays)
Returns the values from each provided array combined into a single array.
|
static boolean |
contains(boolean[] array, boolean target)
Returns
true if
target is present as an element anywhere in
array.
|
static int |
countTrue(boolean... values)
Returns the number of
values that are
true.
|
static boolean[] |
ensureCapacity(boolean[] array, int minLength, int padding)
Returns an array containing the same values as
array, but guaranteed to be of a specified minimum length.
|
static int |
hashCode(boolean value)
Returns a hash code for
value; equal to the result of invoking
((Boolean) value).hashCode().
|
static int |
indexOf(boolean[] array, boolean target)
Returns the index of the first appearance of the value
target in
array.
|
static int |
indexOf(boolean[] array, boolean[] 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
Returns a string containing the supplied
boolean values separated by
separator.
|
static int |
lastIndexOf(boolean[] array, boolean target)
Returns the index of the last appearance of the value
target in
array.
|
static Comparator |
lexicographicalComparator()
Returns a comparator that compares two
boolean arrays lexicographically.
|
static boolean[] |
toArray(Collection
Copies a collection of
Boolean instances into a new array of primitive
boolean values.
|
public static int hashCode(boolean value)
value; equal to the result of invoking
((Boolean) value).hashCode().
value - a primitive
boolean value
public static int compare(boolean a,
boolean b)
boolean values in the standard way (
false is considered less than
true). The sign of the value returned is the same as that of
((Boolean) a).compareTo(b).
Note for Java 7 and later: this method should be treated as deprecated; use the equivalent Boolean method instead.
a - the first
boolean to compare
b - the second
boolean to compare
a is
true, a negative number if only
b is true, or zero if
a == b
public static boolean contains(boolean[] array,
boolean target)
true if
target is present as an element anywhere in
array.
Note: consider representing the array as a BitSet instead, replacing Booleans.contains(array, true) with !bitSet.isEmpty() and Booleans.contains(array, false) with bitSet.nextClearBit(0) == sizeOfBitSet.
array - an array of
boolean values, possibly empty
target - a primitive
boolean value
true if
array[i] == target for some value of
i
public static int indexOf(boolean[] array,
boolean target)
target in
array.
Note: consider representing the array as a BitSet instead, and using BitSet or BitSet.
array - an array of
boolean values, possibly empty
target - a primitive
boolean value
i for which
array[i] == target, or
-1 if no such index exists.
public static int indexOf(boolean[] array,
boolean[] target)
target within
array, or
-1 if there is no such occurrence.
More formally, returns the lowest index i such that java.util.Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.
array - the array to search for the sequence
target
target - the array to search for as a sub-sequence of
array
public static int lastIndexOf(boolean[] array,
boolean target)
target in
array.
array - an array of
boolean values, possibly empty
target - a primitive
boolean value
i for which
array[i] == target, or
-1 if no such index exists.
public static boolean[] concat(boolean[]... arrays)
concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c} returns the array
{a, b, c}.
arrays - zero or more
boolean arrays
public static boolean[] ensureCapacity(boolean[] array,
int minLength,
int padding)
array, but guaranteed to be of a specified minimum length. If
array already has a length of at least
minLength, it is returned directly. Otherwise, a new array of size
minLength + padding is returned, containing the values of
array, and zeroes in the remaining places.
array - the source array
minLength - the minimum length the returned array must guarantee
padding - an extra amount to "grow" the array by if growth is necessary
array, with guaranteed minimum length
minLength
IllegalArgumentException - if
minLength or
padding is negative
public static Stringjoin(String separator, boolean... array)
boolean values separated by
separator. For example,
join("-", false, true, false) returns the string
"false-true-false".
separator - the text that should appear between consecutive values in the resulting string (but not at the start or end)
array - an array of
boolean values, possibly empty
public static Comparator<boolean[]> lexicographicalComparator()
boolean arrays lexicographically. That is, it compares, using
compare(boolean, boolean)), 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,
[] < [false] < [false, true] < [true].
The returned comparator is inconsistent with Object (since arrays support only identity equality), but it is consistent with Arrays.
public static boolean[] toArray(Collection<Boolean > collection)
Boolean instances into a new array of primitive
boolean values.
Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.
Note: consider representing the collection as a BitSet instead.
collection - a collection of
Boolean objects
collection, in the same order, converted to primitives
NullPointerException - if
collection or any of its elements is null
public static List<Boolean > asList(boolean... backingArray)
Arrays.asList(Object[]) . The list supports
List.set(int, Object) , but any attempt to set a value to
null will result in a
NullPointerException.
The returned list maintains the values, but not the identities, of Boolean objects written to or read from it. For example, whether list.get(0) == list.get(0) is true for the returned list is unspecified.
backingArray - the array to back the list
@Beta public static int countTrue(boolean... values)
values that are
true.