1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/util/Comparator.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,168 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util;
1.30 +
1.31 +/**
1.32 + * A comparison function, which imposes a <i>total ordering</i> on some
1.33 + * collection of objects. Comparators can be passed to a sort method (such
1.34 + * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
1.35 + * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
1.36 + * over the sort order. Comparators can also be used to control the order of
1.37 + * certain data structures (such as {@link SortedSet sorted sets} or {@link
1.38 + * SortedMap sorted maps}), or to provide an ordering for collections of
1.39 + * objects that don't have a {@link Comparable natural ordering}.<p>
1.40 + *
1.41 + * The ordering imposed by a comparator <tt>c</tt> on a set of elements
1.42 + * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
1.43 + * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
1.44 + * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
1.45 + * <tt>S</tt>.<p>
1.46 + *
1.47 + * Caution should be exercised when using a comparator capable of imposing an
1.48 + * ordering inconsistent with equals to order a sorted set (or sorted map).
1.49 + * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
1.50 + * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
1.51 + * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
1.52 + * the sorted set (or sorted map) will behave "strangely." In particular the
1.53 + * sorted set (or sorted map) will violate the general contract for set (or
1.54 + * map), which is defined in terms of <tt>equals</tt>.<p>
1.55 + *
1.56 + * For example, suppose one adds two elements {@code a} and {@code b} such that
1.57 + * {@code (a.equals(b) && c.compare(a, b) != 0)}
1.58 + * to an empty {@code TreeSet} with comparator {@code c}.
1.59 + * The second {@code add} operation will return
1.60 + * true (and the size of the tree set will increase) because {@code a} and
1.61 + * {@code b} are not equivalent from the tree set's perspective, even though
1.62 + * this is contrary to the specification of the
1.63 + * {@link Set#add Set.add} method.<p>
1.64 + *
1.65 + * Note: It is generally a good idea for comparators to also implement
1.66 + * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
1.67 + * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In
1.68 + * order for the data structure to serialize successfully, the comparator (if
1.69 + * provided) must implement <tt>Serializable</tt>.<p>
1.70 + *
1.71 + * For the mathematically inclined, the <i>relation</i> that defines the
1.72 + * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
1.73 + * given set of objects <tt>S</tt> is:<pre>
1.74 + * {(x, y) such that c.compare(x, y) <= 0}.
1.75 + * </pre> The <i>quotient</i> for this total order is:<pre>
1.76 + * {(x, y) such that c.compare(x, y) == 0}.
1.77 + * </pre>
1.78 + *
1.79 + * It follows immediately from the contract for <tt>compare</tt> that the
1.80 + * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
1.81 + * imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that
1.82 + * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
1.83 + * equals</i>, we mean that the quotient for the ordering is the equivalence
1.84 + * relation defined by the objects' {@link Object#equals(Object)
1.85 + * equals(Object)} method(s):<pre>
1.86 + * {(x, y) such that x.equals(y)}. </pre>
1.87 + *
1.88 + * <p>Unlike {@code Comparable}, a comparator may optionally permit
1.89 + * comparison of null arguments, while maintaining the requirements for
1.90 + * an equivalence relation.
1.91 + *
1.92 + * <p>This interface is a member of the
1.93 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.94 + * Java Collections Framework</a>.
1.95 + *
1.96 + * @param <T> the type of objects that may be compared by this comparator
1.97 + *
1.98 + * @author Josh Bloch
1.99 + * @author Neal Gafter
1.100 + * @see Comparable
1.101 + * @see java.io.Serializable
1.102 + * @since 1.2
1.103 + */
1.104 +
1.105 +public interface Comparator<T> {
1.106 + /**
1.107 + * Compares its two arguments for order. Returns a negative integer,
1.108 + * zero, or a positive integer as the first argument is less than, equal
1.109 + * to, or greater than the second.<p>
1.110 + *
1.111 + * In the foregoing description, the notation
1.112 + * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
1.113 + * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
1.114 + * <tt>0</tt>, or <tt>1</tt> according to whether the value of
1.115 + * <i>expression</i> is negative, zero or positive.<p>
1.116 + *
1.117 + * The implementor must ensure that <tt>sgn(compare(x, y)) ==
1.118 + * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
1.119 + * implies that <tt>compare(x, y)</tt> must throw an exception if and only
1.120 + * if <tt>compare(y, x)</tt> throws an exception.)<p>
1.121 + *
1.122 + * The implementor must also ensure that the relation is transitive:
1.123 + * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
1.124 + * <tt>compare(x, z)>0</tt>.<p>
1.125 + *
1.126 + * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
1.127 + * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
1.128 + * <tt>z</tt>.<p>
1.129 + *
1.130 + * It is generally the case, but <i>not</i> strictly required that
1.131 + * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
1.132 + * any comparator that violates this condition should clearly indicate
1.133 + * this fact. The recommended language is "Note: this comparator
1.134 + * imposes orderings that are inconsistent with equals."
1.135 + *
1.136 + * @param o1 the first object to be compared.
1.137 + * @param o2 the second object to be compared.
1.138 + * @return a negative integer, zero, or a positive integer as the
1.139 + * first argument is less than, equal to, or greater than the
1.140 + * second.
1.141 + * @throws NullPointerException if an argument is null and this
1.142 + * comparator does not permit null arguments
1.143 + * @throws ClassCastException if the arguments' types prevent them from
1.144 + * being compared by this comparator.
1.145 + */
1.146 + int compare(T o1, T o2);
1.147 +
1.148 + /**
1.149 + * Indicates whether some other object is "equal to" this
1.150 + * comparator. This method must obey the general contract of
1.151 + * {@link Object#equals(Object)}. Additionally, this method can return
1.152 + * <tt>true</tt> <i>only</i> if the specified object is also a comparator
1.153 + * and it imposes the same ordering as this comparator. Thus,
1.154 + * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
1.155 + * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
1.156 + * <tt>o1</tt> and <tt>o2</tt>.<p>
1.157 + *
1.158 + * Note that it is <i>always</i> safe <i>not</i> to override
1.159 + * <tt>Object.equals(Object)</tt>. However, overriding this method may,
1.160 + * in some cases, improve performance by allowing programs to determine
1.161 + * that two distinct comparators impose the same order.
1.162 + *
1.163 + * @param obj the reference object with which to compare.
1.164 + * @return <code>true</code> only if the specified object is also
1.165 + * a comparator and it imposes the same ordering as this
1.166 + * comparator.
1.167 + * @see Object#equals(Object)
1.168 + * @see Object#hashCode()
1.169 + */
1.170 + boolean equals(Object obj);
1.171 +}