org.apache.commons.collections.comparators

Class BooleanComparator

Implemented Interfaces:
Comparator, Serializable

public final class BooleanComparator
extends java.lang.Object
implements Comparator, Serializable

A Comparator for Boolean objects that can sort either true or false first.

Version:
$Revision: 1.14 $ $Date: 2004/05/16 11:56:47 $
Author:
Rodney Waldhoff
Since:
Commons Collections 3.0
See Also:
getTrueFirstComparator(), getFalseFirstComparator(), getBooleanComparator(boolean)

Constructor Summary

BooleanComparator()
Creates a BooleanComparator that sorts false values before true values.
BooleanComparator(boolean trueFirst)
Creates a BooleanComparator that sorts trueFirst values before !trueFirst values.

Method Summary

int
compare(Boolean b1, Boolean b2)
Compares two non-null Boolean objects according to the value of trueFirst.
int
compare(Object obj1, Object obj2)
Compares two arbitrary Objects.
boolean
equals(Object object)
Returns true iff that Object is is a Comparator whose ordering is known to be equivalent to mine.
static BooleanComparator
getBooleanComparator(boolean trueFirst)
Returns a BooleanComparator instance that sorts trueFirst values before !trueFirst values.
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.

Constructor Details

BooleanComparator

public BooleanComparator()

BooleanComparator

public BooleanComparator(boolean trueFirst)
Creates a BooleanComparator that sorts trueFirst values before !trueFirst values.

Please use the static factories instead whenever possible.

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

Method Details

compare

public int compare(Boolean b1,
                   Boolean b2)
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

compare

public int compare(Object obj1,
                   Object obj2)
Parameters:
obj1 - the first object to compare
obj2 - the second object to compare
Returns:
negative if obj1 is less, positive if greater, zero if equal

equals

public boolean equals(Object object)
Parameters:
object - the object to compare to
Returns:
true if equal

getBooleanComparator

public static BooleanComparator getBooleanComparator(boolean trueFirst)
Returns a BooleanComparator instance that sorts trueFirst values before !trueFirst 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 Booleans before false
Returns:
a singleton BooleanComparator instance

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

hashCode

public int hashCode()
Returns:
a hash code for this comparator.

sortsTrueFirst

public boolean sortsTrueFirst()
Returns:
the trueFirst flag

Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.