< prev index next >

src/java.base/share/classes/java/util/Comparators.java

Print this page




   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     final static class NullComparator<T> implements Comparator<T>, Serializable {
  65         private static final long serialVersionUID = -7569533591570686392L;
  66         private final boolean nullFirst;
  67         // if null, non-null Ts are considered equal
  68         private final Comparator<T> real;
  69 
  70         @SuppressWarnings("unchecked")
  71         NullComparator(boolean nullFirst, Comparator<? super T> real) {
  72             this.nullFirst = nullFirst;
  73             this.real = (Comparator<T>) real;
  74         }
  75 
  76         @Override
  77         public int compare(T a, T b) {




   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) {


< prev index next >