emul/mini/src/main/java/java/lang/Comparable.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
parent 55 23ed78656864
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
jaroslav@52
     1
/*
jaroslav@52
     2
 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
jaroslav@52
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@52
     4
 *
jaroslav@52
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@52
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@52
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@52
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@52
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@52
    10
 *
jaroslav@52
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@52
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@52
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@52
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@52
    15
 * accompanied this code).
jaroslav@52
    16
 *
jaroslav@52
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@52
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@52
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@52
    20
 *
jaroslav@52
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@52
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@52
    23
 * questions.
jaroslav@52
    24
 */
jaroslav@52
    25
jaroslav@52
    26
package java.lang;
jaroslav@52
    27
jaroslav@52
    28
/**
jaroslav@52
    29
 * This interface imposes a total ordering on the objects of each class that
jaroslav@52
    30
 * implements it.  This ordering is referred to as the class's <i>natural
jaroslav@52
    31
 * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
jaroslav@52
    32
 * its <i>natural comparison method</i>.<p>
jaroslav@52
    33
 *
jaroslav@52
    34
 * Lists (and arrays) of objects that implement this interface can be sorted
jaroslav@52
    35
 * automatically by {@link Collections#sort(List) Collections.sort} (and
jaroslav@52
    36
 * {@link Arrays#sort(Object[]) Arrays.sort}).  Objects that implement this
jaroslav@52
    37
 * interface can be used as keys in a {@linkplain SortedMap sorted map} or as
jaroslav@52
    38
 * elements in a {@linkplain SortedSet sorted set}, without the need to
jaroslav@52
    39
 * specify a {@linkplain Comparator comparator}.<p>
jaroslav@52
    40
 *
jaroslav@52
    41
 * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
jaroslav@52
    42
 * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
jaroslav@52
    43
 * the same boolean value as <tt>e1.equals(e2)</tt> for every
jaroslav@52
    44
 * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>.  Note that <tt>null</tt>
jaroslav@52
    45
 * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
jaroslav@52
    46
 * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
jaroslav@52
    47
 * returns <tt>false</tt>.<p>
jaroslav@52
    48
 *
jaroslav@52
    49
 * It is strongly recommended (though not required) that natural orderings be
jaroslav@52
    50
 * consistent with equals.  This is so because sorted sets (and sorted maps)
jaroslav@52
    51
 * without explicit comparators behave "strangely" when they are used with
jaroslav@52
    52
 * elements (or keys) whose natural ordering is inconsistent with equals.  In
jaroslav@52
    53
 * particular, such a sorted set (or sorted map) violates the general contract
jaroslav@52
    54
 * for set (or map), which is defined in terms of the <tt>equals</tt>
jaroslav@52
    55
 * method.<p>
jaroslav@52
    56
 *
jaroslav@52
    57
 * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
jaroslav@52
    58
 * <tt>(!a.equals(b) && a.compareTo(b) == 0)</tt> to a sorted
jaroslav@52
    59
 * set that does not use an explicit comparator, the second <tt>add</tt>
jaroslav@52
    60
 * operation returns false (and the size of the sorted set does not increase)
jaroslav@52
    61
 * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
jaroslav@52
    62
 * perspective.<p>
jaroslav@52
    63
 *
jaroslav@52
    64
 * Virtually all Java core classes that implement <tt>Comparable</tt> have natural
jaroslav@52
    65
 * orderings that are consistent with equals.  One exception is
jaroslav@52
    66
 * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
jaroslav@52
    67
 * <tt>BigDecimal</tt> objects with equal values and different precisions
jaroslav@52
    68
 * (such as 4.0 and 4.00).<p>
jaroslav@52
    69
 *
jaroslav@52
    70
 * For the mathematically inclined, the <i>relation</i> that defines
jaroslav@52
    71
 * the natural ordering on a given class C is:<pre>
jaroslav@52
    72
 *       {(x, y) such that x.compareTo(y) &lt;= 0}.
jaroslav@52
    73
 * </pre> The <i>quotient</i> for this total order is: <pre>
jaroslav@52
    74
 *       {(x, y) such that x.compareTo(y) == 0}.
jaroslav@52
    75
 * </pre>
jaroslav@52
    76
 *
jaroslav@52
    77
 * It follows immediately from the contract for <tt>compareTo</tt> that the
jaroslav@52
    78
 * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
jaroslav@52
    79
 * natural ordering is a <i>total order</i> on <tt>C</tt>.  When we say that a
jaroslav@52
    80
 * class's natural ordering is <i>consistent with equals</i>, we mean that the
jaroslav@52
    81
 * quotient for the natural ordering is the equivalence relation defined by
jaroslav@52
    82
 * the class's {@link Object#equals(Object) equals(Object)} method:<pre>
jaroslav@52
    83
 *     {(x, y) such that x.equals(y)}. </pre><p>
jaroslav@52
    84
 *
jaroslav@52
    85
 * This interface is a member of the
jaroslav@52
    86
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@52
    87
 * Java Collections Framework</a>.
jaroslav@52
    88
 *
jaroslav@52
    89
 * @param <T> the type of objects that this object may be compared to
jaroslav@52
    90
 *
jaroslav@52
    91
 * @author  Josh Bloch
jaroslav@52
    92
 * @see java.util.Comparator
jaroslav@52
    93
 * @since 1.2
jaroslav@52
    94
 */
