author | zmajo |
Fri, 03 Jul 2015 07:23:45 +0200 | |
changeset 31671 | 362e0c0acece |
parent 25859 | 3317bb8137f4 |
child 32649 | 2ee9017c7597 |
permissions | -rw-r--r-- |
16064 | 1 |
/* |
18571 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
16064 | 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 |
/** |
|
18571 | 35 |
* Package private supporting class for {@link Comparator}. |
16064 | 36 |
*/ |
18571 | 37 |
class Comparators { |
16064 | 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 |
*/ |
|
18571 | 47 |
enum NaturalOrderComparator implements Comparator<Comparable<Object>> { |
16064 | 48 |
INSTANCE; |
49 |
||
50 |
@Override |
|
51 |
public int compare(Comparable<Object> c1, Comparable<Object> c2) { |
|
52 |
return c1.compareTo(c2); |
|
53 |
} |
|
54 |
||
18571 | 55 |
@Override |
56 |
public Comparator<Comparable<Object>> reversed() { |
|
57 |
return Comparator.reverseOrder(); |
|
58 |
} |
|
16064 | 59 |
} |
60 |
||
61 |
/** |
|
18571 | 62 |
* Null-friendly comparators |
16064 | 63 |
*/ |
18571 | 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; |
|
16064 | 69 |
|
18571 | 70 |
@SuppressWarnings("unchecked") |
71 |
NullComparator(boolean nullFirst, Comparator<? super T> real) { |
|
72 |
this.nullFirst = nullFirst; |
|
73 |
this.real = (Comparator<T>) real; |
|
74 |
} |
|
16064 | 75 |
|
18571 | 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 |
} |
|
16064 | 86 |
|
18571 | 87 |
@Override |
88 |
public Comparator<T> thenComparing(Comparator<? super T> other) { |
|
89 |
Objects.requireNonNull(other); |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18571
diff
changeset
|
90 |
return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other)); |
18571 | 91 |
} |
16064 | 92 |
|
18571 | 93 |
@Override |
94 |
public Comparator<T> reversed() { |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18571
diff
changeset
|
95 |
return new NullComparator<>(!nullFirst, real == null ? null : real.reversed()); |
18571 | 96 |
} |
16064 | 97 |
} |
98 |
} |