1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util;
  26 
  27 import java.io.Serializable;
  28 import java.util.function.BinaryOperator;
  29 import java.util.function.Function;
  30 import java.util.function.ToDoubleFunction;
  31 import java.util.function.ToIntFunction;
  32 import java.util.function.ToLongFunction;
  33 
  34 /**
  35  * Package private supporting class for {@link Comparator}.
  36  */
  37 class Comparators {
  38     private Comparators() {
  39         throw new AssertionError("no instances");
  40     }
  41 
  42     /**
  43      * Compares {@link Comparable} objects in natural order.
  44      *
  45      * @see Comparable
  46      */
  47     enum NaturalOrderComparator implements Comparator<Comparable<Object>> {
  48         INSTANCE;
  49 
  50         @Override
  51         public int compare(Comparable<Object> c1, Comparable<Object> c2) {
  52             return c1.compareTo(c2);
  53         }
  54 
  55         @Override
  56         public Comparator<Comparable<Object>> reversed() {
  57             return Comparator.reverseOrder();
  58         }
  59     }
  60 
  61     /**
  62      * Null-friendly comparators
  63      */
  64     static final class NullComparator<T> implements Comparator<T>, Serializable {
  65         @java.io.Serial
  66         private static final long serialVersionUID = -7569533591570686392L;
  67         private final boolean nullFirst;
  68         // if null, non-null Ts are considered equal
  69         @SuppressWarnings("serial") // Not statically typed as Serializable
  70         private final Comparator<T> real;
  71 
  72         @SuppressWarnings("unchecked")
  73         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  74             this.nullFirst = nullFirst;
  75             this.real = (Comparator<T>) real;
  76         }
  77 
  78         @Override
  79         public int compare(T a, T b) {
  80             if (a == null) {
  81                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
  82             } else if (b == null) {
  83                 return nullFirst ? 1: -1;
  84             } else {
  85                 return (real == null) ? 0 : real.compare(a, b);
  86             }
  87         }
  88 
  89         @Override
  90         public Comparator<T> thenComparing(Comparator<? super T> other) {
  91             Objects.requireNonNull(other);
  92             return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other));
  93         }
  94 
  95         @Override
  96         public Comparator<T> reversed() {
  97             return new NullComparator<>(!nullFirst, real == null ? null : real.reversed());
  98         }
  99     }
 100 }