jaroslav@52
    95
jaroslav@52
    96
public interface Comparable<T> {
jaroslav@52
    97
    /**
jaroslav@52
    98
     * Compares this object with the specified object for order.  Returns a
jaroslav@52
    99
     * negative integer, zero, or a positive integer as this object is less
jaroslav@52
   100
     * than, equal to, or greater than the specified object.
jaroslav@52
   101
     *
jaroslav@52
   102
     * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
jaroslav@52
   103
     * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
jaroslav@52
   104
     * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
jaroslav@52
   105
     * <tt>y.compareTo(x)</tt> throws an exception.)
jaroslav@52
   106
     *
jaroslav@52
   107
     * <p>The implementor must also ensure that the relation is transitive:
jaroslav@52
   108
     * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
jaroslav@52
   109
     * <tt>x.compareTo(z)&gt;0</tt>.
jaroslav@52
   110
     *
jaroslav@52
   111
     * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
jaroslav@52
   112
     * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
jaroslav@52
   113
     * all <tt>z</tt>.
jaroslav@52
   114
     *
jaroslav@52
   115
     * <p>It is strongly recommended, but <i>not</i> strictly required that
jaroslav@52
   116
     * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
jaroslav@52
   117
     * class that implements the <tt>Comparable</tt> interface and violates
jaroslav@52
   118
     * this condition should clearly indicate this fact.  The recommended
jaroslav@52
   119
     * language is "Note: this class has a natural ordering that is
jaroslav@52
   120
     * inconsistent with equals."
jaroslav@52
   121
     *
jaroslav@52
   122
     * <p>In the foregoing description, the notation
jaroslav@52
   123
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
jaroslav@52
   124
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
jaroslav@52
   125
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
jaroslav@52
   126
     * <i>expression</i> is negative, zero or positive.
jaroslav@52
   127
     *
jaroslav@52
   128
     * @param   o the object to be compared.
jaroslav@52
   129
     * @return  a negative integer, zero, or a positive integer as this object
jaroslav@52
   130
     *          is less than, equal to, or greater than the specified object.
jaroslav@52
   131
     *
jaroslav@52
   132
     * @throws NullPointerException if the specified object is null
jaroslav@52
   133
     * @throws ClassCastException if the specified object's type prevents it
jaroslav@52
   134
     *         from being compared to this object.
jaroslav@52
   135
     */
jaroslav@52
   136
    public int compareTo(T o);
jaroslav@52
   137
}