1. /*
  2. * @(#)Comparator.java 1.11 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.util;
  8. /**
  9. * A comparison function, which imposes a <i>total ordering</i> on some
  10. * collection of objects. Comparators can be passed to a sort method (such as
  11. * <tt>Collections.sort</tt>) to allow precise control over the sort order.
  12. * Comparators can also be used to control the order of certain data
  13. * structures (such as <tt>TreeSet</tt> or <tt>TreeMap</tt>).<p>
  14. *
  15. * The ordering imposed by a Comparator <tt>c</tt> on a set of elements
  16. * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
  17. * <tt>(compare((Object)e1, (Object)e2)==0)</tt> has the same boolean value as
  18. * <tt>e1.equals((Object)e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
  19. * <tt>S</tt>.<p>
  20. *
  21. * Caution should be exercised when using a comparator capable of imposing an
  22. * ordering inconsistent with equals to order a sorted set (or sorted map).
  23. * Suppose a sorted set (or sorted map) with an explicit Comparator <tt>c</tt>
  24. * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
  25. * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
  26. * the sorted set (or sorted map) will behave "strangely." In particular the
  27. * sorted set (or sorted map) will violate the general contract for set (or
  28. * map), which is defined in terms of <tt>equals</tt>.<p>
  29. *
  30. * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
  31. * <tt>(a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0)</tt> to a
  32. * sorted set with comparator <tt>c</tt>, the second <tt>add</tt> operation
  33. * will return false (and the size of the sorted set will not increase)
  34. * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
  35. * perspective.<p>
  36. *
  37. * Note: It is generally a good idea for comparators to implement
  38. * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
  39. * serializable data structures (like <tt>TreeSet</tt>, <tt>TreeMap</tt>). In
  40. * order for the data structure to serialize successfully, the comparator (if
  41. * provided) must implement <tt>Serializable</tt>.<p>
  42. *
  43. * For the mathematically inclined, the <i>relation</i> that defines
  44. * the <i>total order</i> that a given comparator <tt>c</tt> imposes on a
  45. * given set of objects <tt>S</tt> is:<pre>
  46. * {(x, y) such that c.compare((Object)x, (Object)y) <= 0}.
  47. * </pre> The <i>quotient</i> for this total order is:<pre>
  48. * {(x, y) such that x.compareTo((Object)y) == 0}.
  49. * </pre>
  50. *
  51. * It follows immediately from the contract for <tt>compare</tt> that the
  52. * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
  53. * natural ordering is a <i>total order</i> on <tt>S</tt>. When we say that
  54. * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
  55. * equals</i>, we mean that the quotient for the natural ordering is the
  56. * equivalence relation defined by the objects' <tt>equals(Object)</tt>
  57. * method(s):<pre>
  58. * {(x, y) such that x.equals((Object)y)}.
  59. * </pre>
  60. *
  61. * @author Josh Bloch
  62. * @version 1.11 11/29/01
  63. * @see Comparable
  64. * @see Arrays#sort(Object[], Comparator)
  65. * @see TreeMap
  66. * @see TreeSet
  67. * @see SortedMap
  68. * @see SortedSet
  69. * @see java.io.Serializable
  70. * @since JDK1.2
  71. */
  72. public interface Comparator {
  73. /**
  74. * Compares its two arguments for order. Returns a negative integer,
  75. * zero, or a positive integer as the first argument is less than, equal
  76. * to, or greater than the second.<p>
  77. *
  78. * The implementor must ensure that <tt>sgn(compare(x, y)) ==
  79. * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
  80. * implies that <tt>compare(x, y)</tt> must throw an exception if and only
  81. * if <tt>compare(y, x)</tt> throws an exception.)<p>
  82. *
  83. * The implementor must also ensure that the relation is transitive:
  84. * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
  85. * <tt>compare(x, z)>0</tt>.<p>
  86. *
  87. * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
  88. * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
  89. * <tt>z</tt>.<p>
  90. *
  91. * It is generally the case, but <i>not</i> strictly required that
  92. * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
  93. * any comparator that violates this condition should clearly indicate
  94. * this fact. The recommended language is "Note: this comparator
  95. * imposes orderings that are inconsistent with equals."
  96. *
  97. * @return a negative integer, zero, or a positive integer as the
  98. * first argument is less than, equal to, or greater than the
  99. * second.
  100. * @throws ClassCastException if the arguments' types prevent them from
  101. * being compared by this Comparator.
  102. */
  103. int compare(Object o1, Object o2);
  104. /**
  105. *
  106. * Indicates whether some other object is "equal to" this
  107. * Comparator. This method must obey the general contract of
  108. * <tt>Object.equals(Object)</tt>. Additionally, this method can return
  109. * <tt>true</tt> <i>only</i> if the specified Object is also a comparator
  110. * and it imposes the same ordering as this comparator. Thus,
  111. * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
  112. * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
  113. * <tt>o1</tt> and <tt>o2</tt>.<p>
  114. *
  115. * Note that it is <i>always</i> safe <i>not</i> to override
  116. * <tt>Object.equals(Object)</tt>. However, overriding this method may,
  117. * in some cases, improve performance by allowing programs to determine
  118. * that two distinct Comparators impose the same order.
  119. *
  120. * @param obj the reference object with which to compare.
  121. * @return <code>true</code> only if the specified object is also
  122. * a comparator and it imposes the same ordering as this
  123. * comparator.
  124. * @see java.lang.Object#equals(java.lang.Object)
  125. * @see java.lang.Object#hashCode()
  126. */
  127. boolean equals(Object obj);
  128. }