ProgramixGenericLib v5.0.1

com.programix.collections
Class NullFirstComparator<T>

java.lang.Object
  extended by com.programix.collections.NullFirstComparator<T>
All Implemented Interfaces:
Comparator<T>

public class NullFirstComparator<T>
extends Object
implements Comparator<T>

This Comparator wrapper considers "null" to be "less than" any non-null reference and will sort any null's to the top. This Comparator wraps around a "raw" Comparator and first checks to see if either of the references is null. If they are both null, then they are considered equal. If one is null and the other is not null, then the null one is considered to be "less than" the other. If both references are not null, then the supplied "raw" Comparator is used to complete the comparison.

Author:
Paul Hyde
See Also:
NullLastComparator, ReverseComparator, ComparableComparator

Constructor Summary
NullFirstComparator(Comparator<T> rawComparator)
          Create a wrapper that checks for null references before calling the rawComparator and considers null's to be "less than" non-null references.
 
Method Summary
 int compare(T obj1, T obj2)
          This implementation of the compare method on Comparator will quickly check if both references are the same using == (which evaluates to true if both references are pointing to the same object or both are null).
 
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

NullFirstComparator

public NullFirstComparator(Comparator<T> rawComparator)
Create a wrapper that checks for null references before calling the rawComparator and considers null's to be "less than" non-null references.

Parameters:
rawComparator - the comparator to use if (and only if) both references are NOT null.
Method Detail

compare

public int compare(T obj1,
                   T obj2)
This implementation of the compare method on Comparator will quickly check if both references are the same using == (which evaluates to true if both references are pointing to the same object or both are null). If the references are the same, then this will quickly return 0.

Specified by:
compare in interface Comparator<T>

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.