Path: blob/master/src/java.base/share/classes/java/util/Comparators.java
41152 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.util;2526import java.io.Serializable;27import java.util.function.BinaryOperator;28import java.util.function.Function;29import java.util.function.ToDoubleFunction;30import java.util.function.ToIntFunction;31import java.util.function.ToLongFunction;3233/**34* Package private supporting class for {@link Comparator}.35*/36class Comparators {37private Comparators() {38throw new AssertionError("no instances");39}4041/**42* Compares {@link Comparable} objects in natural order.43*44* @see Comparable45*/46enum NaturalOrderComparator implements Comparator<Comparable<Object>> {47INSTANCE;4849@Override50public int compare(Comparable<Object> c1, Comparable<Object> c2) {51return c1.compareTo(c2);52}5354@Override55public Comparator<Comparable<Object>> reversed() {56return Comparator.reverseOrder();57}58}5960/**61* Null-friendly comparators62*/63static final class NullComparator<T> implements Comparator<T>, Serializable {64@java.io.Serial65private static final long serialVersionUID = -7569533591570686392L;66private final boolean nullFirst;67// if null, non-null Ts are considered equal68@SuppressWarnings("serial") // Not statically typed as Serializable69private final Comparator<T> real;7071@SuppressWarnings("unchecked")72NullComparator(boolean nullFirst, Comparator<? super T> real) {73this.nullFirst = nullFirst;74this.real = (Comparator<T>) real;75}7677@Override78public int compare(T a, T b) {79if (a == null) {80return (b == null) ? 0 : (nullFirst ? -1 : 1);81} else if (b == null) {82return nullFirst ? 1: -1;83} else {84return (real == null) ? 0 : real.compare(a, b);85}86}8788@Override89public Comparator<T> thenComparing(Comparator<? super T> other) {90Objects.requireNonNull(other);91return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other));92}9394@Override95public Comparator<T> reversed() {96return new NullComparator<>(!nullFirst, real == null ? null : real.reversed());97}98}99}100101102