|
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 @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 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) { |
|
78 if (a == null) { |
|
79 return (b == null) ? 0 : (nullFirst ? -1 : 1); |
|
80 } else if (b == null) { |
|
81 return nullFirst ? 1: -1; |
|
82 } else { |
|
83 return (real == null) ? 0 : real.compare(a, b); |
|
84 } |
|
85 } |
|
86 |
|
87 @Override |
|
88 public Comparator<T> thenComparing(Comparator<? super T> other) { |
|
89 Objects.requireNonNull(other); |
|
90 return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other)); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public Comparator<T> reversed() { |
|
95 return new NullComparator<>(!nullFirst, real == null ? null : real.reversed()); |
|
96 } |
|
97 } |
|
98 } |