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