KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > comparators > ReverseComparator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.comparators;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Comparator JavaDoc;
20
21 /**
22  * Reverses the order of another comparator by reversing the arguments
23  * to its {@link #compare(Object, Object) compare} method.
24  *
25  * @since Commons Collections 2.0
26  * @version $Revision: 1.20 $ $Date: 2004/05/16 11:49:24 $
27  *
28  * @author Henri Yandell
29  * @author Michael A. Smith
30  *
31  * @see java.util.Collections#reverseOrder()
32  */

33 public class ReverseComparator implements Comparator JavaDoc, Serializable JavaDoc {
34
35     /** Serialization version from Collections 2.0. */
36     private static final long serialVersionUID = 2858887242028539265L;
37
38     /** The comparator being decorated. */
39     private Comparator JavaDoc comparator;
40
41     //-----------------------------------------------------------------------
42
/**
43      * Creates a comparator that compares objects based on the inverse of their
44      * natural ordering. Using this Constructor will create a ReverseComparator
45      * that is functionally identical to the Comparator returned by
46      * java.util.Collections.<b>reverseOrder()</b>.
47      *
48      * @see java.util.Collections#reverseOrder()
49      */

50     public ReverseComparator() {
51         this(null);
52     }
53
54     /**
55      * Creates a comparator that inverts the comparison
56      * of the given comparator. If you pass in <code>null</code>,
57      * the ReverseComparator defaults to reversing the
58      * natural order, as per
59      * {@link java.util.Collections#reverseOrder()}</b>.
60      *
61      * @param comparator Comparator to reverse
62      */

63     public ReverseComparator(Comparator JavaDoc comparator) {
64         if(comparator != null) {
65             this.comparator = comparator;
66         } else {
67             this.comparator = ComparableComparator.getInstance();
68         }
69     }
70
71     //-----------------------------------------------------------------------
72
/**
73      * Compares two objects in reverse order.
74      *
75      * @param obj1 the first object to compare
76      * @param obj2 the second object to compare
77      * @return negative if obj1 is less, positive if greater, zero if equal
78      */

79     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
80         return comparator.compare(obj2, obj1);
81     }
82
83     //-----------------------------------------------------------------------
84
/**
85      * Implement a hash code for this comparator that is consistent with
86      * {@link #equals(Object) equals}.
87      *
88      * @return a suitable hash code
89      * @since Commons Collections 3.0
90      */

91     public int hashCode() {
92         return "ReverseComparator".hashCode() ^ comparator.hashCode();
93     }
94
95     /**
96      * Returns <code>true</code> iff <i>that</i> Object is
97      * is a {@link Comparator} whose ordering is known to be
98      * equivalent to mine.
99      * <p>
100      * This implementation returns <code>true</code>
101      * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
102      * equals <code>this.getClass()</code>, and the underlying
103      * comparators are equal.
104      * Subclasses may want to override this behavior to remain consistent
105      * with the {@link Comparator#equals(Object) equals} contract.
106      *
107      * @param object the object to compare to
108      * @return true if equal
109      * @since Commons Collections 3.0
110      */

111     public boolean equals(Object JavaDoc object) {
112         if(this == object) {
113             return true;
114         } else if(null == object) {
115             return false;
116         } else if(object.getClass().equals(this.getClass())) {
117             ReverseComparator thatrc = (ReverseComparator)object;
118             return comparator.equals(thatrc.comparator);
119         } else {
120             return false;
121         }
122     }
123
124 }
125
Popular Tags