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.lang;
027
028 import java.util.*;
029
030 /**
031 * This interface imposes a total ordering on the objects of each class that
032 * implements it. This ordering is referred to as the class's <i>natural
033 * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
034 * its <i>natural comparison method</i>.<p>
035 *
036 * Lists (and arrays) of objects that implement this interface can be sorted
037 * automatically by {@link Collections#sort(List) Collections.sort} (and
038 * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this
039 * interface can be used as keys in a {@linkplain SortedMap sorted map} or as
040 * elements in a {@linkplain SortedSet sorted set}, without the need to
041 * specify a {@linkplain Comparator comparator}.<p>
042 *
043 * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
044 * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
045 * the same boolean value as <tt>e1.equals(e2)</tt> for every
046 * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>
047 * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
048 * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
049 * returns <tt>false</tt>.<p>
050 *
051 * It is strongly recommended (though not required) that natural orderings be
052 * consistent with equals. This is so because sorted sets (and sorted maps)
053 * without explicit comparators behave "strangely" when they are used with
054 * elements (or keys) whose natural ordering is inconsistent with equals. In
055 * particular, such a sorted set (or sorted map) violates the general contract
056 * for set (or map), which is defined in terms of the <tt>equals</tt>
057 * method.<p>
058 *
059 * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
060 * <tt>(!a.equals(b) && a.compareTo(b) == 0)</tt> to a sorted
061 * set that does not use an explicit comparator, the second <tt>add</tt>
062 * operation returns false (and the size of the sorted set does not increase)
063 * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
064 * perspective.<p>
065 *
066 * Virtually all Java core classes that implement <tt>Comparable</tt> have natural
067 * orderings that are consistent with equals. One exception is
068 * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
069 * <tt>BigDecimal</tt> objects with equal values and different precisions
070 * (such as 4.0 and 4.00).<p>
071 *
072 * For the mathematically inclined, the <i>relation</i> that defines
073 * the natural ordering on a given class C is:<pre>
074 * {(x, y) such that x.compareTo(y) <= 0}.
075 * </pre> The <i>quotient</i> for this total order is: <pre>
076 * {(x, y) such that x.compareTo(y) == 0}.
077 * </pre>
078 *
079 * It follows immediately from the contract for <tt>compareTo</tt> that the
080 * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
081 * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
082 * class's natural ordering is <i>consistent with equals</i>, we mean that the
083 * quotient for the natural ordering is the equivalence relation defined by
084 * the class's {@link Object#equals(Object) equals(Object)} method:<pre>
085 * {(x, y) such that x.equals(y)}. </pre><p>
086 *
087 * This interface is a member of the
088 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
089 * Java Collections Framework</a>.
090 *
091 * @param <T> the type of objects that this object may be compared to
092 *
093 * @author Josh Bloch
094 * @version 1.33, 06/12/07
095 * @see java.util.Comparator
096 * @since 1.2
097 */
098
099 public interface Comparable<T> {
100 /**
101 * Compares this object with the specified object for order. Returns a
102 * negative integer, zero, or a positive integer as this object is less
103 * than, equal to, or greater than the specified object.
104 *
105 * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
106 * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
107 * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
108 * <tt>y.compareTo(x)</tt> throws an exception.)
109 *
110 * <p>The implementor must also ensure that the relation is transitive:
111 * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
112 * <tt>x.compareTo(z)>0</tt>.
113 *
114 * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
115 * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
116 * all <tt>z</tt>.
117 *
118 * <p>It is strongly recommended, but <i>not</i> strictly required that
119 * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
120 * class that implements the <tt>Comparable</tt> interface and violates
121 * this condition should clearly indicate this fact. The recommended
122 * language is "Note: this class has a natural ordering that is
123 * inconsistent with equals."
124 *
125 * <p>In the foregoing description, the notation
126 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
127 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
128 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
129 * <i>expression</i> is negative, zero or positive.
130 *
131 * @param o the object to be compared.
132 * @return a negative integer, zero, or a positive integer as this object
133 * is less than, equal to, or greater than the specified object.
134 *
135 * @throws NullPointerException if the specified object is null
136 * @throws ClassCastException if the specified object's type prevents it
137 * from being compared to this object.
138 */
139 public int compareTo(T o);
140 }
|