001 /* Comparator.java -- Interface for objects that specify an ordering
002 Copyright (C) 1998, 2001, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 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
039 package java.util;
040
041 /**
042 * Interface for objects that specify an ordering between objects. The ordering
043 * should be <em>total</em>, such that any two objects of the correct type
044 * can be compared, and the comparison is reflexive, anti-symmetric, and
045 * transitive. It is also recommended that the comparator be <em>consistent
046 * with equals</em>, although this is not a strict requirement. A relation
047 * is consistent with equals if these two statements always have the same
048 * results (if no exceptions occur):<br>
049 * <code>compare((Object) e1, (Object) e2) == 0</code> and
050 * <code>e1.equals((Object) e2)</code><br>
051 * Comparators that violate consistency with equals may cause strange behavior
052 * in sorted lists and sets. For example, a case-sensitive dictionary order
053 * comparison of Strings is consistent with equals, but if it is
054 * case-insensitive it is not, because "abc" and "ABC" compare as equal even
055 * though "abc".equals("ABC") returns false.
056 * <P>
057 * In general, Comparators should be Serializable, because when they are passed
058 * to Serializable data structures such as SortedMap or SortedSet, the entire
059 * data structure will only serialize correctly if the comparator is
060 * Serializable.
061 *
062 * @author Original author unknown
063 * @author Eric Blake (ebb9@email.byu.edu)
064 * @see Comparable
065 * @see TreeMap
066 * @see TreeSet
067 * @see SortedMap
068 * @see SortedSet
069 * @see Arrays#sort(Object[], Comparator)
070 * @see java.io.Serializable
071 * @since 1.2
072 * @status updated to 1.4
073 */
074 public interface Comparator<T>
075 {
076 /**
077 * Return an integer that is negative, zero or positive depending on whether
078 * the first argument is less than, equal to or greater than the second
079 * according to this ordering. This method should obey the following
080 * contract:
081 * <ul>
082 * <li>if compare(a, b) < 0 then compare(b, a) > 0</li>
083 * <li>if compare(a, b) throws an exception, so does compare(b, a)</li>
084 * <li>if compare(a, b) < 0 and compare(b, c) < 0 then compare(a, c)
085 * < 0</li>
086 * <li>if compare(a, b) == 0 then compare(a, c) and compare(b, c) must
087 * have the same sign</li>
088 * </ul>
089 * To be consistent with equals, the following additional constraint is
090 * in place:
091 * <ul>
092 * <li>if a.equals(b) or both a and b are null, then
093 * compare(a, b) == 0.</li>
094 * </ul><p>
095 *
096 * Although it is permissible for a comparator to provide an order
097 * inconsistent with equals, that should be documented.
098 *
099 * @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 }