1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.comparators;
18
19 import java.io.Serializable;
20 import java.util.Comparator;
21 import java.util.SortedMap;
22 import java.util.SortedSet;
23
24 /**
25 * A {@link Comparator Comparator} that compares {@link Comparable Comparable}
26 * objects.
27 * <p>
28 * This Comparator is useful, for example, for enforcing the natural order in
29 * custom implementations of {@link SortedSet SortedSet} and
30 * {@link SortedMap SortedMap}.
31 * </p>
32 * <p>
33 * Note: In the 2.0 and 2.1 releases of Commons Collections, this class would
34 * throw a {@link ClassCastException} if either of the arguments to
35 * {@link #compare(Comparable, Comparable)} compare} were {@code null}, not
36 * {@link Comparable Comparable}, or for which
37 * {@link Comparable#compareTo(Object) compareTo} gave inconsistent results.
38 * This is no longer the case. See {@link #compare(Comparable, Comparable)} compare} for
39 * details.
40 * </p>
41 *
42 * @param <E> The type of objects compared by this comparator
43 * @since 2.0
44 * @see java.util.Collections#reverseOrder()
45 */
46 public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable {
47
48 /** Serialization version. */
49 private static final long serialVersionUID = -291439688585137865L;
50
51 /** The singleton instance. */
52 @SuppressWarnings("rawtypes")
53 public static final ComparableComparator INSTANCE = new ComparableComparator();
54
55 /**
56 * Gets the singleton instance of a ComparableComparator.
57 * <p>
58 * Developers are encouraged to use the comparator returned from this method
59 * instead of constructing a new instance to reduce allocation and GC overhead
60 * when multiple comparable comparators may be used in the same VM.
61 *
62 * @param <E> the element type
63 * @return The singleton ComparableComparator
64 * @since 4.0
65 */
66 public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator() {
67 return INSTANCE;
68 }
69
70 /**
71 * Constructor whose use should be avoided.
72 * <p>
73 * Please use the {@link #comparableComparator()} method whenever possible.
74 */
75 public ComparableComparator() {
76 }
77
78 /**
79 * Compare the two {@link Comparable Comparable} arguments.
80 * This method is equivalent to:
81 * <pre>((Comparable)obj1).compareTo(obj2)</pre>
82 *
83 * @param obj1 The first object to compare
84 * @param obj2 The second object to compare
85 * @return negative if obj1 is less, positive if greater, zero if equal
86 * @throws NullPointerException if <em>obj1</em> is {@code null},
87 * or when {@code ((Comparable)obj1).compareTo(obj2)} does
88 * @throws ClassCastException if <em>obj1</em> is not a {@code Comparable},
89 * or when {@code ((Comparable)obj1).compareTo(obj2)} does
90 */
91 @Override
92 public int compare(final E obj1, final E obj2) {
93 return obj1.compareTo(obj2);
94 }
95
96 /**
97 * Returns {@code true} iff <em>that</em> Object is a {@link Comparator Comparator}
98 * whose ordering is known to be equivalent to mine.
99 * <p>
100 * This implementation returns {@code true} iff
101 * {@code <em>object</em>.{@link Object#getClass() getClass()}} equals
102 * {@code this.getClass()}. Subclasses may want to override this behavior to remain
103 * consistent with the {@link Comparator#equals(Object)} contract.
104 *
105 * @param object The object to compare with
106 * @return {@code true} if equal
107 * @since 3.0
108 */
109 @Override
110 public boolean equals(final Object object) {
111 return this == object ||
112 object != null && object.getClass().equals(this.getClass());
113 }
114
115 /**
116 * Implement a hash code for this comparator that is consistent with
117 * {@link #equals(Object) equals}.
118 *
119 * @return A hash code for this comparator.
120 * @since 3.0
121 */
122 @Override
123 public int hashCode() {
124 return "ComparableComparator".hashCode();
125 }
126
127 }