net.sf.jga.util
Class NullComparator<T>

java.lang.Object
  extended by net.sf.jga.util.NullComparator<T>
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator<T>

public class NullComparator<T>
extends java.lang.Object
implements java.util.Comparator<T>, java.io.Serializable

Comparator Decorator used to gracefully compare null and non-null values. In most cases, passing a null to a Comparator results in a NullPointerException, as (in the general case) null is not part of a class' strict ordering. However, in many applications, null objects must be included in an ordering. This wrapper can be used to shield an underlying Comparator from dealing with where in the ordering null values should fall.

A flag is provided that determines whether nulls are less than non-nulls, (the default case) or greater than non-nulls. When comparing two values, this comparator determines if either argument is null: if both are null, then 0 is returned (ie, all nulls are considered equal). If neither argument is null, then both arguments are passed to the wrapped comparator. If one argument is null, then a positive or negative integer is returned, depending on which argument was null and the setting of the flag.

See Also:
Serialized Form

Constructor Summary
NullComparator(java.util.Comparator<T> comp)
          Builds a NullComparator that uses the given comparator to compare pairs of non-null values.
NullComparator(java.util.Comparator<T> comp, boolean flag)
          Builds a NullComparator that uses the given comparator to compare pairs of non-null values, and uses the flag to determine if non-null values appear before non-null values in the resulting ordering.
 
Method Summary
 int compare(T x, T y)
           
 java.util.Comparator<T> getComparator()
          Returns the comparator used to compare pairs of non-null values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

NullComparator

public NullComparator(java.util.Comparator<T> comp)
Builds a NullComparator that uses the given comparator to compare pairs of non-null values. Null values appear before non-null values in the resulting ordering.


NullComparator

public NullComparator(java.util.Comparator<T> comp,
                      boolean flag)
Builds a NullComparator that uses the given comparator to compare pairs of non-null values, and uses the flag to determine if non-null values appear before non-null values in the resulting ordering.

Method Detail

getComparator

public java.util.Comparator<T> getComparator()
Returns the comparator used to compare pairs of non-null values.


compare

public int compare(T x,
                   T y)
Specified by:
compare in interface java.util.Comparator<T>


Copyright © 2002-2006 David A. Hall. All Rights Reserved.