1. /*
  2. * @(#)Comparator.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 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 c.compare((Object)x, (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><p>
  60. *
  61. * This interface is a member of the
  62. * <a href="{@docRoot}/../guide/collections/index.html">
  63. * Java Collections Framework</a>.
  64. *
  65. * @author Josh Bloch
  66. * @version 1.19, 01/23/03
  67. * @see Comparable
  68. * @see Arrays#sort(Object[], Comparator)
  69. * @see TreeMap
  70. * @see TreeSet
  71. * @see SortedMap
  72. * @see SortedSet
  73. * @see java.io.Serializable
  74. * @since 1.2
  75. */
  76. public interface Comparator {
  77. /**
  78. * Compares its two arguments for order. Returns a negative integer,
  79. * zero, or a positive integer as the first argument is less than, equal
  80. * to, or greater than the second.<p>
  81. *
  82. * The implementor must ensure that <tt>sgn(compare(x, y)) ==
  83. * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
  84. * implies that <tt>compare(x, y)</tt> must throw an exception if and only
  85. * if <tt>compare(y, x)</tt> throws an exception.)<p>
  86. *
  87. * The implementor must also ensure that the relation is transitive:
  88. * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
  89. * <tt>compare(x, z)>0</tt>.<p>
  90. *
  91. * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
  92. * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
  93. * <tt>z</tt>.<p>
  94. *
  95. * It is generally the case, but <i>not</i> strictly required that
  96. * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
  97. * any comparator that violates this condition should clearly indicate
  98. * this fact. The recommended language is "Note: this comparator
  99. * imposes orderings that are inconsistent with equals."
  100. *
  101. * @param o1 the first object to be compared.
  102. * @param o2 the second object to be compared.
  103. * @return a negative integer, zero, or a positive integer as the
  104. * first argument is less than, equal to, or greater than the
  105. * second.
  106. * @throws ClassCastException if the arguments' types prevent them from
  107. * being compared by this Comparator.
  108. */
  109. int compare(Object o1, Object o2);
  110. /**
  111. *
  112. * Indicates whether some other object is "equal to" this
  113. * Comparator. This method must obey the general contract of
  114. * <tt>Object.equals(Object)</tt>. Additionally, this method can return
  115. * <tt>true</tt> <i>only</i> if the specified Object is also a comparator
  116. * and it imposes the same ordering as this comparator. Thus,
  117. * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
  118. * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
  119. * <tt>o1</tt> and <tt>o2</tt>.<p>
  120. *
  121. * Note that it is <i>always</i> safe <i>not</i> to override
  122. * <tt>Object.equals(Object)</tt>. However, overriding this method may,
  123. * in some cases, improve performance by allowing programs to determine
  124. * that two distinct Comparators impose the same order.
  125. *
  126. * @param obj the reference object with which to compare.
  127. * @return <code>true</code> only if the specified object is also
  128. * a comparator and it imposes the same ordering as this
  129. * comparator.
  130. * @see java.lang.Object#equals(java.lang.Object)
  131. * @see java.lang.Object#hashCode()
  132. */
  133. boolean equals(Object obj);
  134. }