001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package java.lang;
026:
027: /**
028: * This interface imposes a total ordering on the objects of each class that
029: * implements it. This ordering is referred to as the class's <i>natural
030: * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
031: * its <i>natural comparison method</i>.
032: *
033: * <p><b><i>This interface is provided as part of the JSR 239 NIO Buffer
034: * building block.</i></b>
035: *
036: * <p> <b><i>This interface is used by the <code>java.nio.ByteBuffer</code>,
037: * <code>ShortBuffer</code>, <code>IntBuffer</code>, and
038: * <code>FloatBuffer</code> classes. The documentation that follows
039: * is taken from Java Standard Edition version 1.4.2 and may not
040: * reflect the actual classes and interfaces that are present on a
041: * particular Java platform that supports the JSR 239 building
042: * block.</i></b>
043: *
044: * <p>
045: * Lists (and arrays) of objects that implement this interface can
046: * be sorted automatically by <tt>Collections.sort</tt> (and
047: * <tt>Arrays.sort</tt>). Objects that implement this interface can
048: * be used as keys in a sorted map or elements in a sorted set,
049: * without the need to specify a comparator.<p>
050: *
051: * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
052: * with equals</i> if and only if <tt>(e1.compareTo((Object)e2) == 0)</tt> has
053: * the same boolean value as <tt>e1.equals((Object)e2)</tt> for every
054: * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>
055: * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
056: * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
057: * returns <tt>false</tt>.<p>
058: *
059: * It is strongly recommended (though not required) that natural orderings be
060: * consistent with equals. This is so because sorted sets (and sorted maps)
061: * without explicit comparators behave "strangely" when they are used with
062: * elements (or keys) whose natural ordering is inconsistent with equals. In
063: * particular, such a sorted set (or sorted map) violates the general contract
064: * for set (or map), which is defined in terms of the <tt>equals</tt>
065: * method.<p>
066: *
067: * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
068: * <tt>(!a.equals((Object)b) && a.compareTo((Object)b) == 0)</tt> to a sorted
069: * set that does not use an explicit comparator, the second <tt>add</tt>
070: * operation returns false (and the size of the sorted set does not increase)
071: * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
072: * perspective.<p>
073: *
074: * Virtually all Java core classes that implement comparable have natural
075: * orderings that are consistent with equals. One exception is
076: * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
077: * <tt>BigDecimal</tt> objects with equal values and different precisions
078: * (such as 4.0 and 4.00).<p>
079: *
080: * For the mathematically inclined, the <i>relation</i> that defines
081: * the natural ordering on a given class C is:<pre>
082: * {(x, y) such that x.compareTo((Object)y) <= 0}.
083: * </pre> The <i>quotient</i> for this total order is: <pre>
084: * {(x, y) such that x.compareTo((Object)y) == 0}.
085: * </pre>
086: *
087: * It follows immediately from the contract for <tt>compareTo</tt> that the
088: * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
089: * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
090: * class's natural ordering is <i>consistent with equals</i>, we mean that the
091: * quotient for the natural ordering is the equivalence relation defined by
092: * the class's <tt>equals(Object)</tt> method:<pre>
093: * {(x, y) such that x.equals((Object)y)}.
094: * </pre><p>
095: *
096: * This interface is a member of the Java Collections Framework.
097: *
098: * @version 1.20, 01/23/03
099: * @see java.util.Comparator
100: * @see java.util.Collections#sort(java.util.List)
101: * @see java.util.Arrays#sort(Object[])
102: * @see java.util.SortedSet
103: * @see java.util.SortedMap
104: * @see java.util.TreeSet
105: * @see java.util.TreeMap
106: * @since 1.2
107: */
108:
109: public interface Comparable {
110: /**
111: * Compares this object with the specified object for order. Returns a
112: * negative integer, zero, or a positive integer as this object is less
113: * than, equal to, or greater than the specified object.<p>
114: *
115: * In the foregoing description, the notation
116: * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
117: * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
118: * <tt>0</tt>, or <tt>1</tt> according to whether the value of <i>expression</i>
119: * is negative, zero or positive.
120: *
121: * The implementor must ensure <tt>sgn(x.compareTo(y)) ==
122: * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
123: * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
124: * <tt>y.compareTo(x)</tt> throws an exception.)<p>
125: *
126: * The implementor must also ensure that the relation is transitive:
127: * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
128: * <tt>x.compareTo(z)>0</tt>.<p>
129: *
130: * Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt>
131: * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
132: * all <tt>z</tt>.<p>
133: *
134: * It is strongly recommended, but <i>not</i> strictly required that
135: * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
136: * class that implements the <tt>Comparable</tt> interface and violates
137: * this condition should clearly indicate this fact. The recommended
138: * language is "Note: this class has a natural ordering that is
139: * inconsistent with equals."
140: *
141: * @param o the Object to be compared.
142: * @return a negative integer, zero, or a positive integer as this object
143: * is less than, equal to, or greater than the specified object.
144: *
145: * @throws ClassCastException if the specified object's type prevents it
146: * from being compared to this Object.
147: */
148: public int compareTo(Object o);
149: }
|