-
Notifications
You must be signed in to change notification settings - Fork 587
/
Comparators.java
475 lines (404 loc) · 15.6 KB
/
Comparators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.block.factory;
import java.util.Collection;
import java.util.Comparator;
import org.eclipse.collections.api.block.SerializableComparator;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.set.sorted.SortedSetIterable;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.comparator.FunctionComparator;
public final class Comparators
{
private static final SerializableComparator<?> COMPARABLE_COMPARATOR = new ComparableComparator();
private static final SerializableComparator<?> NATURAL_ORDER_COMPARATOR = new NaturalOrderComparator();
private static final SerializableComparator<?> REVERSE_NATURAL_ORDER_COMPARATOR = new ReverseComparator(NATURAL_ORDER_COMPARATOR);
private static final SerializableComparator<?> POWER_SET_COMPARATOR = new PowerSetComparator();
private static final SerializableComparator<Collection<?>> ASCENDING_COLLECTION_SIZE_COMPARATOR = new AscendingCollectionSizeComparator();
private static final SerializableComparator<Collection<?>> DESCENDING_COLLECTION_SIZE_COMPARATOR = new DescendingCollectionSizeComparator();
private Comparators()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
/**
* @since 10.0
*/
public static <T> SerializableComparator<T> comparableComparator()
{
return (SerializableComparator<T>) COMPARABLE_COMPARATOR;
}
/**
* @since 10.0
*/
public static <T> Comparator<? super T> comparableComparatorIfNull(Comparator<? super T> comparator)
{
return comparator == null ? Comparators.comparableComparator() : comparator;
}
/**
* Uses the natural compareTo methods of the objects which will throw if there are any nulls.
*/
public static <T> SerializableComparator<T> naturalOrder()
{
return (SerializableComparator<T>) NATURAL_ORDER_COMPARATOR;
}
/**
* Uses the natural compareTo methods of the objects which will throw if there are any nulls.
*/
public static <T> SerializableComparator<T> reverseNaturalOrder()
{
return (SerializableComparator<T>) REVERSE_NATURAL_ORDER_COMPARATOR;
}
/**
* @param comparator original comparator whose order will be reversed
* @return A comparator that reverses the order of any other Serializable Comparator.
*/
public static <T> SerializableComparator<T> reverse(Comparator<T> comparator)
{
if (comparator == null)
{
throw new NullPointerException();
}
return new ReverseComparator<>(comparator);
}
public static <T> SerializableComparator<T> safeNullsLow(Comparator<T> notNullSafeComparator)
{
return new SafeNullsLowComparator<>(notNullSafeComparator);
}
public static <T> SerializableComparator<T> safeNullsHigh(Comparator<T> notNullSafeComparator)
{
return new SafeNullsHighComparator<>(notNullSafeComparator);
}
public static <T> SerializableComparator<T> chain(Comparator<T>... comparators)
{
if (comparators.length == 0)
{
throw new IllegalArgumentException("Nothing to chain");
}
return new ChainedComparator<>(comparators);
}
public static <T, V extends Comparable<? super V>> SerializableComparator<T> fromFunctions(
Function<? super T, ? extends V> one)
{
return Comparators.byFunction(one);
}
public static <T, V1 extends Comparable<? super V1>, V2 extends Comparable<? super V2>> SerializableComparator<T> fromFunctions(
Function<? super T, ? extends V1> one,
Function<? super T, ? extends V2> two)
{
return Comparators.chain(
Comparators.byFunction(one),
Comparators.byFunction(two));
}
public static <T, V1 extends Comparable<? super V1>, V2 extends Comparable<? super V2>, V3 extends Comparable<? super V3>> SerializableComparator<T> fromFunctions(
Function<? super T, ? extends V1> one,
Function<? super T, ? extends V2> two,
Function<? super T, ? extends V3> three)
{
return Comparators.chain(
Comparators.byFunction(one),
Comparators.byFunction(two),
Comparators.byFunction(three));
}
public static <T> SerializableComparator<SortedSetIterable<T>> powerSet()
{
return (SerializableComparator<SortedSetIterable<T>>) POWER_SET_COMPARATOR;
}
public static SerializableComparator<Collection<?>> ascendingCollectionSizeComparator()
{
return ASCENDING_COLLECTION_SIZE_COMPARATOR;
}
public static SerializableComparator<Collection<?>> descendingCollectionSizeComparator()
{
return DESCENDING_COLLECTION_SIZE_COMPARATOR;
}
/**
* Creates a comparator for pairs by using an existing comparator that only compares the first element of the pair
*
* @param comparator original comparator that compares the first element of the pair
* @return A comparator that compares pairs only by their first element
*/
public static <T> SerializableComparator<Pair<T, ?>> byFirstOfPair(Comparator<? super T> comparator)
{
return new ByFirstOfPairComparator<>(comparator);
}
/**
* Creates a comparator for pairs by using an existing comparator that only compares the second element of the pair
*
* @param comparator original comparator that compares the second element of the pair
* @return A comparator that compares pairs only by their second element
*/
public static <T> SerializableComparator<Pair<?, T>> bySecondOfPair(Comparator<? super T> comparator)
{
return new BySecondOfPairComparator<>(comparator);
}
private static final class NaturalOrderComparator<T extends Comparable<T>> implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
@Override
public int compare(T o1, T o2)
{
if (o1 == null || o2 == null)
{
throw new NullPointerException();
}
return o1.compareTo(o2);
}
}
private static final class ReverseComparator<T> implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
private final Comparator<T> comparator;
private ReverseComparator(Comparator<T> comparator)
{
this.comparator = comparator;
}
@Override
public int compare(T o1, T o2)
{
return this.comparator.compare(o2, o1);
}
}
private static final class SafeNullsLowComparator<T>
implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
private final Comparator<T> notNullSafeComparator;
private SafeNullsLowComparator(Comparator<T> newNotNullSafeComparator)
{
this.notNullSafeComparator = newNotNullSafeComparator;
}
@Override
public int compare(T value1, T value2)
{
if (value1 != null && value2 != null)
{
return this.notNullSafeComparator.compare(value1, value2);
}
if (value1 == null && value2 == null)
{
return 0;
}
return value1 == null ? -1 : 1;
}
}
private static final class SafeNullsHighComparator<T>
implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
private final Comparator<T> notNullSafeComparator;
private SafeNullsHighComparator(Comparator<T> newNotNullSafeComparator)
{
this.notNullSafeComparator = newNotNullSafeComparator;
}
@Override
public int compare(T value1, T value2)
{
if (value1 != null && value2 != null)
{
return this.notNullSafeComparator.compare(value1, value2);
}
if (value1 == null && value2 == null)
{
return 0;
}
return value1 == null ? 1 : -1;
}
}
private static final class ChainedComparator<T>
implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
private final Comparator<T>[] comparators;
private ChainedComparator(Comparator<T>[] comparators)
{
this.comparators = comparators;
}
@Override
public int compare(T value1, T value2)
{
for (Comparator<T> comparator : this.comparators)
{
int result = comparator.compare(value1, value2);
if (result != 0)
{
return result;
}
}
return 0;
}
}
private static final class PowerSetComparator<T> implements SerializableComparator<SortedSetIterable<T>>
{
private static final long serialVersionUID = 1L;
@Override
public int compare(SortedSetIterable<T> setA, SortedSetIterable<T> setB)
{
int compareTo = Integer.compare(setA.size(), setB.size());
if (compareTo == 0)
{
return setA.compareTo(setB);
}
return compareTo;
}
}
private static final class AscendingCollectionSizeComparator implements SerializableComparator<Collection<?>>
{
private static final long serialVersionUID = 1L;
@Override
public int compare(Collection<?> c1, Collection<?> c2)
{
return c1.size() - c2.size();
}
}
private static final class DescendingCollectionSizeComparator implements SerializableComparator<Collection<?>>
{
private static final long serialVersionUID = 1L;
@Override
public int compare(Collection<?> c1, Collection<?> c2)
{
return c2.size() - c1.size();
}
}
public static <T, V extends Comparable<? super V>> SerializableComparator<T> byFunction(Function<? super T, ? extends V> function)
{
if (function instanceof BooleanFunction)
{
return Functions.toBooleanComparator((BooleanFunction<T>) function);
}
if (function instanceof ByteFunction)
{
return Functions.toByteComparator((ByteFunction<T>) function);
}
if (function instanceof CharFunction)
{
return Functions.toCharComparator((CharFunction<T>) function);
}
if (function instanceof DoubleFunction)
{
return Functions.toDoubleComparator((DoubleFunction<T>) function);
}
if (function instanceof FloatFunction)
{
return Functions.toFloatComparator((FloatFunction<T>) function);
}
if (function instanceof IntFunction)
{
return Functions.toIntComparator((IntFunction<T>) function);
}
if (function instanceof LongFunction)
{
return Functions.toLongComparator((LongFunction<T>) function);
}
if (function instanceof ShortFunction)
{
return Functions.toShortComparator((ShortFunction<T>) function);
}
return Comparators.byFunction(function, naturalOrder());
}
public static <T> SerializableComparator<T> byBooleanFunction(BooleanFunction<T> function)
{
return Functions.toBooleanComparator(function);
}
public static <T> SerializableComparator<T> byByteFunction(ByteFunction<T> function)
{
return Functions.toByteComparator(function);
}
public static <T> SerializableComparator<T> byCharFunction(CharFunction<T> function)
{
return Functions.toCharComparator(function);
}
public static <T> SerializableComparator<T> byDoubleFunction(DoubleFunction<T> function)
{
return Functions.toDoubleComparator(function);
}
public static <T> SerializableComparator<T> byFloatFunction(FloatFunction<T> function)
{
return Functions.toFloatComparator(function);
}
public static <T> SerializableComparator<T> byIntFunction(IntFunction<T> function)
{
return Functions.toIntComparator(function);
}
public static <T> SerializableComparator<T> byLongFunction(LongFunction<T> function)
{
return Functions.toLongComparator(function);
}
public static <T> SerializableComparator<T> byShortFunction(ShortFunction<T> function)
{
return Functions.toShortComparator(function);
}
public static <T, V> SerializableComparator<T> byFunction(
Function<? super T, ? extends V> function,
Comparator<V> comparator)
{
return new FunctionComparator<>(function, comparator);
}
public static boolean nullSafeEquals(Object value1, Object value2)
{
return value1 == null ? value2 == null : value1.equals(value2);
}
public static <T extends Comparable<T>> int nullSafeCompare(T value1, T value2)
{
if (value1 != null && value2 != null)
{
return value1.compareTo(value2);
}
if (value1 == null && value2 == null)
{
return 0;
}
return value1 == null ? -1 : 1;
}
private static final class ByFirstOfPairComparator<T> implements SerializableComparator<Pair<T, ?>>
{
private static final long serialVersionUID = 1L;
private final Comparator<? super T> comparator;
private ByFirstOfPairComparator(Comparator<? super T> comparator)
{
this.comparator = comparator;
}
@Override
public int compare(Pair<T, ?> p1, Pair<T, ?> p2)
{
return this.comparator.compare(p1.getOne(), p2.getOne());
}
}
private static final class BySecondOfPairComparator<T> implements SerializableComparator<Pair<?, T>>
{
private static final long serialVersionUID = 1L;
private final Comparator<? super T> comparator;
private BySecondOfPairComparator(Comparator<? super T> comparator)
{
this.comparator = comparator;
}
@Override
public int compare(Pair<?, T> p1, Pair<?, T> p2)
{
return this.comparator.compare(p1.getTwo(), p2.getTwo());
}
}
private static class ComparableComparator<T> implements SerializableComparator<T>
{
private static final long serialVersionUID = 1L;
@Override
public int compare(T one, T two)
{
return ((Comparable<T>) one).compareTo(two);
}
}
}