0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * A comparison function, which imposes a <i>total ordering</i> on some
0N/A * collection of objects. Comparators can be passed to a sort method (such
0N/A * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
0N/A * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
0N/A * over the sort order. Comparators can also be used to control the order of
0N/A * certain data structures (such as {@link SortedSet sorted sets} or {@link
0N/A * SortedMap sorted maps}), or to provide an ordering for collections of
0N/A * objects that don't have a {@link Comparable natural ordering}.<p>
0N/A *
0N/A * The ordering imposed by a comparator <tt>c</tt> on a set of elements
0N/A * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
0N/A * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
0N/A * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
0N/A * <tt>S</tt>.<p>
0N/A *
0N/A * Caution should be exercised when using a comparator capable of imposing an
0N/A * ordering inconsistent with equals to order a sorted set (or sorted map).
0N/A * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
0N/A * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
0N/A * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
0N/A * the sorted set (or sorted map) will behave "strangely." In particular the
0N/A * sorted set (or sorted map) will violate the general contract for set (or
0N/A * map), which is defined in terms of <tt>equals</tt>.<p>
0N/A *
0N/A * For example, suppose one adds two elements {@code a} and {@code b} such that
0N/A * {@code (a.equals(b) && c.compare(a, b) != 0)}
0N/A * to an empty {@code TreeSet} with comparator {@code c}.
0N/A * The second {@code add} operation will return
0N/A * true (and the size of the tree set will increase) because {@code a} and
0N/A * {@code b} are not equivalent from the tree set's perspective, even though
0N/A * this is contrary to the specification of the
0N/A * {@link Set#add Set.add} method.<p>
0N/A *
0N/A * Note: It is generally a good idea for comparators to also implement
0N/A * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
0N/A * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In
0N/A * order for the data structure to serialize successfully, the comparator (if
0N/A * provided) must implement <tt>Serializable</tt>.<p>
0N/A *
0N/A * For the mathematically inclined, the <i>relation</i> that defines the
0N/A * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
0N/A * given set of objects <tt>S</tt> is:<pre>
0N/A * {(x, y) such that c.compare(x, y) &lt;= 0}.
0N/A * </pre> The <i>quotient</i> for this total order is:<pre>
0N/A * {(x, y) such that c.compare(x, y) == 0}.
0N/A * </pre>
0N/A *
0N/A * It follows immediately from the contract for <tt>compare</tt> that the
0N/A * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
0N/A * imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that
0N/A * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
0N/A * equals</i>, we mean that the quotient for the ordering is the equivalence
0N/A * relation defined by the objects' {@link Object#equals(Object)
0N/A * equals(Object)} method(s):<pre>
0N/A * {(x, y) such that x.equals(y)}. </pre>
0N/A *
0N/A * <p>Unlike {@code Comparable}, a comparator may optionally permit
0N/A * comparison of null arguments, while maintaining the requirements for
0N/A * an equivalence relation.
0N/A *
0N/A * <p>This interface is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @param <T> the type of objects that may be compared by this comparator
0N/A *
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @see Comparable
0N/A * @see java.io.Serializable
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic interface Comparator<T> {
0N/A /**
0N/A * Compares its two arguments for order. Returns a negative integer,
0N/A * zero, or a positive integer as the first argument is less than, equal
0N/A * to, or greater than the second.<p>
0N/A *
0N/A * In the foregoing description, the notation
0N/A * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
0N/A * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
0N/A * <tt>0</tt>, or <tt>1</tt> according to whether the value of
0N/A * <i>expression</i> is negative, zero or positive.<p>
0N/A *
0N/A * The implementor must ensure that <tt>sgn(compare(x, y)) ==
0N/A * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
0N/A * implies that <tt>compare(x, y)</tt> must throw an exception if and only
0N/A * if <tt>compare(y, x)</tt> throws an exception.)<p>
0N/A *
0N/A * The implementor must also ensure that the relation is transitive:
0N/A * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
0N/A * <tt>compare(x, z)&gt;0</tt>.<p>
0N/A *
0N/A * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
0N/A * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
0N/A * <tt>z</tt>.<p>
0N/A *
0N/A * It is generally the case, but <i>not</i> strictly required that
0N/A * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
0N/A * any comparator that violates this condition should clearly indicate
0N/A * this fact. The recommended language is "Note: this comparator
0N/A * imposes orderings that are inconsistent with equals."
0N/A *
0N/A * @param o1 the first object to be compared.
0N/A * @param o2 the second object to be compared.
0N/A * @return a negative integer, zero, or a positive integer as the
0N/A * first argument is less than, equal to, or greater than the
0N/A * second.
0N/A * @throws NullPointerException if an argument is null and this
0N/A * comparator does not permit null arguments
0N/A * @throws ClassCastException if the arguments' types prevent them from
0N/A * being compared by this comparator.
0N/A */
0N/A int compare(T o1, T o2);
0N/A
0N/A /**
0N/A * Indicates whether some other object is &quot;equal to&quot; this
0N/A * comparator. This method must obey the general contract of
0N/A * {@link Object#equals(Object)}. Additionally, this method can return
0N/A * <tt>true</tt> <i>only</i> if the specified object is also a comparator
0N/A * and it imposes the same ordering as this comparator. Thus,
0N/A * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
0N/A * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
0N/A * <tt>o1</tt> and <tt>o2</tt>.<p>
0N/A *
0N/A * Note that it is <i>always</i> safe <i>not</i> to override
0N/A * <tt>Object.equals(Object)</tt>. However, overriding this method may,
0N/A * in some cases, improve performance by allowing programs to determine
0N/A * that two distinct comparators impose the same order.
0N/A *
0N/A * @param obj the reference object with which to compare.
0N/A * @return <code>true</code> only if the specified object is also
0N/A * a comparator and it imposes the same ordering as this
0N/A * comparator.
0N/A * @see Object#equals(Object)
0N/A * @see Object#hashCode()
0N/A */
0N/A boolean equals(Object obj);
0N/A}