Class BooleanComparator
- java.lang.Object
-
- org.apache.commons.collections4.comparators.BooleanComparator
-
- All Implemented Interfaces:
java.io.Serializable,java.util.Comparator<java.lang.Boolean>
public final class BooleanComparator extends java.lang.Object implements java.util.Comparator<java.lang.Boolean>, java.io.SerializableAComparatorforBooleanobjects 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 aBooleanComparatorthat sortsfalsevalues beforetruevalues.BooleanComparator(boolean trueFirst)Creates aBooleanComparatorthat sortstrueFirstvalues before!trueFirstvalues.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BooleanComparatorbooleanComparator(boolean trueFirst)Returns a BooleanComparator instance that sortstrueFirstvalues before!trueFirstvalues.intcompare(java.lang.Boolean b1, java.lang.Boolean b2)booleanequals(java.lang.Object object)Returnstrueiff that Object is is aComparatorwhose ordering is known to be equivalent to mine.static BooleanComparatorgetFalseFirstComparator()Returns a BooleanComparator instance that sortsfalsevalues beforetruevalues.static BooleanComparatorgetTrueFirstComparator()Returns a BooleanComparator instance that sortstruevalues beforefalsevalues.inthashCode()Implement a hash code for this comparator that is consistent withequals.booleansortsTrueFirst()Returnstrueiff I sorttruevalues beforefalsevalues.
-
-
-
Constructor Detail
-
BooleanComparator
public BooleanComparator()
Creates aBooleanComparatorthat sortsfalsevalues beforetruevalues.Equivalent to
BooleanComparator(false).Please use the static factory instead whenever possible.
-
BooleanComparator
public BooleanComparator(boolean trueFirst)
Creates aBooleanComparatorthat sortstrueFirstvalues before!trueFirstvalues.Please use the static factories instead whenever possible.
- Parameters:
trueFirst- whentrue, sorttrueboolean values beforefalse
-
-
Method Detail
-
getTrueFirstComparator
public static BooleanComparator getTrueFirstComparator()
Returns a BooleanComparator instance that sortstruevalues beforefalsevalues.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
-
getFalseFirstComparator
public static BooleanComparator getFalseFirstComparator()
Returns a BooleanComparator instance that sortsfalsevalues beforetruevalues.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
-
booleanComparator
public static BooleanComparator booleanComparator(boolean trueFirst)
Returns a BooleanComparator instance that sortstrueFirstvalues before!trueFirstvalues.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- whentrue, sorttrueBooleans beforefalse- Returns:
- a singleton BooleanComparator instance
- Since:
- 4.0
-
compare
public int compare(java.lang.Boolean b1, java.lang.Boolean b2)- Specified by:
comparein interfacejava.util.Comparator<java.lang.Boolean>- Parameters:
b1- the first boolean to compareb2- the second boolean to compare- Returns:
- negative if obj1 is less, positive if greater, zero if equal
- Throws:
java.lang.NullPointerException- when either argumentnull
-
hashCode
public int hashCode()
Implement a hash code for this comparator that is consistent withequals.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code for this comparator.
-
equals
public boolean equals(java.lang.Object object)
Returnstrueiff that Object is is aComparatorwhose ordering is known to be equivalent to mine.This implementation returns
trueiffthatis aBooleanComparatorwhose value ofsortsTrueFirst()is equal to mine.- Specified by:
equalsin interfacejava.util.Comparator<java.lang.Boolean>- Overrides:
equalsin classjava.lang.Object- Parameters:
object- the object to compare to- Returns:
- true if equal
-
sortsTrueFirst
public boolean sortsTrueFirst()
Returnstrueiff I sorttruevalues beforefalsevalues. In other words, returnstrueiffcompare(Boolean.FALSE,Boolean.TRUE)returns a positive value.- Returns:
- the trueFirst flag
-
-