jaroslav@52: /* jaroslav@52: * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. jaroslav@52: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@52: * jaroslav@52: * This code is free software; you can redistribute it and/or modify it jaroslav@52: * under the terms of the GNU General Public License version 2 only, as jaroslav@52: * published by the Free Software Foundation. Oracle designates this jaroslav@52: * particular file as subject to the "Classpath" exception as provided jaroslav@52: * by Oracle in the LICENSE file that accompanied this code. jaroslav@52: * jaroslav@52: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@52: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@52: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@52: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@52: * accompanied this code). jaroslav@52: * jaroslav@52: * You should have received a copy of the GNU General Public License version jaroslav@52: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@52: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@52: * jaroslav@52: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@52: * or visit www.oracle.com if you need additional information or have any jaroslav@52: * questions. jaroslav@52: */ jaroslav@52: jaroslav@52: package java.lang; jaroslav@52: jaroslav@52: /** jaroslav@52: * This interface imposes a total ordering on the objects of each class that jaroslav@52: * implements it. This ordering is referred to as the class's natural jaroslav@52: * ordering, and the class's compareTo method is referred to as jaroslav@52: * its natural comparison method.

jaroslav@52: * jaroslav@52: * Lists (and arrays) of objects that implement this interface can be sorted jaroslav@52: * automatically by {@link Collections#sort(List) Collections.sort} (and jaroslav@52: * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this jaroslav@52: * interface can be used as keys in a {@linkplain SortedMap sorted map} or as jaroslav@52: * elements in a {@linkplain SortedSet sorted set}, without the need to jaroslav@52: * specify a {@linkplain Comparator comparator}.

jaroslav@52: * jaroslav@52: * The natural ordering for a class C is said to be consistent jaroslav@52: * with equals if and only if e1.compareTo(e2) == 0 has jaroslav@52: * the same boolean value as e1.equals(e2) for every jaroslav@52: * e1 and e2 of class C. Note that null jaroslav@52: * is not an instance of any class, and e.compareTo(null) should jaroslav@52: * throw a NullPointerException even though e.equals(null) jaroslav@52: * returns false.

jaroslav@52: * jaroslav@52: * It is strongly recommended (though not required) that natural orderings be jaroslav@52: * consistent with equals. This is so because sorted sets (and sorted maps) jaroslav@52: * without explicit comparators behave "strangely" when they are used with jaroslav@52: * elements (or keys) whose natural ordering is inconsistent with equals. In jaroslav@52: * particular, such a sorted set (or sorted map) violates the general contract jaroslav@52: * for set (or map), which is defined in terms of the equals jaroslav@52: * method.

jaroslav@52: * jaroslav@52: * For example, if one adds two keys a and b such that jaroslav@52: * (!a.equals(b) && a.compareTo(b) == 0) to a sorted jaroslav@52: * set that does not use an explicit comparator, the second add jaroslav@52: * operation returns false (and the size of the sorted set does not increase) jaroslav@52: * because a and b are equivalent from the sorted set's jaroslav@52: * perspective.

jaroslav@52: * jaroslav@52: * Virtually all Java core classes that implement Comparable have natural jaroslav@52: * orderings that are consistent with equals. One exception is jaroslav@52: * java.math.BigDecimal, whose natural ordering equates jaroslav@52: * BigDecimal objects with equal values and different precisions jaroslav@52: * (such as 4.0 and 4.00).

jaroslav@52: * jaroslav@52: * For the mathematically inclined, the relation that defines jaroslav@52: * the natural ordering on a given class C is:

jaroslav@52:  *       {(x, y) such that x.compareTo(y) <= 0}.
jaroslav@52:  * 
The quotient for this total order is:
jaroslav@52:  *       {(x, y) such that x.compareTo(y) == 0}.
jaroslav@52:  * 
jaroslav@52: * jaroslav@52: * It follows immediately from the contract for compareTo that the jaroslav@52: * quotient is an equivalence relation on C, and that the jaroslav@52: * natural ordering is a total order on C. When we say that a jaroslav@52: * class's natural ordering is consistent with equals, we mean that the jaroslav@52: * quotient for the natural ordering is the equivalence relation defined by jaroslav@52: * the class's {@link Object#equals(Object) equals(Object)} method:
jaroslav@52:  *     {(x, y) such that x.equals(y)}. 

jaroslav@52: * jaroslav@52: * This interface is a member of the jaroslav@52: * jaroslav@52: * Java Collections Framework. jaroslav@52: * jaroslav@52: * @param the type of objects that this object may be compared to jaroslav@52: * jaroslav@52: * @author Josh Bloch jaroslav@52: * @see java.util.Comparator jaroslav@52: * @since 1.2 jaroslav@52: */ jaroslav@52: jaroslav@52: public interface Comparable { jaroslav@52: /** jaroslav@52: * Compares this object with the specified object for order. Returns a jaroslav@52: * negative integer, zero, or a positive integer as this object is less jaroslav@52: * than, equal to, or greater than the specified object. jaroslav@52: * jaroslav@52: *

The implementor must ensure sgn(x.compareTo(y)) == jaroslav@52: * -sgn(y.compareTo(x)) for all x and y. (This jaroslav@52: * implies that x.compareTo(y) must throw an exception iff jaroslav@52: * y.compareTo(x) throws an exception.) jaroslav@52: * jaroslav@52: *

The implementor must also ensure that the relation is transitive: jaroslav@52: * (x.compareTo(y)>0 && y.compareTo(z)>0) implies jaroslav@52: * x.compareTo(z)>0. jaroslav@52: * jaroslav@52: *

Finally, the implementor must ensure that x.compareTo(y)==0 jaroslav@52: * implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for jaroslav@52: * all z. jaroslav@52: * jaroslav@52: *

It is strongly recommended, but not strictly required that jaroslav@52: * (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any jaroslav@52: * class that implements the Comparable interface and violates jaroslav@52: * this condition should clearly indicate this fact. The recommended jaroslav@52: * language is "Note: this class has a natural ordering that is jaroslav@52: * inconsistent with equals." jaroslav@52: * jaroslav@52: *

In the foregoing description, the notation jaroslav@52: * sgn(expression) designates the mathematical jaroslav@52: * signum function, which is defined to return one of -1, jaroslav@52: * 0, or 1 according to whether the value of jaroslav@52: * expression is negative, zero or positive. jaroslav@52: * jaroslav@52: * @param o the object to be compared. jaroslav@52: * @return a negative integer, zero, or a positive integer as this object jaroslav@52: * is less than, equal to, or greater than the specified object. jaroslav@52: * jaroslav@52: * @throws NullPointerException if the specified object is null jaroslav@52: * @throws ClassCastException if the specified object's type prevents it jaroslav@52: * from being compared to this object. jaroslav@52: */ jaroslav@52: public int compareTo(T o); jaroslav@52: }