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