|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectstructure5.ReverseComparator<E>
public class ReverseComparator<E extends java.lang.Comparable<E>>
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);
| Field Summary | |
|---|---|
protected java.util.Comparator<E> |
base
|
| Constructor Summary | |
|---|---|
ReverseComparator()
Construct a comparator that generates reverse natural comparison |
|
ReverseComparator(java.util.Comparator<E> base)
Construct a comparator that generates reverse of another comparator |
|
| Method Summary | |
|---|---|
int |
compare(E a,
E 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 |
|---|
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected java.util.Comparator<E extends java.lang.Comparable<E>> base
| Constructor Detail |
|---|
public ReverseComparator()
public ReverseComparator(java.util.Comparator<E> base)
base - the ordering to be reversed| Method Detail |
|---|
public int compare(E a,
E b)
compare in interface java.util.Comparator<E extends java.lang.Comparable<E>>a - object performing the compareb - the object being compared
public boolean equals(java.lang.Object b)
equals in interface java.util.Comparator<E extends java.lang.Comparable<E>>equals in class java.lang.Objectb - a possible NaturalComparator
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||