structure
Class ReverseComparator<ELTTYPE extends java.lang.Comparable<ELTTYPE>>

java.lang.Object
  extended by structure.ReverseComparator<ELTTYPE>
All Implemented Interfaces:
java.util.Comparator<ELTTYPE>

public class ReverseComparator<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
extends java.lang.Object
implements java.util.Comparator<ELTTYPE>

Implementation of the Comparator interface that provides a Comparator.compare(Object,Object) method that compares two objects using those objects default compareTo methods. Reverse comparator however, reverses the natural comparison as follows:

Were we to call compare(a,b), ReverseComparator would return the following:

     >0 if a < b
      0 if a = b
     <0 if a > b
 

Example usage:

To print out the equality relationship between two randomly generated integers we could use the following:

 public static void main(String[] argv){
     Random  rand = new Random();
     Comparator c = new ReverseComparator();
      
     //generate two random Integers
     Integer a = new Integer(rand.nextInt(100));
     Integer b = new Integer(rand.nextInt(100));
      
     //print out the proper equality relationship between these integers
     if(c.compare(a,b) > 0) System.out.println("A:" + a + " > B:" + b);
     else if (c.compare(a,b) < 0) System.out.println("A:" + a + " < B:" + b);
     else System.out.println("A:" + a + " = B:" + b);
 

 


Constructor Summary
ReverseComparator()
          Construct a comparator that generates reverse natural comparison
ReverseComparator(java.util.Comparator<ELTTYPE> base)
          Construct a comparator that generates reverse of another comparator
 
Method Summary
 int compare(ELTTYPE a, ELTTYPE b)
          Compare two values, a and b.
 boolean equals(java.lang.Object b)
          Returns true if the other object is a NaturalComparator.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReverseComparator

public ReverseComparator()
Construct a comparator that generates reverse natural comparison

Post:
constructs a comparator that orders in reverse order

ReverseComparator

public ReverseComparator(java.util.Comparator<ELTTYPE> base)
Construct a comparator that generates reverse of another comparator

Parameters:
base - the ordering to be reversed
Post:
constructs a Comparator that orders in reverse order of base
Method Detail

compare

public int compare(ELTTYPE a,
                   ELTTYPE b)
Compare two values, a and b. Simply calls the default compareTo method for a on b.

Specified by:
compare in interface java.util.Comparator<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Parameters:
a - object performing the compare
b - the object being compared
Returns:
value <, ==, > 0 if a <, ==, > b using a.compareTo
Pre:
a, b non-null, and b is of type of a
Post:
returns value <, ==, > 0 if a <, ==, > b

equals

public boolean equals(java.lang.Object b)
Returns true if the other object is a NaturalComparator.

Specified by:
equals in interface java.util.Comparator<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Overrides:
equals in class java.lang.Object
Parameters:
b - a possible NaturalComparator
Returns:
true if b is a NaturalComparator
Post:
returns true if b is a NaturalComparator