Package org.apache.commons.collections4.comparators

# Class BooleanComparator

java.lang.Object
org.apache.commons.collections4.comparators.BooleanComparator

All Implemented Interfaces:
  `Serializable`, `Comparator<Boolean>`

public final class BooleanComparator extends Object implements Comparator<Boolean>, Serializable
A `Comparator` for `Boolean` objects that can sort either true or false first.

Since:
  3.0
See Also:
  - `getTrueFirstComparator()`
  - `getFalseFirstComparator()`
  - `booleanComparator(boolean)`
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BooleanComparator()`
  Creates a `BooleanComparator` that sorts `false` values before `true` values.
  `BooleanComparator(boolean trueFirst)`
  Creates a `BooleanComparator` that sorts `<i>trueFirst</i>` values before `&#x21;<i>trueFirst</i>` values.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static BooleanComparator`
  `booleanComparator(boolean trueFirst)`
  Returns a BooleanComparator instance that sorts `<i>trueFirst</i>` values before `&#x21;<i>trueFirst</i>` values.
  `int`
  `compare(Boolean b1, Boolean b2)`
  Compares two non-`null` `Boolean` objects according to the value of `sortsTrueFirst()`.
  `boolean`
  `equals(Object object)`
  Returns `true` iff that Object is a `Comparator` whose ordering is known to be equivalent to mine.
  `static BooleanComparator`
  `getFalseFirstComparator()`
  Returns a BooleanComparator instance that sorts `false` values before `true` values.
  `static BooleanComparator`
  `getTrueFirstComparator()`
  Returns a BooleanComparator instance that sorts `true` values before `false` values.
  `int`
  `hashCode()`
  Implement a hash code for this comparator that is consistent with `equals`.
  `boolean`
  `sortsTrueFirst()`
  Returns `true` iff I sort `true` values before `false` values.

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface java.util.Comparator

  `reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- ## Constructor Details

  - ### BooleanComparator

    public BooleanComparator()
    Creates a `BooleanComparator` that sorts `false` values before `true` values.

    Equivalent to `BooleanComparator(false)`.

    Please use the static factory instead whenever possible.

  - ### BooleanComparator

    public BooleanComparator(boolean trueFirst)
    Creates a `BooleanComparator` that sorts `<i>trueFirst</i>` values before `&#x21;<i>trueFirst</i>` values.

    Please use the static factories instead whenever possible.

    Parameters:
      `trueFirst` - when `true`, sort `true` boolean values before `false`

- ## Method Details

  - ### booleanComparator

    public static BooleanComparator booleanComparator(boolean trueFirst)
    Returns a BooleanComparator instance that sorts `<i>trueFirst</i>` values before `&#x21;<i>trueFirst</i>` values.

    Clients are encouraged to use the value returned from this method instead of constructing a new instance to reduce allocation and garbage collection overhead when multiple BooleanComparators may be used in the same virtual machine.

    Parameters:
      `trueFirst` - when `true`, sort `true` `Boolean`s before `false`
    Returns:
      a singleton BooleanComparator instance
    Since:
      4.0

  - ### getFalseFirstComparator

    public static BooleanComparator getFalseFirstComparator()
    Returns a BooleanComparator instance that sorts `false` values before `true` values.

    Clients are encouraged to use the value returned from this method instead of constructing a new instance to reduce allocation and garbage collection overhead when multiple BooleanComparators may be used in the same virtual machine.

    Returns:
      the false first singleton BooleanComparator

  - ### getTrueFirstComparator

    public static BooleanComparator getTrueFirstComparator()
    Returns a BooleanComparator instance that sorts `true` values before `false` values.

    Clients are encouraged to use the value returned from this method instead of constructing a new instance to reduce allocation and garbage collection overhead when multiple BooleanComparators may be used in the same virtual machine.

    Returns:
      the true first singleton BooleanComparator

  - ### compare

    public int compare(Boolean b1, Boolean b2)
    Compares two non-`null` `Boolean` objects according to the value of `sortsTrueFirst()`.
    Specified by:
      `compare` in interface `Comparator<Boolean>`
    Parameters:
      `b1` - the first boolean to compare
      `b2` - the second boolean to compare
    Returns:
      negative if obj1 is less, positive if greater, zero if equal
    Throws:
      `NullPointerException` - when either argument `null`

  - ### equals

    public boolean equals(Object object)
    Returns `true` iff that Object is a `Comparator` whose ordering is known to be equivalent to mine.

    This implementation returns `true` iff `<i>that</i>` is a `BooleanComparator` whose value of `sortsTrueFirst()` is equal to mine.

    Specified by:
      `equals` in interface `Comparator<Boolean>`
    Overrides:
      `equals` in class `Object`
    Parameters:
      `object` - the object to compare to
    Returns:
      true if equal

  - ### hashCode

    public int hashCode()
    Implement a hash code for this comparator that is consistent with `equals`.
    Overrides:
      `hashCode` in class `Object`
    Returns:
      a hash code for this comparator.

  - ### sortsTrueFirst

    public boolean sortsTrueFirst()
    Returns `true` iff I sort `true` values before `false` values. In other words, returns `true` iff `compare(Boolean.FALSE,Boolean.TRUE)` returns a positive value.
    Returns:
      the trueFirst flag