jdk/src/share/classes/java/util/Comparator.java
author yhuang
Wed, 14 Mar 2012 23:07:21 -0700
changeset 12135 078775fa4b30
parent 5506 202f599c92aa
child 15647 314007859004
permissions -rw-r--r--
7085757: Currency Data: ISO 4217 Amendment 152 Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A comparison function, which imposes a <i>total ordering</i> on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * collection of objects.  Comparators can be passed to a sort method (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * over the sort order.  Comparators can also be used to control the order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * certain data structures (such as {@link SortedSet sorted sets} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * SortedMap sorted maps}), or to provide an ordering for collections of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * objects that don't have a {@link Comparable natural ordering}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <tt>S</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Caution should be exercised when using a comparator capable of imposing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * ordering inconsistent with equals to order a sorted set (or sorted map).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * is used with elements (or keys) drawn from a set <tt>S</tt>.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the sorted set (or sorted map) will behave "strangely."  In particular the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * sorted set (or sorted map) will violate the general contract for set (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * map), which is defined in terms of <tt>equals</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * For example, suppose one adds two elements {@code a} and {@code b} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@code (a.equals(b) && c.compare(a, b) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to an empty {@code TreeSet} with comparator {@code c}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The second {@code add} operation will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * true (and the size of the tree set will increase) because {@code a} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@code b} are not equivalent from the tree set's perspective, even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * this is contrary to the specification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * {@link Set#add Set.add} method.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Note: It is generally a good idea for comparators to also implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * serializable data structures (like {@link TreeSet}, {@link TreeMap}).  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * order for the data structure to serialize successfully, the comparator (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * provided) must implement <tt>Serializable</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * For the mathematically inclined, the <i>relation</i> that defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * given set of objects <tt>S</tt> is:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *       {(x, y) such that c.compare(x, y) &lt;= 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre> The <i>quotient</i> for this total order is:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *       {(x, y) such that c.compare(x, y) == 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * It follows immediately from the contract for <tt>compare</tt> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * imposed ordering is a <i>total order</i> on <tt>S</tt>.  When we say that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * equals</i>, we mean that the quotient for the ordering is the equivalence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * relation defined by the objects' {@link Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * equals(Object)} method(s):<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     {(x, y) such that x.equals(y)}. </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>Unlike {@code Comparable}, a comparator may optionally permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * comparison of null arguments, while maintaining the requirements for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * an equivalence relation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>This interface is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @param <T> the type of objects that may be compared by this comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @see java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
public interface Comparator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Compares its two arguments for order.  Returns a negative integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * zero, or a positive integer as the first argument is less than, equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to, or greater than the second.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * In the foregoing description, the notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <i>expression</i> is negative, zero or positive.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The implementor must ensure that <tt>sgn(compare(x, y)) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * implies that <tt>compare(x, y)</tt> must throw an exception if and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * if <tt>compare(y, x)</tt> throws an exception.)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The implementor must also ensure that the relation is transitive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <tt>compare(x, z)&gt;0</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <tt>z</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * It is generally the case, but <i>not</i> strictly required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * any comparator that violates this condition should clearly indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * this fact.  The recommended language is "Note: this comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * imposes orderings that are inconsistent with equals."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param o1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param o2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         first argument is less than, equal to, or greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws NullPointerException if an argument is null and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         comparator does not permit null arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws ClassCastException if the arguments' types prevent them from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         being compared by this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    int compare(T o1, T o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Indicates whether some other object is &quot;equal to&quot; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * comparator.  This method must obey the general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@link Object#equals(Object)}.  Additionally, this method can return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <tt>true</tt> <i>only</i> if the specified object is also a comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * and it imposes the same ordering as this comparator.  Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <tt>o1</tt> and <tt>o2</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Note that it is <i>always</i> safe <i>not</i> to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <tt>Object.equals(Object)</tt>.  However, overriding this method may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * in some cases, improve performance by allowing programs to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * that two distinct comparators impose the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param   obj   the reference object with which to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return  <code>true</code> only if the specified object is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          a comparator and it imposes the same ordering as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    boolean equals(Object obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}