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