Comparable.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Comparable.java -- Interface for comparaing objects to obtain an ordering
  2. Copyright (C) 1998, 1999, 2001, 2002, 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.lang;
  32. /**
  33. * Interface for objects that can be ordering among other objects. The
  34. * ordering can be <em>total</em>, such that two objects only compare equal
  35. * if they are also equal by the equals method, or <em>partial</em> such
  36. * that this is not necessarily true. For example, a case-sensitive
  37. * dictionary order comparison of Strings is total, but if it is
  38. * case-insensitive it is partial, because "abc" and "ABC" compare as
  39. * equal even though "abc".equals("ABC") returns false. However, if you use
  40. * a partial ordering, it is a good idea to document your class as
  41. * "inconsistent with equals", because the behavior of your class in a
  42. * SortedMap will be different than in a HashMap.
  43. *
  44. * <p>Lists, arrays, and sets of objects that implement this interface can
  45. * be sorted automatically, without the need for an explicit
  46. * {@link java.util.Comparator}. Note that <code>e1.compareTo(null)</code>
  47. * should throw an Exception; as should comparison between incompatible
  48. * classes.
  49. *
  50. * @author Geoff Berry
  51. * @author Warren Levy (warrenl@cygnus.com)
  52. * @see java.util.Comparator
  53. * @see java.util.Collections#sort(java.util.List)
  54. * @see java.util.Arrays#sort(Object[])
  55. * @see java.util.SortedSet
  56. * @see java.util.SortedMap
  57. * @see java.util.TreeSet
  58. * @see java.util.TreeMap
  59. * @since 1.2
  60. * @status updated to 1.5
  61. */
  62. public interface Comparable<T>
  63. {
  64. /**
  65. * Compares this object with another, and returns a numerical result based
  66. * on the comparison. If the result is negative, this object sorts less
  67. * than the other; if 0, the two are equal, and if positive, this object
  68. * sorts greater than the other. To translate this into boolean, simply
  69. * perform <code>o1.compareTo(o2) <em>&lt;op&gt;</em> 0</code>, where op
  70. * is one of &lt;, &lt;=, =, !=, &gt;, or &gt;=.
  71. *
  72. * <p>You must make sure that the comparison is mutual, ie.
  73. * <code>sgn(x.compareTo(y)) == -sgn(y.compareTo(x))</code> (where sgn() is
  74. * defined as -1, 0, or 1 based on the sign). This includes throwing an
  75. * exception in either direction if the two are not comparable; hence,
  76. * <code>compareTo(null)</code> should always throw an Exception.
  77. *
  78. * <p>You should also ensure transitivity, in two forms:
  79. * <code>x.compareTo(y) &gt; 0 && y.compareTo(z) &gt; 0</code> implies
  80. * <code>x.compareTo(z) &gt; 0</code>; and <code>x.compareTo(y) == 0</code>
  81. * implies <code>x.compareTo(z) == y.compareTo(z)</code>.
  82. *
  83. * @param o the object to be compared
  84. * @return an integer describing the comparison
  85. * @throws NullPointerException if o is null
  86. * @throws ClassCastException if o cannot be compared
  87. */
  88. int compareTo(T o);
  89. }