source: trunk/gcc/libjava/java/util/Comparator.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.9 KB
Line 
1/* Comparator.java -- Interface for objects that specify an ordering
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package 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 */
74public interface Comparator
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) &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>
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(Object o1, Object 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}
Note: See TracBrowser for help on using the repository browser.