001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util;
027
028 /**
029 * A comparison function, which imposes a <i>total ordering</i> on some
030 * collection of objects. Comparators can be passed to a sort method (such
031 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
032 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
033 * over the sort order. Comparators can also be used to control the order of
034 * certain data structures (such as {@link SortedSet sorted sets} or {@link
035 * SortedMap sorted maps}), or to provide an ordering for collections of
036 * objects that don't have a {@link Comparable natural ordering}.<p>
037 *
038 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
039 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
040 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
041 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
042 * <tt>S</tt>.<p>
043 *
044 * Caution should be exercised when using a comparator capable of imposing an
045 * ordering inconsistent with equals to order a sorted set (or sorted map).
046 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
047 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
048 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
049 * the sorted set (or sorted map) will behave "strangely." In particular the
050 * sorted set (or sorted map) will violate the general contract for set (or
051 * map), which is defined in terms of <tt>equals</tt>.<p>
052 *
053 * For example, suppose one adds two elements {@code a} and {@code b} such that
054 * {@code (a.equals(b) && c.compare(a, b) != 0)}
055 * to an empty {@code TreeSet} with comparator {@code c}.
056 * The second {@code add} operation will return
057 * true (and the size of the tree set will increase) because {@code a} and
058 * {@code b} are not equivalent from the tree set's perspective, even though
059 * this is contrary to the specification of the
060 * {@link Set#add Set.add} method.<p>
061 *
062 * Note: It is generally a good idea for comparators to also implement
063 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
064 * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In
065 * order for the data structure to serialize successfully, the comparator (if
066 * provided) must implement <tt>Serializable</tt>.<p>
067 *
068 * For the mathematically inclined, the <i>relation</i> that defines the
069 * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
070 * given set of objects <tt>S</tt> is:<pre>
071 * {(x, y) such that c.compare(x, y) <= 0}.
072 * </pre> The <i>quotient</i> for this total order is:<pre>
073 * {(x, y) such that c.compare(x, y) == 0}.
074 * </pre>
075 *
076 * It follows immediately from the contract for <tt>compare</tt> that the
077 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
078 * imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that
079 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
080 * equals</i>, we mean that the quotient for the ordering is the equivalence
081 * relation defined by the objects' {@link Object#equals(Object)
082 * equals(Object)} method(s):<pre>
083 * {(x, y) such that x.equals(y)}. </pre>
084 *
085 * <p>Unlike {@code Comparable}, a comparator may optionally permit
086 * comparison of null arguments, while maintaining the requirements for
087 * an equivalence relation.
088 *
089 * <p>This interface is a member of the
090 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
091 * Java Collections Framework</a>.
092 *
093 * @param <T> the type of objects that may be compared by this comparator
094 *
095 * @author Josh Bloch
096 * @author Neal Gafter
097 * @version 1.33, 06/12/07
098 * @see Comparable
099 * @see java.io.Serializable
100 * @since 1.2
101 */
102
103 public interface Comparator<T> {
104 /**
105 * Compares its two arguments for order. Returns a negative integer,
106 * zero, or a positive integer as the first argument is less than, equal
107 * to, or greater than the second.<p>
108 *
109 * In the foregoing description, the notation
110 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
111 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
112 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
113 * <i>expression</i> is negative, zero or positive.<p>
114 *
115 * The implementor must ensure that <tt>sgn(compare(x, y)) ==
116 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
117 * implies that <tt>compare(x, y)</tt> must throw an exception if and only
118 * if <tt>compare(y, x)</tt> throws an exception.)<p>
119 *
120 * The implementor must also ensure that the relation is transitive:
121 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
122 * <tt>compare(x, z)>0</tt>.<p>
123 *
124 * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
125 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
126 * <tt>z</tt>.<p>
127 *
128 * It is generally the case, but <i>not</i> strictly required that
129 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
130 * any comparator that violates this condition should clearly indicate
131 * this fact. The recommended language is "Note: this comparator
132 * imposes orderings that are inconsistent with equals."
133 *
134 * @param o1 the first object to be compared.
135 * @param o2 the second object to be compared.
136 * @return a negative integer, zero, or a positive integer as the
137 * first argument is less than, equal to, or greater than the
138 * second.
139 * @throws NullPointerException if an argument is null and this
140 * comparator does not permit null arguments
141 * @throws ClassCastException if the arguments' types prevent them from
142 * being compared by this comparator.
143 */
144 int compare(T o1, T o2);
145
146 /**
147 * Indicates whether some other object is "equal to" this
148 * comparator. This method must obey the general contract of
149 * {@link Object#equals(Object)}. Additionally, this method can return
150 * <tt>true</tt> <i>only</i> if the specified object is also a comparator
151 * and it imposes the same ordering as this comparator. Thus,
152 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
153 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
154 * <tt>o1</tt> and <tt>o2</tt>.<p>
155 *
156 * Note that it is <i>always</i> safe <i>not</i> to override
157 * <tt>Object.equals(Object)</tt>. However, overriding this method may,
158 * in some cases, improve performance by allowing programs to determine
159 * that two distinct comparators impose the same order.
160 *
161 * @param obj the reference object with which to compare.
162 * @return <code>true</code> only if the specified object is also
163 * a comparator and it imposes the same ordering as this
164 * comparator.
165 * @see Object#equals(Object)
166 * @see Object#hashCode()
167 */
168 boolean equals(Object obj);
169 }
|