ProgramixGenericLib v5.0.1

com.programix.collections
Class NullLastComparator<T>

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

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

This Comparator wrapper considers "null" to be "greater than" any non-null reference and will sort any null's to the bottom. 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 "greater 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:
NullFirstComparator, ReverseComparator, ComparableComparator

Constructor Summary
NullLastComparator(Comparator<T> rawComparator)
          Create a wrapper that checks for null references before calling the rawComparator and considers null's to be "greater 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

NullLastComparator

public NullLastComparator(Comparator<T> rawComparator)
Create a wrapper that checks for null references before calling the rawComparator and considers null's to be "greater 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.