jdk/src/share/classes/java/util/Comparator.java
author mduigou
Fri, 08 Mar 2013 15:45:06 -0800
changeset 16064 ef93558b0d63
parent 15647 314007859004
child 18571 8e3cb3c46ae8
permissions -rw-r--r--
8001667: Comparator combinators and extension methods Reviewed-by: mduigou, briangoetz Contributed-by: henry.jen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
    28
import java.util.function.Function;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
    29
import java.util.function.ToIntFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
    30
import java.util.function.ToLongFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
    31
import java.util.function.ToDoubleFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A comparison function, which imposes a <i>total ordering</i> on some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * collection of objects.  Comparators can be passed to a sort method (such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * over the sort order.  Comparators can also be used to control the order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * certain data structures (such as {@link SortedSet sorted sets} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SortedMap sorted maps}), or to provide an ordering for collections of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * objects that don't have a {@link Comparable natural ordering}.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <tt>S</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Caution should be exercised when using a comparator capable of imposing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ordering inconsistent with equals to order a sorted set (or sorted map).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is used with elements (or keys) drawn from a set <tt>S</tt>.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the sorted set (or sorted map) will behave "strangely."  In particular the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * sorted set (or sorted map) will violate the general contract for set (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * map), which is defined in terms of <tt>equals</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * For example, suppose one adds two elements {@code a} and {@code b} such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@code (a.equals(b) && c.compare(a, b) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to an empty {@code TreeSet} with comparator {@code c}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The second {@code add} operation will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * true (and the size of the tree set will increase) because {@code a} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * {@code b} are not equivalent from the tree set's perspective, even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * this is contrary to the specification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link Set#add Set.add} method.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Note: It is generally a good idea for comparators to also implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * serializable data structures (like {@link TreeSet}, {@link TreeMap}).  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * order for the data structure to serialize successfully, the comparator (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * provided) must implement <tt>Serializable</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * For the mathematically inclined, the <i>relation</i> that defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * given set of objects <tt>S</tt> is:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *       {(x, y) such that c.compare(x, y) &lt;= 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * </pre> The <i>quotient</i> for this total order is:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *       {(x, y) such that c.compare(x, y) == 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * It follows immediately from the contract for <tt>compare</tt> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * imposed ordering is a <i>total order</i> on <tt>S</tt>.  When we say that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * equals</i>, we mean that the quotient for the ordering is the equivalence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * relation defined by the objects' {@link Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * equals(Object)} method(s):<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     {(x, y) such that x.equals(y)}. </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>Unlike {@code Comparable}, a comparator may optionally permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * comparison of null arguments, while maintaining the requirements for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * an equivalence relation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>This interface is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @param <T> the type of objects that may be compared by this comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 5506
diff changeset
   106
