author | simonis |
Fri, 14 Aug 2015 10:35:45 +0200 | |
changeset 32209 | 24bb680a1609 |
parent 32033 | bf24e33c7919 |
child 44549 | 1d4bd699fda4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15647
314007859004
8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
import java.util.*; |
|
28 |
||
29 |
/** |
|
30 |
* This interface imposes a total ordering on the objects of each class that |
|
31 |
* implements it. This ordering is referred to as the class's <i>natural |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
32 |
* ordering</i>, and the class's {@code compareTo} method is referred to as |
2 | 33 |
* its <i>natural comparison method</i>.<p> |
34 |
* |
|
35 |
* Lists (and arrays) of objects that implement this interface can be sorted |
|
36 |
* automatically by {@link Collections#sort(List) Collections.sort} (and |
|
37 |
* {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this |
|
38 |
* interface can be used as keys in a {@linkplain SortedMap sorted map} or as |
|
39 |
* elements in a {@linkplain SortedSet sorted set}, without the need to |
|
40 |
* specify a {@linkplain Comparator comparator}.<p> |
|
41 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
42 |
* The natural ordering for a class {@code C} is said to be <i>consistent |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
43 |
* with equals</i> if and only if {@code e1.compareTo(e2) == 0} has |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
44 |
* the same boolean value as {@code e1.equals(e2)} for every |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
45 |
* {@code e1} and {@code e2} of class {@code C}. Note that {@code null} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
46 |
* is not an instance of any class, and {@code e.compareTo(null)} should |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
47 |
* throw a {@code NullPointerException} even though {@code e.equals(null)} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
48 |
* returns {@code false}.<p> |
2 | 49 |
* |
50 |
* It is strongly recommended (though not required) that natural orderings be |
|
51 |
* consistent with equals. This is so because sorted sets (and sorted maps) |
|
52 |
* without explicit comparators behave "strangely" when they are used with |
|
53 |
* elements (or keys) whose natural ordering is inconsistent with equals. In |
|
54 |
* particular, such a sorted set (or sorted map) violates the general contract |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
55 |
* for set (or map), which is defined in terms of the {@code equals} |
2 | 56 |
* method.<p> |
57 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
58 |
* For example, if one adds two keys {@code a} and {@code b} such that |
18156 | 59 |
* {@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
60 |
* set that does not use an explicit comparator, the second {@code add} |
2 | 61 |
* operation returns false (and the size of the sorted set does not increase) |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
62 |
* because {@code a} and {@code b} are equivalent from the sorted set's |
2 | 63 |
* perspective.<p> |
64 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
65 |
* Virtually all Java core classes that implement {@code Comparable} have natural |
2 | 66 |
* orderings that are consistent with equals. One exception is |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
67 |
* {@code java.math.BigDecimal}, whose natural ordering equates |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
68 |
* {@code BigDecimal} objects with equal values and different precisions |
2 | 69 |
* (such as 4.0 and 4.00).<p> |
70 |
* |
|
71 |
* For the mathematically inclined, the <i>relation</i> that defines |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
72 |
* the natural ordering on a given class C is:<pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
73 |
* {(x, y) such that x.compareTo(y) <= 0}. |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
74 |
* }</pre> The <i>quotient</i> for this total order is: <pre>{@code |
2 | 75 |
* {(x, y) such that x.compareTo(y) == 0}. |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
76 |
* }</pre> |
2 | 77 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
78 |
* It follows immediately from the contract for {@code compareTo} that the |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
79 |
* quotient is an <i>equivalence relation</i> on {@code C}, and that the |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
80 |
* natural ordering is a <i>total order</i> on {@code C}. When we say that a |
2 | 81 |
* class's natural ordering is <i>consistent with equals</i>, we mean that the |
82 |
* quotient for the natural ordering is the equivalence relation defined by |
|
83 |
* 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 the |
|
87 |
* <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 to |
|
91 |
* |
|
92 |
* @author Josh Bloch |
|
93 |
* @see java.util.Comparator |
|
94 |
* @since 1.2 |
|
95 |
*/ |
|
96 |
public interface Comparable<T> { |
|
97 |
/** |
|
98 |
* Compares this object with the specified object for order. Returns a |
|
99 |
* negative integer, zero, or a positive integer as this object is less |
|
100 |
* than, equal to, or greater than the specified object. |
|
101 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
102 |
* <p>The implementor must ensure |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
103 |
* {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
104 |
* for all {@code x} and {@code y}. (This |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
105 |
* implies that {@code x.compareTo(y)} must throw an exception iff |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
106 |
* {@code y.compareTo(x)} throws an exception.) |
2 | 107 |
* |
108 |
* <p>The implementor must also ensure that the relation is transitive: |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
109 |
* {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
110 |
* {@code x.compareTo(z) > 0}. |
2 | 111 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
112 |
* <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
113 |
* implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
114 |
* all {@code z}. |
2 | 115 |
* |
116 |
* <p>It is strongly recommended, but <i>not</i> strictly required that |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
117 |
* {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
118 |
* class that implements the {@code Comparable} interface and violates |
2 | 119 |
* this condition should clearly indicate this fact. The recommended |
120 |
* language is "Note: this class has a natural ordering that is |
|
121 |
* inconsistent with equals." |
|
122 |
* |
|
123 |
* <p>In the foregoing description, the notation |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
124 |
* {@code sgn(}<i>expression</i>{@code )} designates the mathematical |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
125 |
* <i>signum</i> function, which is defined to return one of {@code -1}, |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
126 |
* {@code 0}, or {@code 1} according to whether the value of |
2 | 127 |
* <i>expression</i> is negative, zero or positive. |
128 |
* |
|
129 |
* @param o the object to be compared. |
|
130 |
* @return a negative integer, zero, or a positive integer as this object |
|
131 |
* is less than, equal to, or greater than the specified object. |
|
132 |
* |
|
133 |
* @throws NullPointerException if the specified object is null |
|
134 |
* @throws ClassCastException if the specified object's type prevents it |
|
135 |
* from being compared to this object. |
|
136 |
*/ |
|
137 |
public int compareTo(T o); |
|
138 |
} |