ProgramixGenericLib v4.0.0

com.programix.collections
Class NullFirstComparator

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

public class NullFirstComparator
extends Object
implements Comparator

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 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(Object obj1, Object 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 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(Object obj1,
                   Object 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

ProgramixGenericLib v4.0.0

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