Orbital library

orbital.util
Class ReverseComparator

java.lang.Object
  extended by orbital.util.ReverseComparator
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator

public class ReverseComparator
extends java.lang.Object
implements java.util.Comparator, java.io.Serializable

This class is a ReverseComparator which imposes the reverse order on a collection of objects.

Depending on whether a Comparator to be reverted is given, the ordering induced by that Comparator will be reverted. Otherwise, the natural ordering will be reverted - as Collections.revertOrder() does - supposing that the objects in the collection implement the Comparable interface.

This corresponds roughly to a negation of a comparator (except for the case of equality).

Author:
André Platzer
See Also:
Collections.reverseOrder(), Serialized Form

Field Summary
protected  java.util.Comparator inner
          Contains the Comparator to be inverted, or null if the objects Comparable implementation should be used, instead.
 
Constructor Summary
ReverseComparator()
          Create a Comparator that is the opposite to the natural ordering induced by the Comparable implementation of the objects compared.
ReverseComparator(java.util.Comparator inner)
          Create a Comparator that is the opposite to an ordering induced by a given one.
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          Compares two objects.
 boolean equals(java.lang.Object obj)
           
 int hashCode()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inner

protected java.util.Comparator inner
Contains the Comparator to be inverted, or null if the objects Comparable implementation should be used, instead.

Constructor Detail

ReverseComparator

public ReverseComparator(java.util.Comparator inner)
Create a Comparator that is the opposite to an ordering induced by a given one.

Parameters:
inner - the Comparator to be inverted, or null if the objects Comparable implementation should be used, instead.

ReverseComparator

public ReverseComparator()
Create a Comparator that is the opposite to the natural ordering induced by the Comparable implementation of the objects compared.

Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Compares two objects. The inner Comparator is used if specified, or one of the arguments are used if they implement Comparable.

Specified by:
compare in interface java.util.Comparator
Returns:
If a Comparator inner!=null, then this method returns inner.compare(o2,o1). If inner==null and if one of the objects (preferably o1) is Comparable, -((Comparable)on).compareTo(om) is returned. If no comparator is specified and none of the objects implements Comparable, an exception is thrown.
Throws:
java.lang.ClassCastException - if Comparator inner==null and both, o1 and o2 do not implement Comparable.

equals

public boolean equals(java.lang.Object obj)
Specified by:
equals in interface java.util.Comparator
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

Orbital library
1.3.0: 11 Apr 2009

Copyright © 1996-2009 André Platzer
All Rights Reserved.