001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.comparators;
017:
018: import java.io.Serializable;
019: import java.util.Comparator;
020:
021: /**
022: * A {@link Comparator Comparator} that compares
023: * {@link Comparable Comparable} objects.
024: * <p />
025: * This Comparator is useful, for example,
026: * for enforcing the natural order in custom implementations
027: * of SortedSet and SortedMap.
028: * <p />
029: * Note: In the 2.0 and 2.1 releases of Commons Collections,
030: * this class would throw a {@link ClassCastException} if
031: * either of the arguments to {@link #compare(Object, Object) compare}
032: * were <code>null</code>, not {@link Comparable Comparable},
033: * or for which {@link Comparable#compareTo(Object) compareTo} gave
034: * inconsistent results. This is no longer the case. See
035: * {@link #compare(Object, Object) compare} for details.
036: *
037: * @since Commons Collections 2.0
038: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
039: *
040: * @author Henri Yandell
041: *
042: * @see java.util.Collections#reverseOrder()
043: */
044: public class ComparableComparator implements Comparator, Serializable {
045:
046: /** Serialization version. */
047: private static final long serialVersionUID = -291439688585137865L;
048:
049: /** The singleton instance. */
050: private static final ComparableComparator instance = new ComparableComparator();
051:
052: //-----------------------------------------------------------------------
053: /**
054: * Gets the singleton instance of a ComparableComparator.
055: * <p>
056: * Developers are encouraged to use the comparator returned from this method
057: * instead of constructing a new instance to reduce allocation and GC overhead
058: * when multiple comparable comparators may be used in the same VM.
059: *
060: * @return the singleton ComparableComparator
061: */
062: public static ComparableComparator getInstance() {
063: return instance;
064: }
065:
066: //-----------------------------------------------------------------------
067: /**
068: * Constructor whose use should be avoided.
069: * <p>
070: * Please use the {@link #getInstance()} method whenever possible.
071: */
072: public ComparableComparator() {
073: super ();
074: }
075:
076: //-----------------------------------------------------------------------
077: /**
078: * Compare the two {@link Comparable Comparable} arguments.
079: * This method is equivalent to:
080: * <pre>((Comparable)obj1).compareTo(obj2)</pre>
081: *
082: * @param obj1 the first object to compare
083: * @param obj2 the second object to compare
084: * @return negative if obj1 is less, positive if greater, zero if equal
085: * @throws NullPointerException when <i>obj1</i> is <code>null</code>,
086: * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
087: * @throws ClassCastException when <i>obj1</i> is not a <code>Comparable</code>,
088: * or when <code>((Comparable)obj1).compareTo(obj2)</code> does
089: */
090: public int compare(Object obj1, Object obj2) {
091: return ((Comparable) obj1).compareTo(obj2);
092: }
093:
094: //-----------------------------------------------------------------------
095: /**
096: * Implement a hash code for this comparator that is consistent with
097: * {@link #equals(Object) equals}.
098: *
099: * @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 object) {
122: return (this == object)
123: || ((null != object) && (object.getClass().equals(this
124: .getClass())));
125: }
126:
127: }
|