net.sf.collections15.comparators
Class BooleanComparator

java.lang.Object
  extended by net.sf.collections15.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.Serializable

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

Since:
Collections15 1.0
Author:
Rodney Waldhoff, Chris Lambrou (port to Java 5.0)
See Also:
getTrueFirstComparator(), getFalseFirstComparator(), getInstance(boolean), Serialized Form

Constructor Summary
protected BooleanComparator(boolean trueFirst)
          Creates a BooleanComparator that sorts trueFirst values before !trueFirst values.
 
Method Summary
 int compare(java.lang.Boolean b1, java.lang.Boolean b2)
          Compares two non-null Boolean objects according to the value of trueFirst.
 boolean equals(java.lang.Object object)
          Compares this comparator to a specified object.
static BooleanComparator getFalseFirstComparator()
          Returns a BooleanComparator instance that sorts false values before true values.
static BooleanComparator getInstance(boolean trueFirst)
          Returns a BooleanComparator instance with the specified sorting order.
static BooleanComparator getTrueFirstComparator()
          Returns a BooleanComparator instance that sorts true values before false values.
 int hashCode()
          Implements a hash code for this comparator that is consistent with equals.
 boolean sortsTrueFirst()
          Returns true only if this Boolean comparator instance sorts true values before false values.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BooleanComparator

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

Parameters:
trueFirst - If true the Comparator sorts true before false. If false, the Comparator sorts false before true.
Method Detail

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:
a Comparator that sorts true before false.

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:
a Comparator that sorts false before true.

getInstance

public static BooleanComparator getInstance(boolean trueFirst)
Returns a BooleanComparator instance with the specified sorting order.

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 - If true the Comparator instance returned sorts true before false. If false, the returned instances sorts false before true.
Returns:
A Comparator with the specified sorting order.

compare

public int compare(java.lang.Boolean b1,
                   java.lang.Boolean b2)
Compares two non-null Boolean objects according to the value of trueFirst.

Specified by:
compare in interface java.util.Comparator<java.lang.Boolean>
Parameters:
b1 - The first boolean to compare.
b2 - The second boolean to compare.
Returns:
A negative value if b1 is less than b2, a positive value if b1 is greater than b2, or zero if b1 and b2 are equal.
Throws:
java.lang.NullPointerException - if either argument null.

hashCode

public int hashCode()
Implements a hash code for this comparator that is consistent with equals.

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code for this comparator.

equals

public boolean equals(java.lang.Object object)
Compares this comparator to a specified object.

Specified by:
equals in interface java.util.Comparator<java.lang.Boolean>
Overrides:
equals in class java.lang.Object
Parameters:
object - The object to compare to.
Returns:
true only if object is a BooleanComparator whose trueFirst value is equal to this BooleanComparator's value.

sortsTrueFirst

public boolean sortsTrueFirst()
Returns true only if this Boolean comparator instance sorts true values before false values. In other words, returns true only if compare(Boolean.FALSE,Boolean.TRUE) returns a positive value.

Returns:
The trueFirst flag.


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.