Holger's Java API

## com.antelmann.util
Class UniversalComparator

    java.lang.Object
      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 values.

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

Constructor Summary
`UniversalComparator()` uses the default Collator instance
`UniversalComparator(boolean caseSensitive)` uses no Collator but direct String comparison instead
`UniversalComparator(Collator collator)` if a null value is given, String comparison is used instead of a collator

Method Summary

`static

`<T> Comparator<T>`

`

`combine(Comparator<? super T> c1, Comparator<? super T> c2)`

`int` `compare(Number n1, Number n2)`
`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 (which may be null); its standard instance is the default value.
`static UniversalComparator` `getDefaultInstance()` returns one global default instance, which uses the default Collator instance by default
`boolean` `isCaseSensitive()` if true, the String comparison (if it applies) is case sensitive (false is the default).

`static

`<T> Comparator<T>`

`

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

`Comparator<Object>` `reversed()`
`void` `setCaseSensitive(boolean flag)` if set to true, the String comparison (if it applies) is case sensitive.
`void` `setCollator(Collator collator)` allows to change the default collator in use; if set to null, the String comparison is used directly without a Collator.

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()

  uses the default Collator instance

### UniversalComparator

    public UniversalComparator(boolean caseSensitive)

  uses no Collator but direct String comparison instead

### UniversalComparator

    public UniversalComparator(Collator collator)

  if a null value is given, String comparison is used instead of a collator

Method Detail

### getDefaultInstance

    public static UniversalComparator getDefaultInstance()

  returns one global default instance, which uses the default Collator instance by default

### getCollator

    public Collator getCollator()

  provides access to the embedded collator (which may be null); its standard instance is the default value.

### setCollator

    public void setCollator(Collator collator)

  allows to change the default collator in use; if set to null, the String comparison is used directly without a Collator.

  See Also:
    `String.compareTo(String)`

### isCaseSensitive

    public boolean isCaseSensitive()

  if true, the String comparison (if it applies) is case sensitive (false is the default). Note that this option only applies if the collator of this instance is null!

### setCaseSensitive

    public void setCaseSensitive(boolean flag)

  if set to true, the String comparison (if it applies) is case sensitive. Note that this option only applies if the collator of this instance is null!

### compare

    public int compare(String o1,
                       String o2)

### compare

    public int compare(Number n1,
                       Number n2)

### 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>`

### reversed

    public Comparator<Object> reversed()

### reverse

    public static <T> Comparator<T> reverse(Comparator<T> c)

  a utility method for convenience that simply reverses the given ordering

### combine

    public static <T> Comparator<T> combine(Comparator<? super T> c1,
                                            Comparator<? super T> c2)

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