jdk/src/share/classes/java/lang/Comparable.java
author anthony
Tue, 05 May 2009 17:56:31 +0400
changeset 3444 18840bd1c784
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6818787: It is possible to reposition the security icon too far from the border of the window on X11 Summary: The constraints for the position of the icon are moved to the shared code Reviewed-by: art, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This interface imposes a total ordering on the objects of each class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * implements it.  This ordering is referred to as the class's <i>natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * its <i>natural comparison method</i>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Lists (and arrays) of objects that implement this interface can be sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * automatically by {@link Collections#sort(List) Collections.sort} (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@link Arrays#sort(Object[]) Arrays.sort}).  Objects that implement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * interface can be used as keys in a {@linkplain SortedMap sorted map} or as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * elements in a {@linkplain SortedSet sorted set}, without the need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * specify a {@linkplain Comparator comparator}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the same boolean value as <tt>e1.equals(e2)</tt> for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>.  Note that <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * returns <tt>false</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * It is strongly recommended (though not required) that natural orderings be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * consistent with equals.  This is so because sorted sets (and sorted maps)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * without explicit comparators behave "strangely" when they are used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * elements (or keys) whose natural ordering is inconsistent with equals.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * particular, such a sorted set (or sorted map) violates the general contract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * for set (or map), which is defined in terms of the <tt>equals</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * method.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <tt>(!a.equals(b) && a.compareTo(b) == 0)</tt> to a sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * set that does not use an explicit comparator, the second <tt>add</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * operation returns false (and the size of the sorted set does not increase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * perspective.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Virtually all Java core classes that implement <tt>Comparable</tt> have natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * orderings that are consistent with equals.  One exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tt>BigDecimal</tt> objects with equal values and different precisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * (such as 4.0 and 4.00).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * For the mathematically inclined, the <i>relation</i> that defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * the natural ordering on a given class C is:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *       {(x, y) such that x.compareTo(y) &lt;= 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre> The <i>quotient</i> for this total order is: <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *       {(x, y) such that x.compareTo(y) == 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * It follows immediately from the contract for <tt>compareTo</tt> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * natural ordering is a <i>total order</i> on <tt>C</tt>.  When we say that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * class's natural ordering is <i>consistent with equals</i>, we mean that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * quotient for the natural ordering is the equivalence relation defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the class's {@link Object#equals(Object) equals(Object)} method:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     {(x, y) such that x.equals(y)}. </pre><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * This interface is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @param <T> the type of objects that this object may be compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @see java.util.Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public interface Comparable<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Compares this object with the specified object for order.  Returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * negative integer, zero, or a positive integer as this object is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * than, equal to, or greater than the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <tt>y.compareTo(x)</tt> throws an exception.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>The implementor must also ensure that the relation is transitive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <tt>x.compareTo(z)&gt;0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * all <tt>z</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>It is strongly recommended, but <i>not</i> strictly required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * class that implements the <tt>Comparable</tt> interface and violates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * this condition should clearly indicate this fact.  The recommended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * language is "Note: this class has a natural ordering that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * inconsistent with equals."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>In the foregoing description, the notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <i>expression</i> is negative, zero or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param   o the object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return  a negative integer, zero, or a positive integer as this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *          is less than, equal to, or greater than the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws NullPointerException if the specified object is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws ClassCastException if the specified object's type prevents it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         from being compared to this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public int compareTo(T o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}