3299 |
|
* @see Comparable |
3300 |
|
*/ |
3301 |
|
public static <T> Comparator<T> reverseOrder() { |
3302 |
< |
return (Comparator<T>) REVERSE_ORDER; |
3302 |
> |
return (Comparator<T>) ReverseComparator.REVERSE_ORDER; |
3303 |
|
} |
3304 |
|
|
3305 |
– |
private static final Comparator REVERSE_ORDER = new ReverseComparator(); |
3306 |
– |
|
3305 |
|
/** |
3306 |
|
* @serial include |
3307 |
|
*/ |
3308 |
< |
private static class ReverseComparator<T> |
3308 |
> |
private static class ReverseComparator |
3309 |
|
implements Comparator<Comparable<Object>>, Serializable { |
3310 |
|
|
3311 |
|
// use serialVersionUID from JDK 1.2.2 for interoperability |
3312 |
|
private static final long serialVersionUID = 7207038068494060240L; |
3313 |
|
|
3314 |
+ |
private static final ReverseComparator REVERSE_ORDER |
3315 |
+ |
= new ReverseComparator(); |
3316 |
+ |
|
3317 |
|
public int compare(Comparable<Object> c1, Comparable<Object> c2) { |
3318 |
|
return c2.compareTo(c1); |
3319 |
|
} |
3332 |
|
* comparator is also serializable or null). |
3333 |
|
* |
3334 |
|
* @return a comparator that imposes the reverse ordering of the |
3335 |
< |
* specified comparator. |
3335 |
> |
* specified comparator |
3336 |
|
* @since 1.5 |
3337 |
|
*/ |
3338 |
|
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) { |
3339 |
|
if (cmp == null) |
3340 |
|
return reverseOrder(); |
3341 |
|
|
3342 |
+ |
if (cmp instanceof ReverseComparator2) |
3343 |
+ |
return ((ReverseComparator2<T>)cmp).cmp; |
3344 |
+ |
|
3345 |
|
return new ReverseComparator2<T>(cmp); |
3346 |
|
} |
3347 |
|
|
3360 |
|
* |
3361 |
|
* @serial |
3362 |
|
*/ |
3363 |
< |
private Comparator<T> cmp; |
3363 |
> |
private final Comparator<T> cmp; |
3364 |
|
|
3365 |
|
ReverseComparator2(Comparator<T> cmp) { |
3366 |
|
assert cmp != null; |
3370 |
|
public int compare(T t1, T t2) { |
3371 |
|
return cmp.compare(t2, t1); |
3372 |
|
} |
3373 |
+ |
|
3374 |
+ |
public boolean equals(Object o) { |
3375 |
+ |
return (o == this) || |
3376 |
+ |
(o instanceof ReverseComparator2 && |
3377 |
+ |
cmp.equals(((ReverseComparator2)o).cmp)); |
3378 |
+ |
} |
3379 |
+ |
|
3380 |
+ |
public int hashCode() { |
3381 |
+ |
return cmp.hashCode() ^ Integer.MIN_VALUE; |
3382 |
+ |
} |
3383 |
|
} |
3384 |
|
|
3385 |
|
/** |