1. /*
  2. * @(#)Comparable.java 1.13 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. * This interface imposes a total ordering on the objects of each class that
  13. * implements it. This ordering is referred to as the class's <i>natural
  14. * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
  15. * its <i>natural comparison method</i>.<p>
  16. *
  17. * Lists (and arrays) of objects that implement this interface can be sorted
  18. * automatically by <tt>Collections.sort</tt> (and <tt>Arrays.sort</tt>).
  19. * Objects that implement this interface can be used as keys in a sorted map
  20. * or elements in a sorted set, without the need to specify a comparator.<p>
  21. *
  22. * A class's natural ordering is said to be <i>consistent with equals</i> if
  23. * and only if <tt>(e1.compareTo((Object)e2)==0)</tt> has the same boolean
  24. * value as <tt>e1.equals((Object)e2)</tt> for every <tt>e1</tt> and
  25. * <tt>e2</tt> of class <tt>C</tt>.<p>
  26. *
  27. * It is strongly recommended (though not required) that natural orderings be
  28. * consistent with equals. This is so because sorted sets (and sorted maps)
  29. * without explicit comparators behave "strangely" when they are used with
  30. * elements (or keys) whose natural ordering is inconsistent with equals. In
  31. * particular, such a sorted set (or sorted map) violates the general contract
  32. * for set (or map), which is defined in terms of the <tt>equals</tt>
  33. * operation.<p>
  34. *
  35. * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
  36. * <tt>(a.equals((Object)b) && a.compareTo((Object)b) != 0)</tt> to a sorted
  37. * set that does not use an explicit comparator, the second <tt>add</tt>
  38. * operation returns false (and the size of the sorted set does not increase)
  39. * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
  40. * perspective.<p>
  41. *
  42. * Virtually all Java core classes that implement comparable have natural
  43. * orderings that are consistent with equals. One exception is
  44. * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
  45. * <tt>BigDecimals</tt> with equal values and different precisions (such as
  46. * 4.0 and 4.00).<p>
  47. *
  48. * For the mathematically inclined, the <i>relation</i> that defines
  49. * the natural ordering on a given class C is:<pre>
  50. * {(x, y) such that x.compareTo((Object)y) <= 0}.
  51. * </pre> The <i>quotient</i> for this total order is: <pre>
  52. * {(x, y) such that x.compareTo((Object)y) == 0}.
  53. * </pre>
  54. *
  55. * It follows immediately from the contract for <tt>compareTo</tt> that the
  56. * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
  57. * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
  58. * class's natural ordering is <i>consistent with equals</i>, we mean that the
  59. * quotient for the natural ordering is the equivalence relation defined by
  60. * the class's <tt>equals(Object)</tt> method:<pre>
  61. * {(x, y) such that x.equals((Object)y)}.
  62. * </pre>
  63. *
  64. * @author Josh Bloch
  65. * @version 1.13, 02/02/00
  66. * @see java.util.Comparator
  67. * @see java.util.Collections#sort(java.util.List)
  68. * @see java.util.Arrays#sort(Object[])
  69. * @see java.util.SortedSet
  70. * @see java.util.SortedMap
  71. * @see java.util.TreeSet
  72. * @see java.util.TreeMap
  73. * @since 1.2
  74. */
  75. public interface Comparable {
  76. /**
  77. * Compares this object with the specified object for order. Returns a
  78. * negative integer, zero, or a positive integer as this object is less
  79. * than, equal to, or greater than the specified object.<p>
  80. *
  81. * The implementor must ensure <tt>sgn(x.compareTo(y)) ==
  82. * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
  83. * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
  84. * <tt>y.compareTo(x)</tt> throws an exception.)<p>
  85. *
  86. * The implementor must also ensure that the relation is transitive:
  87. * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
  88. * <tt>x.compareTo(z)>0</tt>.<p>
  89. *
  90. * Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt>
  91. * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
  92. * all <tt>z</tt>.<p>
  93. *
  94. * It is strongly recommended, but <i>not</i> strictly required that
  95. * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
  96. * class that implements the <tt>Comparable</tt> interface and violates
  97. * this condition should clearly indicate this fact. The recommended
  98. * language is "Note: this class has a natural ordering that is
  99. * inconsistent with equals."
  100. *
  101. * @param o the Object to be compared.
  102. * @return a negative integer, zero, or a positive integer as this object
  103. * is less than, equal to, or greater than the specified object.
  104. *
  105. * @throws ClassCastException if the specified object's type prevents it
  106. * from being compared to this Object.
  107. */
  108. public int compareTo(Object o);
  109. }