View Javadoc

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 net.sf.collections15.comparators;
17  
18  import java.io.Serializable;
19  import java.util.Collections;
20  import java.util.Comparator;
21  
22  /***
23   * Decorator <code>Comparator</code> that reverses the order of another
24   * <code>Comparator</code> by reversing the arguments to its {@link
25   * #compare(Object, Object) compare} method.
26   *
27   * @author Henri Yandell
28   * @author Michael A. Smith
29   * @author Chris Lambrou (port to Java 5.0)
30   * @see java.util.Collections#reverseOrder()
31   * @since Collections15 1.0
32   */
33  public class ReverseComparator <E> implements Comparator<E>, Serializable
34  {
35  
36      static final long serialVersionUID = -2606627357111779957L;
37  
38      /***
39       * The comparator being decorated.
40       */
41      private Comparator<E> comparator;
42  
43      /***
44       * Returns a <code>Comparator</code> that compares <code>Comparable</code>
45       * objects based on the inverse of their natural ordering.
46       *
47       * @see Collections#reverseOrder()
48       */
49      public static <T extends Comparable> ReverseComparator<T> getInstance()
50      {
51          Comparator<T> comparator = ComparableComparator.getInstance();
52          return new ReverseComparator<T>(comparator);
53      }
54  
55      /***
56       * Decorates the specified <code>Comparator</code> by returning
57       * a<code>Comparator</code> instance which reverses its sort order..
58       *
59       * @see Collections#reverseOrder()
60       */
61      public static <T> ReverseComparator<T> decorate(Comparator<T> comparator)
62      {
63          return new ReverseComparator<T>(comparator);
64      }
65  
66      /***
67       * Creates a comparator that inverts the comparison of the given
68       * comparator.
69       *
70       * @param comparator Comparator to reverse. Not null.
71       *
72       * @throws IllegalArgumentException Thrown if the specified <code>Comparator</code>
73       *                                  is <code>null</code>.
74       */
75      protected ReverseComparator(Comparator<E> comparator)
76      {
77          if (comparator == null) {
78              throw new IllegalArgumentException("null comparator not allowed");
79          }
80          this.comparator = comparator;
81      }
82  
83      /***
84       * Compares two objects in reverse order.
85       *
86       * @param obj1 The first object to compare
87       * @param obj2 The second object to compare
88       *
89       * @return A negative value if <code>obj1</code> is less than
90       *         <code>obj2</code>. A positive value if <code>obj1</code> is
91       *         greater than <code>obj2</code>. Zero if the two objects are
92       *         equivalent with respect to the ordering.
93       */
94      public int compare(E obj1, E obj2)
95      {
96          return comparator.compare(obj2, obj1);
97      }
98  
99      /***
100      * Returns a hash code for this comparator that is consistent with {@link
101      * #equals(Object) equals}.
102      *
103      * @return a suitable hash code
104      *
105      * @since Commons Collections 3.0
106      */
107     public int hashCode()
108     {
109         return "ReverseComparator".hashCode() ^ comparator.hashCode(); //@todo: This is stupid!!
110     }
111 
112     /***
113      * Returns <code>true</code> if the specified object is a
114      * <code>Comparator</code> whose ordering is equivalent to this
115      * <code>Comparator</code>.
116      * <p/>
117      * This implementation returns <code>true</code> if <code><i>object</i>.getClass()</code>
118      * equals <code>this.getClass()</code>, and the underlying
119      * <code>Comparators</code> are equal. Subclasses may want to override this
120      * behavior to remain consistent with the {@link Comparator#equals(Object)
121      * equals} contract.
122      *
123      * @param object The object to compare to
124      *
125      * @return <code>true</code> if the object is equal to this object.
126      *
127      * @since Collections15 1.0
128      */
129     public boolean equals(Object object)
130     {
131         if (this == object) {
132             return true;
133         }
134         else if (null == object) {
135             return false;
136         }
137         else if (object.getClass().equals(this.getClass())) {
138             ReverseComparator thatrc = (ReverseComparator) object;
139             return comparator.equals(thatrc.comparator);
140         }
141         else {
142             return false;
143         }
144     }
145 
146 }