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