1 /*
   2  * Copyright (c) 2012, 2013, 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 
  56     /**
  57      * Null-friendly comparators
  58      */
  59     final static class NullComparator<T> implements Comparator<T>, Serializable {
  60         private static final long serialVersionUID = -7569533591570686392L;
  61         private final boolean nullFirst;
  62         // if null, non-null Ts are considered equal
  63         private final Comparator<T> real;
  64 
  65         @SuppressWarnings("unchecked")
  66         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  67             this.nullFirst = nullFirst;
  68             this.real = (Comparator<T>) real;
  69         }
  70 
  71         @Override
  72         public int compare(T a, T b) {
  73             if (a == null) {
  74                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
  75             } else if (b == null) {
  76                 return nullFirst ? 1: -1;
  77             } else {
  78                 return (real == null) ? 0 : real.compare(a, b);
  79             }
  80         }
  81 
  82         @Override
  83         public Comparator<T> thenComparing(Comparator<? super T> other) {
  84             Objects.requireNonNull(other);
  85             return new NullComparator(nullFirst, real == null ? other : real.thenComparing(other));
  86         }
  87 
  88         @Override
  89         public Comparator<T> reversed() {
  90             return new NullComparator(!nullFirst, real == null ? null : real.reversed());
  91         }
  92     }
  93 }