Comparable.hx 6.4 KB

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