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 javany.util;
  26 
  27 import java.io.Serializable;
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 import java.util.Objects;
  31 
  32 /**
  33  * Package private supporting class for {@link Comparator}.
  34  */
  35 class Comparators {
  36     private Comparators() {
  37         throw new AssertionError("no instances");
  38     }
  39 
  40     // natural order comparisons (package-private - exposed via Comparator.naturalOrder())
  41 
  42     @SuppressWarnings("unchecked")
  43     static <any T> Comparator<T> naturalOrder() {
  44         return (Comparator<T>) comparators.get(Comparator<T>.class);
  45     }
  46 
  47     private static final Map<Class<?>, Object> comparators = new HashMap<>();
  48 
  49     @SuppressWarnings("unchecked")
  50     private static <any T> void putComparator(Comparator<T> comparator) {
  51         comparators.put(Comparator<T>.class, comparator);
  52     }
  53 
  54     static {
  55         putComparator((Comparator<boolean>) Boolean::compare);
  56         putComparator((Comparator<byte>) Byte::compare);
  57         putComparator((Comparator<short>) Short::compare);
  58         putComparator((Comparator<char>) Character::compare);
  59         putComparator((Comparator<int>) Integer::compare);
  60         putComparator((Comparator<long>) Long::compare);
  61         putComparator((Comparator<float>) Float::compare);
  62         putComparator((Comparator<double>) Double::compare);
  63         putComparator(new Comparator<Object>() {
  64             @Override
  65             public int compare(Object a, Object b) {
  66                 if (a instanceof Comparable && b instanceof Comparable) {
  67                     @SuppressWarnings("unchecked")
  68                     Comparable<Object> c = (Comparable<Object>)a;
  69                     try {
  70                         return c.compareTo(b);
  71                     } catch (Exception e) {
  72                         throw new IllegalArgumentException(
  73                             "Can't compare mutually non-Comparable objects", e);
  74                     }
  75                 } else {
  76                     throw new IllegalArgumentException(
  77                         "Can't compare non-Comparable objects");
  78                 }
  79             }
  80         });
  81     }
  82 
  83     /**
  84      * Reverses the order of given Comparator.
  85      *
  86      * @see Comparable
  87      */
  88     static class ReverseComparator<any T> implements Comparator<T> {
  89 
  90         private final Comparator<T> comparator;
  91 
  92         ReverseComparator(Comparator<T> comparator) {
  93             this.comparator = comparator;
  94         }
  95 
  96         @Override
  97         public int compare(T c1, T c2) {
  98 
  99             return comparator.compare(c2, c1);
 100         }
 101 
 102         @Override
 103         public Comparator<T> reversed() {
 104             return comparator;
 105         }
 106     }
 107 
 108     /**
 109      * Null-friendly comparators
 110      */
 111     final static class NullComparator<T> implements Comparator<T>, Serializable {
 112         private static final long serialVersionUID = -7569533591570686392L;
 113         private final boolean nullFirst;
 114         // if null, non-null Ts are considered equal
 115         private final Comparator<T> real;
 116 
 117         @SuppressWarnings("unchecked")
 118         NullComparator(boolean nullFirst, Comparator<? super T> real) {
 119             this.nullFirst = nullFirst;
 120             this.real = (Comparator<T>) real;
 121         }
 122 
 123         @Override
 124         public int compare(T a, T b) {
 125             if (a == null) {
 126                 return (b == null) ? 0 : (nullFirst ? -1 : 1);
 127             } else if (b == null) {
 128                 return nullFirst ? 1: -1;
 129             } else {
 130                 return (real == null) ? 0 : real.compare(a, b);
 131             }
 132         }
 133 
 134         @Override
 135         public Comparator<T> thenComparing(Comparator<? super T> other) {
 136             Objects.requireNonNull(other);
 137             return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other));
 138         }
 139 
 140         @Override
 141         public Comparator<T> reversed() {
 142             return new NullComparator<>(!nullFirst, real == null ? null : real.reversed());
 143         }
 144     }
 145 }