Class Booleans


  • @GwtCompatible
    public final class Booleans
    extends java.lang.Object
    Static utility methods pertaining to boolean primitives, that are not already found in either Boolean or Arrays.

    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 java.util.List<java.lang.Boolean> asList​(boolean... backingArray)
      Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
      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 java.util.Comparator<java.lang.Boolean> falseFirst()
      Returns a Comparator<Boolean> that sorts false before true.
      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 java.lang.String join​(java.lang.String separator, boolean... array)
      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 java.util.Comparator<boolean[]> lexicographicalComparator()
      Returns a comparator that compares two boolean arrays lexicographically.
      static void reverse​(boolean[] array)
      Reverses the elements of array.
      static void reverse​(boolean[] array, int fromIndex, int toIndex)
      Reverses the elements of array between fromIndex inclusive and toIndex exclusive.
      static void rotate​(boolean[] array, int distance)
      Performs a right rotation of array of "distance" places, so that the first element is moved to index "distance", and the element at index i ends up at index (distance + i) mod array.length.
      static void rotate​(boolean[] array, int distance, int fromIndex, int toIndex)
      Performs a right rotation of array between fromIndex inclusive and toIndex exclusive.
      static boolean[] toArray​(java.util.Collection<java.lang.Boolean> collection)
      Copies a collection of Boolean instances into a new array of primitive boolean values.
      static java.util.Comparator<java.lang.Boolean> trueFirst()
      Returns a Comparator<Boolean> that sorts true before false.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • trueFirst

        public static java.util.Comparator<java.lang.Boolean> trueFirst()
        Returns a Comparator<Boolean> that sorts true before false.

        This is particularly useful in Java 8+ in combination with Comparator.comparing, e.g. Comparator.comparing(Foo::hasBar, trueFirst()).

        Since:
        21.0
      • falseFirst

        public static java.util.Comparator<java.lang.Boolean> falseFirst()
        Returns a Comparator<Boolean> that sorts false before true.

        This is particularly useful in Java 8+ in combination with Comparator.comparing, e.g. Comparator.comparing(Foo::hasBar, falseFirst()).

        Since:
        21.0
      • hashCode

        public static int hashCode​(boolean value)
        Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode().

        Java 8+ users: use Boolean.hashCode(boolean) instead.

        Parameters:
        value - a primitive boolean value
        Returns:
        a hash code for the value
      • compare

        public static int compare​(boolean a,
                                  boolean b)
        Compares the two specified 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).

        Java 7+ users: this method should be treated as deprecated; use the equivalent Boolean.compare(boolean, boolean) method instead.

        Parameters:
        a - the first boolean to compare
        b - the second boolean to compare
        Returns:
        a positive number if only a is true, a negative number if only b is true, or zero if a == b
      • contains

        public static boolean contains​(boolean[] array,
                                       boolean target)
        Returns 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.

        Parameters:
        array - an array of boolean values, possibly empty
        target - a primitive boolean value
        Returns:
        true if array[i] == target for some value of i
      • indexOf

        public static int indexOf​(boolean[] array,
                                  boolean target)
        Returns the index of the first appearance of the value target in array.

        Note: consider representing the array as a BitSet instead, and using BitSet.nextSetBit(int) or BitSet.nextClearBit(int).

        Parameters:
        array - an array of boolean values, possibly empty
        target - a primitive boolean value
        Returns:
        the least index i for which array[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 specified target within array, or -1 if there is no such occurrence.

        More formally, returns the lowest index i such that Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.

        Parameters:
        array - the array to search for the sequence target
        target - the array to search for as a sub-sequence of array
      • lastIndexOf

        public static int lastIndexOf​(boolean[] array,
                                      boolean target)
        Returns the index of the last appearance of the value target in array.
        Parameters:
        array - an array of boolean values, possibly empty
        target - a primitive boolean value
        Returns:
        the greatest index i for which array[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 more boolean 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 as 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.
        Parameters:
        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
        Returns:
        an array containing the values of array, with guaranteed minimum length minLength
        Throws:
        java.lang.IllegalArgumentException - if minLength or padding is negative
      • join

        public static java.lang.String join​(java.lang.String separator,
                                            boolean... array)
        Returns a string containing the supplied boolean values separated by separator. 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 of boolean values, possibly empty
      • lexicographicalComparator

        public static java.util.Comparator<boolean[]> lexicographicalComparator()
        Returns a comparator that compares two 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.equals(Object) (since arrays support only identity equality), but it is consistent with Arrays.equals(boolean[], boolean[]).

        Since:
        2.0
      • toArray

        public static boolean[] toArray​(java.util.Collection<java.lang.Boolean> collection)
        Copies a collection of 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.

        Parameters:
        collection - a collection of Boolean objects
        Returns:
        an array containing the same values as collection, in the same order, converted to primitives
        Throws:
        java.lang.NullPointerException - if collection or any of its elements is null
      • asList

        public static java.util.List<java.lang.Boolean> asList​(boolean... backingArray)
        Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). The list supports List.set(int, Object), but any attempt to set a value to null will result in a NullPointerException.

        There are at most two distinct objects in this list, (Boolean) true and (Boolean) false. Java guarantees that those are always represented by the same objects.

        The returned list is serializable.

        Parameters:
        backingArray - the array to back the list
        Returns:
        a list view of the array
      • countTrue

        public static int countTrue​(boolean... values)
        Returns the number of values that are true.
        Since:
        16.0
      • reverse

        public static void reverse​(boolean[] array)
        Reverses the elements of array. This is equivalent to Collections.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 of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.reverse(Booleans.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.
        Throws:
        java.lang.IndexOutOfBoundsException - if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
        Since:
        23.1
      • rotate

        public static void rotate​(boolean[] array,
                                  int distance)
        Performs a right rotation of array of "distance" places, so that the first element is moved to index "distance", and the element at index i ends up at index (distance + i) mod array.length. This is equivalent to Collections.rotate(Booleans.asList(array), distance), but is somewhat faster.

        The provided "distance" may be negative, which will rotate left.

        Since:
        32.0.0
      • rotate

        public static void rotate​(boolean[] array,
                                  int distance,
                                  int fromIndex,
                                  int toIndex)
        Performs a right rotation of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.rotate(Booleans.asList(array).subList(fromIndex, toIndex), distance), but is somewhat faster.

        The provided "distance" may be negative, which will rotate left.

        Throws:
        java.lang.IndexOutOfBoundsException - if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
        Since:
        32.0.0