Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Comparable.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;26import java.util.*;2728/**29* This interface imposes a total ordering on the objects of each class that30* implements it. This ordering is referred to as the class's <i>natural31* ordering</i>, and the class's <tt>compareTo</tt> method is referred to as32* its <i>natural comparison method</i>.<p>33*34* Lists (and arrays) of objects that implement this interface can be sorted35* automatically by {@link Collections#sort(List) Collections.sort} (and36* {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this37* interface can be used as keys in a {@linkplain SortedMap sorted map} or as38* elements in a {@linkplain SortedSet sorted set}, without the need to39* specify a {@linkplain Comparator comparator}.<p>40*41* The natural ordering for a class <tt>C</tt> is said to be <i>consistent42* with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has43* the same boolean value as <tt>e1.equals(e2)</tt> for every44* <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>45* is not an instance of any class, and <tt>e.compareTo(null)</tt> should46* throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>47* returns <tt>false</tt>.<p>48*49* It is strongly recommended (though not required) that natural orderings be50* consistent with equals. This is so because sorted sets (and sorted maps)51* without explicit comparators behave "strangely" when they are used with52* elements (or keys) whose natural ordering is inconsistent with equals. In53* particular, such a sorted set (or sorted map) violates the general contract54* for set (or map), which is defined in terms of the <tt>equals</tt>55* method.<p>56*57* For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that58* {@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted59* set that does not use an explicit comparator, the second <tt>add</tt>60* operation returns false (and the size of the sorted set does not increase)61* because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's62* perspective.<p>63*64* Virtually all Java core classes that implement <tt>Comparable</tt> have natural65* orderings that are consistent with equals. One exception is66* <tt>java.math.BigDecimal</tt>, whose natural ordering equates67* <tt>BigDecimal</tt> objects with equal values and different precisions68* (such as 4.0 and 4.00).<p>69*70* For the mathematically inclined, the <i>relation</i> that defines71* the natural ordering on a given class C is:<pre>72* {(x, y) such that x.compareTo(y) <= 0}.73* </pre> The <i>quotient</i> for this total order is: <pre>74* {(x, y) such that x.compareTo(y) == 0}.75* </pre>76*77* It follows immediately from the contract for <tt>compareTo</tt> that the78* quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the79* natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a80* class's natural ordering is <i>consistent with equals</i>, we mean that the81* quotient for the natural ordering is the equivalence relation defined by82* the class's {@link Object#equals(Object) equals(Object)} method:<pre>83* {(x, y) such that x.equals(y)}. </pre><p>84*85* This interface is a member of the86* <a href="{@docRoot}/../technotes/guides/collections/index.html">87* Java Collections Framework</a>.88*89* @param <T> the type of objects that this object may be compared to90*91* @author Josh Bloch92* @see java.util.Comparator93* @since 1.294*/95public interface Comparable<T> {96/**97* Compares this object with the specified object for order. Returns a98* negative integer, zero, or a positive integer as this object is less99* than, equal to, or greater than the specified object.100*101* <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==102* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This103* implies that <tt>x.compareTo(y)</tt> must throw an exception iff104* <tt>y.compareTo(x)</tt> throws an exception.)105*106* <p>The implementor must also ensure that the relation is transitive:107* <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies108* <tt>x.compareTo(z)>0</tt>.109*110* <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>111* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for112* all <tt>z</tt>.113*114* <p>It is strongly recommended, but <i>not</i> strictly required that115* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any116* class that implements the <tt>Comparable</tt> interface and violates117* this condition should clearly indicate this fact. The recommended118* language is "Note: this class has a natural ordering that is119* inconsistent with equals."120*121* <p>In the foregoing description, the notation122* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical123* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,124* <tt>0</tt>, or <tt>1</tt> according to whether the value of125* <i>expression</i> is negative, zero or positive.126*127* @param o the object to be compared.128* @return a negative integer, zero, or a positive integer as this object129* is less than, equal to, or greater than the specified object.130*131* @throws NullPointerException if the specified object is null132* @throws ClassCastException if the specified object's type prevents it133* from being compared to this object.134*/135public int compareTo(T o);136}137138139