1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/java/lang/Comparable.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,137 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +/**
1.32 + * This interface imposes a total ordering on the objects of each class that
1.33 + * implements it. This ordering is referred to as the class's <i>natural
1.34 + * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
1.35 + * its <i>natural comparison method</i>.<p>
1.36 + *
1.37 + * Lists (and arrays) of objects that implement this interface can be sorted
1.38 + * automatically by {@link Collections#sort(List) Collections.sort} (and
1.39 + * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this
1.40 + * interface can be used as keys in a {@linkplain SortedMap sorted map} or as
1.41 + * elements in a {@linkplain SortedSet sorted set}, without the need to
1.42 + * specify a {@linkplain Comparator comparator}.<p>
1.43 + *
1.44 + * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
1.45 + * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
1.46 + * the same boolean value as <tt>e1.equals(e2)</tt> for every
1.47 + * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>
1.48 + * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
1.49 + * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
1.50 + * returns <tt>false</tt>.<p>
1.51 + *
1.52 + * It is strongly recommended (though not required) that natural orderings be
1.53 + * consistent with equals. This is so because sorted sets (and sorted maps)
1.54 + * without explicit comparators behave "strangely" when they are used with
1.55 + * elements (or keys) whose natural ordering is inconsistent with equals. In
1.56 + * particular, such a sorted set (or sorted map) violates the general contract
1.57 + * for set (or map), which is defined in terms of the <tt>equals</tt>
1.58 + * method.<p>
1.59 + *
1.60 + * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
1.61 + * <tt>(!a.equals(b) && a.compareTo(b) == 0)</tt> to a sorted
1.62 + * set that does not use an explicit comparator, the second <tt>add</tt>
1.63 + * operation returns false (and the size of the sorted set does not increase)
1.64 + * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
1.65 + * perspective.<p>
1.66 + *
1.67 + * Virtually all Java core classes that implement <tt>Comparable</tt> have natural
1.68 + * orderings that are consistent with equals. One exception is
1.69 + * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
1.70 + * <tt>BigDecimal</tt> objects with equal values and different precisions
1.71 + * (such as 4.0 and 4.00).<p>
1.72 + *
1.73 + * For the mathematically inclined, the <i>relation</i> that defines
1.74 + * the natural ordering on a given class C is:<pre>
1.75 + * {(x, y) such that x.compareTo(y) <= 0}.
1.76 + * </pre> The <i>quotient</i> for this total order is: <pre>
1.77 + * {(x, y) such that x.compareTo(y) == 0}.
1.78 + * </pre>
1.79 + *
1.80 + * It follows immediately from the contract for <tt>compareTo</tt> that the
1.81 + * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
1.82 + * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
1.83 + * class's natural ordering is <i>consistent with equals</i>, we mean that the
1.84 + * quotient for the natural ordering is the equivalence relation defined by
1.85 + * the class's {@link Object#equals(Object) equals(Object)} method:<pre>
1.86 + * {(x, y) such that x.equals(y)}. </pre><p>
1.87 + *
1.88 + * This interface is a member of the
1.89 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.90 + * Java Collections Framework</a>.
1.91 + *
1.92 + * @param <T> the type of objects that this object may be compared to
1.93 + *
1.94 + * @author Josh Bloch
1.95 + * @see java.util.Comparator
1.96 + * @since 1.2
1.97 + */
1.98 +
1.99 +public interface Comparable<T> {
1.100 + /**
1.101 + * Compares this object with the specified object for order. Returns a
1.102 + * negative integer, zero, or a positive integer as this object is less
1.103 + * than, equal to, or greater than the specified object.
1.104 + *
1.105 + * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
1.106 + * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
1.107 + * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
1.108 + * <tt>y.compareTo(x)</tt> throws an exception.)
1.109 + *
1.110 + * <p>The implementor must also ensure that the relation is transitive:
1.111 + * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
1.112 + * <tt>x.compareTo(z)>0</tt>.
1.113 + *
1.114 + * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
1.115 + * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
1.116 + * all <tt>z</tt>.
1.117 + *
1.118 + * <p>It is strongly recommended, but <i>not</i> strictly required that
1.119 + * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
1.120 + * class that implements the <tt>Comparable</tt> interface and violates
1.121 + * this condition should clearly indicate this fact. The recommended
1.122 + * language is "Note: this class has a natural ordering that is
1.123 + * inconsistent with equals."
1.124 + *
1.125 + * <p>In the foregoing description, the notation
1.126 + * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
1.127 + * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
1.128 + * <tt>0</tt>, or <tt>1</tt> according to whether the value of
1.129 + * <i>expression</i> is negative, zero or positive.
1.130 + *
1.131 + * @param o the object to be compared.
1.132 + * @return a negative integer, zero, or a positive integer as this object
1.133 + * is less than, equal to, or greater than the specified object.
1.134 + *
1.135 + * @throws NullPointerException if the specified object is null
1.136 + * @throws ClassCastException if the specified object's type prevents it
1.137 + * from being compared to this object.
1.138 + */
1.139 + public int compareTo(T o);
1.140 +}