| Comparator.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 package java.util;
9
10 /**
11 * A comparison function, which imposes a <i>total ordering</i> on some
12 * collection of objects. Comparators can be passed to a sort method (such
13 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
14 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
15 * over the sort order. Comparators can also be used to control the order of
16 * certain data structures (such as {@link SortedSet sorted sets} or {@link
17 * SortedMap sorted maps}), or to provide an ordering for collections of
18 * objects that don't have a {@link Comparable natural ordering}.<p>
19 *
20 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
21 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
22 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
23 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
24 * <tt>S</tt>.<p>
25 *
26 * Caution should be exercised when using a comparator capable of imposing an
27 * ordering inconsistent with equals to order a sorted set (or sorted map).
28 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
29 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
30 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
31 * the sorted set (or sorted map) will behave "strangely." In particular the
32 * sorted set (or sorted map) will violate the general contract for set (or
33 * map), which is defined in terms of <tt>equals</tt>.<p>
34 *
35 * For example, suppose one adds two elements {@code a} and {@code b} such that
36 * {@code (a.equals(b) && c.compare(a, b) != 0)}
37 * to an empty {@code TreeSet} with comparator {@code c}.
38 * The second {@code add} operation will return
39 * true (and the size of the tree set will increase) because {@code a} and
40 * {@code b} are not equivalent from the tree set's perspective, even though
41 * this is contrary to the specification of the
42 * {@link Set#add Set.add} method.<p>
43 *
44 * Note: It is generally a good idea for comparators to also implement
45 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
46 * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In
47 * order for the data structure to serialize successfully, the comparator (if
48 * provided) must implement <tt>Serializable</tt>.<p>
49 *
50 * For the mathematically inclined, the <i>relation</i> that defines the
51 * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
52 * given set of objects <tt>S</tt> is:<pre>
53 * {(x, y) such that c.compare(x, y) <= 0}.
54 * </pre> The <i>quotient</i> for this total order is:<pre>
55 * {(x, y) such that c.compare(x, y) == 0}.
56 * </pre>
57 *
58 * It follows immediately from the contract for <tt>compare</tt> that the
59 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
60 * imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that
61 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
62 * equals</i>, we mean that the quotient for the ordering is the equivalence
63 * relation defined by the objects' {@link Object#equals(Object)
64 * equals(Object)} method(s):<pre>
65 * {(x, y) such that x.equals(y)}. </pre><p>
66 *
67 * This interface is a member of the
68 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
69 * Java Collections Framework</a>.
70 *
71 * @param <T> the type of objects that may be compared by this comparator
72 *
73 * @author Josh Bloch
74 * @author Neal Gafter
75 * @version %I%, %G%
76 * @see Comparable
77 * @see java.io.Serializable
78 * @since 1.2
79 */
80
81 public interface Comparator<T> {
82 /**
83 * Compares its two arguments for order. Returns a negative integer,
84 * zero, or a positive integer as the first argument is less than, equal
85 * to, or greater than the second.<p>
86 *
87 * In the foregoing description, the notation
88 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
89 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
90 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
91 * <i>expression</i> is negative, zero or positive.<p>
92 *
93 * The implementor must ensure that <tt>sgn(compare(x, y)) ==
94 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
95 * implies that <tt>compare(x, y)</tt> must throw an exception if and only
96 * if <tt>compare(y, x)</tt> throws an exception.)<p>
97 *
98 * The implementor must also ensure that the relation is transitive:
99 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
100 * <tt>compare(x, z)>0</tt>.<p>
101 *
102 * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
103 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
104 * <tt>z</tt>.<p>
105 *
106 * It is generally the case, but <i>not</i> strictly required that
107 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
108 * any comparator that violates this condition should clearly indicate
109 * this fact. The recommended language is "Note: this comparator
110 * imposes orderings that are inconsistent with equals."
111 *
112 * @param o1 the first object to be compared.
113 * @param o2 the second object to be compared.
114 * @return a negative integer, zero, or a positive integer as the
115 * first argument is less than, equal to, or greater than the
116 * second.
117 * @throws ClassCastException if the arguments' types prevent them from
118 * being compared by this comparator.
119 */
120 int compare(T o1, T o2);
121
122 /**
123 *
124 * Indicates whether some other object is "equal to" this
125 * comparator. This method must obey the general contract of
126 * {@link Object#equals(Object)}. Additionally, this method can return
127 * <tt>true</tt> <i>only</i> if the specified object is also a comparator
128 * and it imposes the same ordering as this comparator. Thus,
129 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
130 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
131 * <tt>o1</tt> and <tt>o2</tt>.<p>
132 *
133 * Note that it is <i>always</i> safe <i>not</i> to override
134 * <tt>Object.equals(Object)</tt>. However, overriding this method may,
135 * in some cases, improve performance by allowing programs to determine
136 * that two distinct comparators impose the same order.
137 *
138 * @param obj the reference object with which to compare.
139 * @return <code>true</code> only if the specified object is also
140 * a comparator and it imposes the same ordering as this
141 * comparator.
142 * @see Object#equals(Object)
143 * @see Object#hashCode()
144 */
145 boolean equals(Object obj);
146 }
147