Holger's
Java API

com.antelmann.util
Class UniversalComparator

java.lang.Object
  extended by com.antelmann.util.UniversalComparator
All Implemented Interfaces:
Comparator<Object>

public class UniversalComparator
extends Object
implements Comparator<Object>

The UniversalComparator is useful as a default if no other more specific sorting strategy is present. Its Collator first tries to order the objects by their natural order - if applicable (except for Strings, which will be sorted with a default Collator). If that fails, the objects will be sorted based on their toString() value using the embedded instance of a Collator. Also, this class gracefully handles null vaules.

Since:
6/2/06
Author:
Holger Antelmann
See Also:
Collator

Constructor Summary
UniversalComparator()
           
UniversalComparator(Collator collator)
           
 
Method Summary
 int compare(Comparable o1, Comparable o2)
           
 int compare(Object o1, Object o2)
          null values are accepted and are sorted before a non-null value
 int compare(String o1, String o2)
           
 Collator getCollator()
          provides access to the embedded collator
static
<T> Comparator<T>
reverse(Comparator<T> c)
          a utility method for convenience that simply reverses the given ordering
 void setCollator(Collator collator)
          allows to change the default collator in use
 
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

UniversalComparator

public UniversalComparator()

UniversalComparator

public UniversalComparator(Collator collator)
Method Detail

getCollator

public Collator getCollator()
provides access to the embedded collator


setCollator

public void setCollator(Collator collator)
allows to change the default collator in use


compare

public int compare(Comparable o1,
                   Comparable o2)

compare

public int compare(String o1,
                   String o2)

compare

public int compare(Object o1,
                   Object o2)
null values are accepted and are sorted before a non-null value

Specified by:
compare in interface Comparator<Object>

reverse

public static <T> Comparator<T> reverse(Comparator<T> c)
a utility method for convenience that simply reverses the given ordering



(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads