001: /* Comparator.java -- Interface for objects that specify an ordering
002: Copyright (C) 1998, 2001 Free Software Foundation, Inc.
003:
004: This file is part of GNU Classpath.
005:
006: GNU Classpath is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2, or (at your option)
009: any later version.
010:
011: GNU Classpath is distributed in the hope that it will be useful, but
012: WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with GNU Classpath; see the file COPYING. If not, write to the
018: Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
019: 02111-1307 USA.
020:
021: Linking this library statically or dynamically with other modules is
022: making a combined work based on this library. Thus, the terms and
023: conditions of the GNU General Public License cover the whole
024: combination.
025:
026: As a special exception, the copyright holders of this library give you
027: permission to link this library with independent modules to produce an
028: executable, regardless of the license terms of these independent
029: modules, and to copy and distribute the resulting executable under
030: terms of your choice, provided that you also meet, for each linked
031: independent module, the terms and conditions of the license of that
032: module. An independent module is a module which is not derived from
033: or based on this library. If you modify this library, you may extend
034: this exception to your version of the library, but you are not
035: obligated to do so. If you do not wish to do so, delete this
036: exception statement from your version. */
037:
038: package java.util;
039:
040: /**
041: * Interface for objects that specify an ordering between objects. The ordering
042: * should be <em>total</em>, such that any two objects of the correct type
043: * can be compared, and the comparison is reflexive, anti-symmetric, and
044: * transitive. It is also recommended that the comparator be <em>consistent
045: * with equals</em>, although this is not a strict requirement. A relation
046: * is consistent with equals if these two statements always have the same
047: * results (if no exceptions occur):<br>
048: * <code>compare((Object) e1, (Object) e2) == 0</code> and
049: * <code>e1.equals((Object) e2)</code><br>
050: * Comparators that violate consistency with equals may cause strange behavior
051: * in sorted lists and sets. For example, a case-sensitive dictionary order
052: * comparison of Strings is consistent with equals, but if it is
053: * case-insensitive it is not, because "abc" and "ABC" compare as equal even
054: * though "abc".equals("ABC") returns false.
055: * <P>
056: * In general, Comparators should be Serializable, because when they are passed
057: * to Serializable data structures such as SortedMap or SortedSet, the entire
058: * data structure will only serialize correctly if the comparator is
059: * Serializable.
060: *
061: * @author Original author unknown
062: * @author Eric Blake <ebb9@email.byu.edu>
063: * @see Comparable
064: * @see TreeMap
065: * @see TreeSet
066: * @see SortedMap
067: * @see SortedSet
068: * @see Arrays#sort(Object[], Comparator)
069: * @see java.io.Serializable
070: * @since 1.2
071: * @status updated to 1.4
072: */
073: public interface Comparator {
074: /**
075: * Return an integer that is negative, zero or positive depending on whether
076: * the first argument is less than, equal to or greater than the second
077: * according to this ordering. This method should obey the following
078: * contract:
079: * <ul>
080: * <li>if compare(a, b) < 0 then compare(b, a) > 0</li>
081: * <li>if compare(a, b) throws an exception, so does compare(b, a)</li>
082: * <li>if compare(a, b) < 0 and compare(b, c) < 0 then compare(a, c)
083: * < 0</li>
084: * <li>if compare(a, b) == 0 then compare(a, c) and compare(b, c) must
085: * have the same sign</li
086: * </ul>
087: * To be consistent with equals, the following additional constraint is
088: * in place:
089: * <ul>
090: * <li>if a.equals(b) or both a and b are null, then
091: * compare(a, b) == 0.</li>
092: * </ul><p>
093: *
094: * Although it is permissible for a comparator to provide an order
095: * inconsistent with equals, that should be documented.
096: *
097: * @param o1 the first object
098: * @param o2 the second object
099: * @return the comparison
100: * @throws ClassCastException if the elements are not of types that can be
101: * compared by this ordering.
102: */
103: int compare(Object o1, Object o2);
104:
105: /**
106: * Return true if the object is equal to this object. To be
107: * considered equal, the argument object must satisfy the constraints
108: * of <code>Object.equals()</code>, be a Comparator, and impose the
109: * same ordering as this Comparator. The default implementation
110: * inherited from Object is usually adequate.
111: *
112: * @param obj The object
113: * @return true if it is a Comparator that imposes the same order
114: * @see Object#equals(Object)
115: */
116: boolean equals(Object obj);
117: }
|