src/java.base/share/classes/java/util/Comparators.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58520 e036ee8bae56
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     4
 *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    10
 *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    15
 * accompanied this code).
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    16
 *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    20
 *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    23
 * questions.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    24
 */
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    25
package java.util;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    26
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    27
import java.io.Serializable;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    28
import java.util.function.BinaryOperator;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    29
import java.util.function.Function;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    30
import java.util.function.ToDoubleFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    31
import java.util.function.ToIntFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    32
import java.util.function.ToLongFunction;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    33
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    34
/**
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    35
 * Package private supporting class for {@link Comparator}.
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    36
 */
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    37
class Comparators {
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    38
    private Comparators() {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    39
        throw new AssertionError("no instances");
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    40
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    41
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    42
    /**
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    43
     * Compares {@link Comparable} objects in natural order.
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    44
     *
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    45
     * @see Comparable
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    46
     */
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    47
    enum NaturalOrderComparator implements Comparator<Comparable<Object>> {
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    48
        INSTANCE;
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    49
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    50
        @Override
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    51
        public int compare(Comparable<Object> c1, Comparable<Object> c2) {
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    52
            return c1.compareTo(c2);
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    53
        }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    54
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    55
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    56
        public Comparator<Comparable<Object>> reversed() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    57
            return Comparator.reverseOrder();
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    58
        }
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    59
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    60
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    61
    /**
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    62
     * Null-friendly comparators
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    63
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    64
    static final class NullComparator<T> implements Comparator<T>, Serializable {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
    65
        @java.io.Serial
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    66
        private static final long serialVersionUID = -7569533591570686392L;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    67
        private final boolean nullFirst;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    68
        // if null, non-null Ts are considered equal
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
    69
        @SuppressWarnings("serial") // Not statically typed as Serializable
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    70
        private final Comparator<T> real;
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    71
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    72
        @SuppressWarnings("unchecked")
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    73
        NullComparator(boolean nullFirst, Comparator<? super T> real) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    74
            this.nullFirst = nullFirst;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    75
            this.real = (Comparator<T>) real;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    76
        }
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    77
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    78
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    79
        public int compare(T a, T b) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    80
            if (a == null) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    81
                return (b == null) ? 0 : (nullFirst ? -1 : 1);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    82
            } else if (b == null) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    83
                return nullFirst ? 1: -1;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    84
            } else {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    85
                return (real == null) ? 0 : real.compare(a, b);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    86
            }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    87
        }
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    88
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    89
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    90
        public Comparator<T> thenComparing(Comparator<? super T> other) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    91
            Objects.requireNonNull(other);
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18571
diff changeset
    92
            return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other));
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    93
        }
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    94
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    95
        @Override
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    96
        public Comparator<T> reversed() {
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18571
diff changeset
    97
            return new NullComparator<>(!nullFirst, real == null ? null : real.reversed());
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 16858
diff changeset
    98
        }
16064
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
    99
    }
ef93558b0d63 8001667: Comparator combinators and extension methods
mduigou
parents:
diff changeset
   100
}