@FunctionalInterface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
public interface Comparator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Compares its two arguments for order.  Returns a negative integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * zero, or a positive integer as the first argument is less than, equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * to, or greater than the second.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * In the foregoing description, the notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <tt>0</tt>, or <tt>1</tt> according to whether the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <i>expression</i> is negative, zero or positive.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The implementor must ensure that <tt>sgn(compare(x, y)) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * implies that <tt>compare(x, y)</tt> must throw an exception if and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * if <tt>compare(y, x)</tt> throws an exception.)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The implementor must also ensure that the relation is transitive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <tt>compare(x, z)&gt;0</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <tt>z</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * It is generally the case, but <i>not</i> strictly required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * any comparator that violates this condition should clearly indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * this fact.  The recommended language is "Note: this comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * imposes orderings that are inconsistent with equals."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param o1 the first object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param o2 the second object to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         first argument is less than, equal to, or greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws NullPointerException if an argument is null and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *         comparator does not permit null arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws ClassCastException if the arguments' types prevent them from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *         being compared by this comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    int compare(T o1, T o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Indicates whether some other object is &quot;equal to&quot; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * comparator.  This method must obey the general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * {@link Object#equals(Object)}.  Additionally, this method can return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <tt>true</tt> <i>only</i> if the specified object is also a comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * and it imposes the same ordering as this comparator.  Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <tt>o1</tt> and <tt>o2</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Note that it is <i>always</i> safe <i>not</i> to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <tt>Object.equals(Object)</tt>.  However, overriding this method may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * in some cases, improve performance by allowing programs to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * that two distinct comparators impose the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param   obj   the reference object with which to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return  <code>true</code> only if the specified object is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *          a comparator and it imposes the same ordering as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *          comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    boolean equals(Object obj);
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   173
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   174
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   175
     * Returns a comparator that imposes the reverse ordering of this
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   176
     * comparator.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   177
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   178
     * @return A comparator that imposes the reverse ordering of this
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   179
     *         comparator.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   180
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   181
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   182
    default Comparator<T> reverseOrder() {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   183
        return Collections.reverseOrder(this);
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   184
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   185
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   186
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   187
     * Constructs a lexicographic order comparator with another comparator.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   188
     * For example, a {@code Comparator<Person> byLastName} can be composed
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   189
     * with another {@code Comparator<Person> byFirstName}, then {@code
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   190
     * byLastName.thenComparing(byFirstName)} creates a {@code
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   191
     * Comparator<Person>} which sorts by last name, and for equal last names
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   192
     * sorts by first name.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   193
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   194
     * @param other the other comparator used when equals on this.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   195
     * @throws NullPointerException if the argument is null.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   196
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   197
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   198
    default Comparator<T> thenComparing(Comparator<? super T> other) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   199
        return Comparators.compose(this, other);
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   200
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   201
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   202
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   203
     * Constructs a lexicographic order comparator with a function that
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   204
     * extracts a {@code Comparable} key.  This default implementation calls
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   205
     * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   206
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   207
     * @param <U> the {@link Comparable} type for comparison
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   208
     * @param keyExtractor the function used to extract the {@link Comparable} sort key
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   209
     * @throws NullPointerException if the argument is null.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   210
     * @see Comparators#comparing(Function)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   211
     * @see #thenComparing(Comparator)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   212
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   213
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   214
    default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   215
        return thenComparing(Comparators.comparing(keyExtractor));
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   216
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   217
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   218
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   219
     * Constructs a lexicographic order comparator with a function that
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   220
     * extracts a {@code int} value.  This default implementation calls {@code
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   221
     * thenComparing(this, Comparators.comparing(keyExtractor))}.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   222
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   223
     * @param keyExtractor the function used to extract the integer value
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   224
     * @throws NullPointerException if the argument is null.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   225
     * @see Comparators#comparing(ToIntFunction)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   226
     * @see #thenComparing(Comparator)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   227
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   228
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   229
    default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   230
        return thenComparing(Comparators.comparing(keyExtractor));
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   231
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   232
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   233
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   234
     * Constructs a lexicographic order comparator with a function that
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   235
     * extracts a {@code long} value.  This default implementation calls
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   236
     * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   237
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   238
     * @param keyExtractor the function used to extract the long value
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   239
     * @throws NullPointerException if the argument is null.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   240
     * @see Comparators#comparing(ToLongFunction)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   241
     * @see #thenComparing(Comparator)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   242
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   243
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   244
    default Comparator<T> thenComparing(ToLongFunction<? super T> keyExtractor) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   245
        return thenComparing(Comparators.comparing(keyExtractor));
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   246
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   247
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   248
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   249
     * Constructs a lexicographic order comparator with a function that
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   250
     * extracts a {@code double} value.  This default implementation calls
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   251
     * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   252
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   253
     * @param keyExtractor the function used to extract the double value
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   254
     * @throws NullPointerException if the argument is null.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   255
     * @see Comparators#comparing(ToDoubleFunction)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   256
     * @see #thenComparing(Comparator)
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   257
     * @since 1.8
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   258
     */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   259
    default Comparator<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   260
        return thenComparing(Comparators.comparing(keyExtractor));
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents: 15647
diff changeset
   261
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}