1/*2* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/25 26packagejava.lang; 27importjava.util.*; 28 29/**30* This interface imposes a total ordering on the objects of each class that31* implements it. This ordering is referred to as the class's <i>natural32* ordering</i>, and the class's <tt>compareTo</tt> method is referred to as33* its <i>natural comparison method</i>.<p>34*35* Lists (and arrays) of objects that implement this interface can be sorted36* automatically by {@link Collections#sort(List) Collections.sort} (and37* {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this38* interface can be used as keys in a {@linkplain SortedMap sorted map} or as39* elements in a {@linkplain SortedSet sorted set}, without the need to40* specify a {@linkplain Comparator comparator}.<p>41*42* The natural ordering for a class <tt>C</tt> is said to be <i>consistent43* with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has44* the same boolean value as <tt>e1.equals(e2)</tt> for every45* <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>46* is not an instance of any class, and <tt>e.compareTo(null)</tt> should47* throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>48* returns <tt>false</tt>.<p>49*50* It is strongly recommended (though not required) that natural orderings be51* consistent with equals. This is so because sorted sets (and sorted maps)52* without explicit comparators behave "strangely" when they are used with53* elements (or keys) whose natural ordering is inconsistent with equals. In54* particular, such a sorted set (or sorted map) violates the general contract55* for set (or map), which is defined in terms of the <tt>equals</tt>56* method.<p>57*58* For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that59* {@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted60* set that does not use an explicit comparator, the second <tt>add</tt>61* operation returns false (and the size of the sorted set does not increase)62* because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's63* perspective.<p>64*65* Virtually all Java core classes that implement <tt>Comparable</tt> have natural66* orderings that are consistent with equals. One exception is67* <tt>java.math.BigDecimal</tt>, whose natural ordering equates68* <tt>BigDecimal</tt> objects with equal values and different precisions69* (such as 4.0 and 4.00).<p>70*71* For the mathematically inclined, the <i>relation</i> that defines72* the natural ordering on a given class C is:<pre>73* {(x, y) such that x.compareTo(y) <= 0}.74* </pre> The <i>quotient</i> for this total order is: <pre>75* {(x, y) such that x.compareTo(y) == 0}.76* </pre>77*78* It follows immediately from the contract for <tt>compareTo</tt> that the79* quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the80* natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a81* class's natural ordering is <i>consistent with equals</i>, we mean that the82* quotient for the natural ordering is the equivalence relation defined by83* the class's {@link Object#equals(Object) equals(Object)} method:<pre>84* {(x, y) such that x.equals(y)}. </pre><p>85*86* This interface is a member of the87* <a href="{@docRoot}/../technotes/guides/collections/index.html">88* Java Collections Framework</a>.89*90* @param <T> the type of objects that this object may be compared to91*92* @author Josh Bloch93* @see java.util.Comparator94* @since 1.295*/96publicinterfaceComparable<T> { 97/**98* Compares this object with the specified object for order. Returns a99* negative integer, zero, or a positive integer as this object is less100* than, equal to, or greater than the specified object.101*102* <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==103* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This104* implies that <tt>x.compareTo(y)</tt> must throw an exception iff105* <tt>y.compareTo(x)</tt> throws an exception.)106*107* <p>The implementor must also ensure that the relation is transitive:108* <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies109* <tt>x.compareTo(z)>0</tt>.110*111* <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>112* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for113* all <tt>z</tt>.114*115* <p>It is strongly recommended, but <i>not</i> strictly required that116* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any117* class that implements the <tt>Comparable</tt> interface and violates118* this condition should clearly indicate this fact. The recommended119* language is "Note: this class has a natural ordering that is120* inconsistent with equals."121*122* <p>In the foregoing description, the notation123* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical124* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,125* <tt>0</tt>, or <tt>1</tt> according to whether the value of126* <i>expression</i> is negative, zero or positive.127*128* @param o the object to be compared.129* @return a negative integer, zero, or a positive integer as this object130* is less than, equal to, or greater than the specified object.131*132* @throws NullPointerException if the specified object is null133* @throws ClassCastException if the specified object's type prevents it134* from being compared to this object.135*/136publicintcompareTo(T o); 137 }