Class Booleans
- java.lang.Object
-
- com.google.common.primitives.Booleans
-
@GwtCompatible public final class Booleans extends Object
Static utility methods pertaining toboolean
primitives, that are not already found in eitherBoolean
orArrays
.See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static List<Boolean>
asList(boolean... backingArray)
Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[])
.static int
compare(boolean a, boolean b)
Compares the two specifiedboolean
values in the standard way (false
is considered less thantrue
).static boolean[]
concat(boolean[]... arrays)
Returns the values from each provided array combined into a single array.static boolean
contains(boolean[] array, boolean target)
Returnstrue
iftarget
is present as an element anywhere inarray
.static int
countTrue(boolean... values)
Returns the number ofvalues
that aretrue
.static boolean[]
ensureCapacity(boolean[] array, int minLength, int padding)
Returns an array containing the same values asarray
, but guaranteed to be of a specified minimum length.static Comparator<Boolean>
falseFirst()
Returns aComparator<Boolean>
that sortsfalse
beforetrue
.static int
hashCode(boolean value)
Returns a hash code forvalue
; 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 valuetarget
inarray
.static int
indexOf(boolean[] array, boolean[] target)
Returns the start position of the first occurrence of the specifiedtarget
withinarray
, or-1
if there is no such occurrence.static String
join(String separator, boolean... array)
Returns a string containing the suppliedboolean
values separated byseparator
.static int
lastIndexOf(boolean[] array, boolean target)
Returns the index of the last appearance of the valuetarget
inarray
.static Comparator<boolean[]>
lexicographicalComparator()
Returns a comparator that compares twoboolean
arrays lexicographically.static void
reverse(boolean[] array)
Reverses the elements ofarray
.static void
reverse(boolean[] array, int fromIndex, int toIndex)
Reverses the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive.static boolean[]
toArray(Collection<Boolean> collection)
Copies a collection ofBoolean
instances into a new array of primitiveboolean
values.static Comparator<Boolean>
trueFirst()
Returns aComparator<Boolean>
that sortstrue
beforefalse
.
-
-
-
Method Detail
-
trueFirst
@Beta public static Comparator<Boolean> trueFirst()
Returns aComparator<Boolean>
that sortstrue
beforefalse
.This is particularly useful in Java 8+ in combination with
Comparators.comparing
, e.g.Comparators.comparing(Foo::hasBar, trueFirst())
.- Since:
- 21.0
-
falseFirst
@Beta public static Comparator<Boolean> falseFirst()
Returns aComparator<Boolean>
that sortsfalse
beforetrue
.This is particularly useful in Java 8+ in combination with
Comparators.comparing
, e.g.Comparators.comparing(Foo::hasBar, falseFirst())
.- Since:
- 21.0
-
hashCode
public static int hashCode(boolean value)
Returns a hash code forvalue
; equal to the result of invoking((Boolean) value).hashCode()
.Java 8 users: use
Boolean.hashCode(boolean)
instead.- Parameters:
value
- a primitiveboolean
value- Returns:
- a hash code for the value
-
compare
public static int compare(boolean a, boolean b)
Compares the two specifiedboolean
values in the standard way (false
is considered less thantrue
). 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.compare(boolean, boolean)
method instead.- Parameters:
a
- the firstboolean
to compareb
- the secondboolean
to compare- Returns:
- a positive number if only
a
istrue
, a negative number if onlyb
is true, or zero ifa == b
-
contains
public static boolean contains(boolean[] array, boolean target)
Returnstrue
iftarget
is present as an element anywhere inarray
.Note: consider representing the array as a
BitSet
instead, replacingBooleans.contains(array, true)
with!bitSet.isEmpty()
andBooleans.contains(array, false)
withbitSet.nextClearBit(0) == sizeOfBitSet
.- Parameters:
array
- an array ofboolean
values, possibly emptytarget
- a primitiveboolean
value- Returns:
true
ifarray[i] == target
for some value ofi
-
indexOf
public static int indexOf(boolean[] array, boolean target)
Returns the index of the first appearance of the valuetarget
inarray
.Note: consider representing the array as a
BitSet
instead, and usingBitSet.nextSetBit(int)
orBitSet.nextClearBit(int)
.- Parameters:
array
- an array ofboolean
values, possibly emptytarget
- a primitiveboolean
value- Returns:
- the least index
i
for whicharray[i] == target
, or-1
if no such index exists.
-
indexOf
public static int indexOf(boolean[] array, boolean[] target)
Returns the start position of the first occurrence of the specifiedtarget
withinarray
, or-1
if there is no such occurrence.More formally, returns the lowest index
i
such thatArrays.copyOfRange(array, i, i + target.length)
contains exactly the same elements astarget
.- Parameters:
array
- the array to search for the sequencetarget
target
- the array to search for as a sub-sequence ofarray
-
lastIndexOf
public static int lastIndexOf(boolean[] array, boolean target)
Returns the index of the last appearance of the valuetarget
inarray
.- Parameters:
array
- an array ofboolean
values, possibly emptytarget
- a primitiveboolean
value- Returns:
- the greatest index
i
for whicharray[i] == target
, or-1
if no such index exists.
-
concat
public static boolean[] concat(boolean[]... arrays)
Returns the values from each provided array combined into a single array. For example,concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c}
returns the array{a, b, c}
.- Parameters:
arrays
- zero or moreboolean
arrays- Returns:
- a single array containing all the values from the source arrays, in order
-
ensureCapacity
public static boolean[] ensureCapacity(boolean[] array, int minLength, int padding)
Returns an array containing the same values asarray
, but guaranteed to be of a specified minimum length. Ifarray
already has a length of at leastminLength
, it is returned directly. Otherwise, a new array of sizeminLength + padding
is returned, containing the values ofarray
, and zeroes in the remaining places.- Parameters:
array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- an extra amount to "grow" the array by if growth is necessary- Returns:
- an array containing the values of
array
, with guaranteed minimum lengthminLength
- Throws:
IllegalArgumentException
- ifminLength
orpadding
is negative
-
join
public static String join(String separator, boolean... array)
Returns a string containing the suppliedboolean
values separated byseparator
. For example,join("-", false, true, false)
returns the string"false-true-false"
.- Parameters:
separator
- the text that should appear between consecutive values in the resulting string (but not at the start or end)array
- an array ofboolean
values, possibly empty
-
lexicographicalComparator
public static Comparator<boolean[]> lexicographicalComparator()
Returns a comparator that compares twoboolean
arrays lexicographically. That is, it compares, usingcompare(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.equals(Object)
(since arrays support only identity equality), but it is consistent withArrays.equals(boolean[], boolean[])
.- Since:
- 2.0
-
toArray
public static boolean[] toArray(Collection<Boolean> collection)
Copies a collection ofBoolean
instances into a new array of primitiveboolean
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.- Parameters:
collection
- a collection ofBoolean
objects- Returns:
- an array containing the same values as
collection
, in the same order, converted to primitives - Throws:
NullPointerException
- ifcollection
or any of its elements is null
-
asList
public static List<Boolean> asList(boolean... backingArray)
Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[])
. The list supportsList.set(int, Object)
, but any attempt to set a value tonull
will result in aNullPointerException
.The returned list maintains the values, but not the identities, of
Boolean
objects written to or read from it. For example, whetherlist.get(0) == list.get(0)
is true for the returned list is unspecified.- Parameters:
backingArray
- the array to back the list- Returns:
- a list view of the array
-
countTrue
@Beta public static int countTrue(boolean... values)
Returns the number ofvalues
that aretrue
.- Since:
- 16.0
-
reverse
public static void reverse(boolean[] array)
Reverses the elements ofarray
. This is equivalent toCollections.reverse(Booleans.asList(array))
, but is likely to be more efficient.- Since:
- 23.1
-
reverse
public static void reverse(boolean[] array, int fromIndex, int toIndex)
Reverses the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive. This is equivalent toCollections.reverse(Booleans.asList(array).subList(fromIndex, toIndex))
, but is likely to be more efficient.- Throws:
IndexOutOfBoundsException
- iffromIndex < 0
,toIndex > array.length
, ortoIndex > fromIndex
- Since:
- 23.1
-
-