KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A {@link Comparator Comparator} that compares
23  * {@link Comparable Comparable} objects.
24  * <p />
25  * This Comparator is useful, for example,
26  * for enforcing the natural order in custom implementations
27  * of SortedSet and SortedMap.
28  * <p />
29  * Note: In the 2.0 and 2.1 releases of Commons Collections,
30  * this class would throw a {@link ClassCastException} if
31  * either of the arguments to {@link #compare(Object, Object) compare}
32  * were <code>null</code>, not {@link Comparable Comparable},
33  * or for which {@link Comparable#compareTo(Object) compareTo} gave
34  * inconsistent results. This is no longer the case. See
35  * {@link #compare(Object, Object) compare} for details.
36  *
37  * @since Commons Collections 2.0
38  * @version $Revision: 1.15 $ $Date: 2004/05/15 13:24:11 $
39  *
40  * @author Henri Yandell
41  *
42  * @see java.util.Collections#reverseOrder()
43  */

44 public class ComparableComparator implements Comparator JavaDoc, Serializable JavaDoc {
45
46     /** Serialization version. */
47     private static final long serialVersionUID=-291439688585137865L;
48
49     /** The singleton instance. */
50     private static final ComparableComparator instance = new ComparableComparator();
51
52     //-----------------------------------------------------------------------
53
/**
54      * Gets the singleton instance of a ComparableComparator.
55      * <p>
56      * Developers are encouraged to use the comparator returned from this method
57      * instead of constructing a new instance to reduce allocation and GC overhead
58      * when multiple comparable comparators may be used in the same VM.
59      *
60      * @return the singleton ComparableComparator
61      */

62     public static ComparableComparator getInstance() {
63         return instance;
64     }
65
66     //-----------------------------------------------------------------------
67
/**
68      * Constructor whose use should be avoided.
69      * <p>
70      * Please use the {@link #getInstance()} method whenever possible.
71      */

72     public ComparableComparator() {
73         super();
74     }
75
76     //-----------------------------------------------------------------------
77
/**
78      * Compare the two {@link Comparable Comparable} arguments.
79      * This method is equivalent to:
80      * <pre>((Comparable)obj1).compareTo(obj2)</pre>
81      *
82      * @param obj1 the first object to compare
83      * @param obj2 the second object to compare
84      * @return negative if obj1 is less, positive if greater, zero if equal
85      * @throws NullPointerException when <i>obj1</i> is <code>null</code>,
86      * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
87      * @throws ClassCastException when <i>obj1</i> is not a <code>Comparable</code>,
88      * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
89      */

90     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
91         return ((Comparable JavaDoc)obj1).compareTo(obj2);
92     }
93
94     //-----------------------------------------------------------------------
95
/**
96      * Implement a hash code for this comparator that is consistent with
97      * {@link #equals(Object) equals}.
98      *
99      * @return a hash code for this comparator.
100      * @since Commons Collections 3.0
101      */

102     public int hashCode() {
103         return "ComparableComparator".hashCode();
104     }
105
106     /**
107      * Returns <code>true</code> iff <i>that</i> Object is
108      * is a {@link Comparator Comparator} whose ordering is
109      * known to be equivalent to mine.
110      * <p>
111      * This implementation returns <code>true</code>
112      * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code>
113      * equals <code>this.getClass()</code>.
114      * Subclasses may want to override this behavior to remain consistent
115      * with the {@link Comparator#equals(Object)} contract.
116      *
117      * @param object the object to compare with
118      * @return true if equal
119      * @since Commons Collections 3.0
120      */

121     public boolean equals(Object JavaDoc object) {
122         return (this == object) ||
123                ((null != object) && (object.getClass().equals(this.getClass())));
124     }
125
126 }
127
Popular Tags