Comparator.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Comparator.java -- Interface for objects that specify an ordering
  2. Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * Interface for objects that specify an ordering between objects. The ordering
  34. * should be <em>total</em>, such that any two objects of the correct type
  35. * can be compared, and the comparison is reflexive, anti-symmetric, and
  36. * transitive. It is also recommended that the comparator be <em>consistent
  37. * with equals</em>, although this is not a strict requirement. A relation
  38. * is consistent with equals if these two statements always have the same
  39. * results (if no exceptions occur):<br>
  40. * <code>compare((Object) e1, (Object) e2) == 0</code> and
  41. * <code>e1.equals((Object) e2)</code><br>
  42. * Comparators that violate consistency with equals may cause strange behavior
  43. * in sorted lists and sets. For example, a case-sensitive dictionary order
  44. * comparison of Strings is consistent with equals, but if it is
  45. * case-insensitive it is not, because "abc" and "ABC" compare as equal even
  46. * though "abc".equals("ABC") returns false.
  47. * <P>
  48. * In general, Comparators should be Serializable, because when they are passed
  49. * to Serializable data structures such as SortedMap or SortedSet, the entire
  50. * data structure will only serialize correctly if the comparator is
  51. * Serializable.
  52. *
  53. * @author Original author unknown
  54. * @author Eric Blake (ebb9@email.byu.edu)
  55. * @see Comparable
  56. * @see TreeMap
  57. * @see TreeSet
  58. * @see SortedMap
  59. * @see SortedSet
  60. * @see Arrays#sort(Object[], Comparator)
  61. * @see java.io.Serializable
  62. * @since 1.2
  63. * @status updated to 1.4
  64. */
  65. public interface Comparator<T>
  66. {
  67. /**
  68. * Return an integer that is negative, zero or positive depending on whether
  69. * the first argument is less than, equal to or greater than the second
  70. * according to this ordering. This method should obey the following
  71. * contract:
  72. * <ul>
  73. * <li>if compare(a, b) &lt; 0 then compare(b, a) &gt; 0</li>
  74. * <li>if compare(a, b) throws an exception, so does compare(b, a)</li>
  75. * <li>if compare(a, b) &lt; 0 and compare(b, c) &lt; 0 then compare(a, c)
  76. * &lt; 0</li>
  77. * <li>if compare(a, b) == 0 then compare(a, c) and compare(b, c) must
  78. * have the same sign</li>
  79. * </ul>
  80. * To be consistent with equals, the following additional constraint is
  81. * in place:
  82. * <ul>
  83. * <li>if a.equals(b) or both a and b are null, then
  84. * compare(a, b) == 0.</li>
  85. * </ul><p>
  86. *
  87. * Although it is permissible for a comparator to provide an order
  88. * inconsistent with equals, that should be documented.
  89. *
  90. * @param o1 the first object
  91. * @param o2 the second object
  92. * @return the comparison
  93. * @throws ClassCastException if the elements are not of types that can be
  94. * compared by this ordering.
  95. */
  96. int compare(T o1, T o2);
  97. /**
  98. * Return true if the object is equal to this object. To be
  99. * considered equal, the argument object must satisfy the constraints
  100. * of <code>Object.equals()</code>, be a Comparator, and impose the
  101. * same ordering as this Comparator. The default implementation
  102. * inherited from Object is usually adequate.
  103. *
  104. * @param obj The object
  105. * @return true if it is a Comparator that imposes the same order
  106. * @see Object#equals(Object)
  107. */
  108. boolean equals(Object obj);
  109. }