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