Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / util / Comparator.java
blobd536520cb471fd22659b23da5e8f187810353638
1 /* Comparator.java -- Interface for objects that specify an ordering
2 Copyright (C) 1998, 2001, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package java.util;
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.
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
74 public interface Comparator
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) &lt; 0 then compare(b, a) &gt; 0</li>
83 * <li>if compare(a, b) throws an exception, so does compare(b, a)</li>
84 * <li>if compare(a, b) &lt; 0 and compare(b, c) &lt; 0 then compare(a, c)
85 * &lt; 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>
96 * Although it is permissible for a comparator to provide an order
97 * inconsistent with equals, that should be documented.
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.
105 int compare(Object o1, Object o2);
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.
114 * @param obj The object
115 * @return true if it is a Comparator that imposes the same order
116 * @see Object#equals(Object)
118 boolean equals(Object obj);