Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Comparing |
|
| 0.0;0 | ||||
Comparing$1 |
|
| 0.0;0 | ||||
Comparing$NaturalComparator |
|
| 0.0;0 | ||||
Comparing$NullSafeComparator |
|
| 0.0;0 |
1 | /* | |
2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
3 | Licensed under the Academic Free License version 3.0 | |
4 | */ | |
5 | ||
6 | package jaggregate; | |
7 | ||
8 | import java.io.Serializable; | |
9 | import java.util.Comparator; | |
10 | ||
11 | import static jaggregate.internal.ArgumentChecks.*; | |
12 | ||
13 | /** | |
14 | * Utility class that offers common {@linkplain Comparator comparators}. | |
15 | * | |
16 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
17 | * @version $Id: Comparing.java,v 1.5 2008/10/03 19:01:23 pholser Exp $ | |
18 | */ | |
19 | public class Comparing { | |
20 | /** | |
21 | * Discourages instantiation. | |
22 | * | |
23 | * @throws UnsupportedOperationException always | |
24 | */ | |
25 | 1 | protected Comparing() { |
26 | 1 | throw new UnsupportedOperationException(); |
27 | } | |
28 | ||
29 | /** | |
30 | * Answers a comparator that defends against {@code null} actual arguments to its | |
31 | * wrapped comparator's {@link Comparator#compare(Object,Object) compare} method. | |
32 | * <p/> | |
33 | * A {@code null} value compares "less than" a non-{@code null} value. | |
34 | * | |
35 | * @param <T> the type of elements to be compared | |
36 | * @param comparator the comparator to wrap | |
37 | * @return the wrapped comparator | |
38 | * @throws NullPointerException if {@code comparator} is {@code null} | |
39 | */ | |
40 | public static <T> Comparator<T> nullSafe( Comparator<T> comparator ) { | |
41 | 756 | ensureNotNull( comparator, "comparator" ); |
42 | ||
43 | 755 | return new NullSafeComparator<T>( comparator ); |
44 | } | |
45 | ||
46 | /** | |
47 | * Answers a comparator that compares its two {@link Comparable} arguments | |
48 | * according to their natural ordering; that is, by how they answer | |
49 | * {@link Comparable#compareTo(Object) compareTo}. The comparator is | |
50 | * {@linkplain #nullSafe(Comparator) "null safe"}. | |
51 | * | |
52 | * @param <T> the type of elements to be compared | |
53 | * @return a comparator of comparables that answers based on the arguments' | |
54 | * natural ordering | |
55 | * @see #nullSafe(Comparator) | |
56 | */ | |
57 | public static <T extends Comparable<? super T>> Comparator<T> byNaturalOrdering() { | |
58 | 751 | return nullSafe( new NaturalComparator<T>() ); |
59 | } | |
60 | ||
61 | private static class NullSafeComparator<T> implements Comparator<T>, Serializable { | |
62 | private static final long serialVersionUID = -1L; | |
63 | private final Comparator<? super T> wrapped; | |
64 | ||
65 | 755 | NullSafeComparator( Comparator<? super T> wrapped ) { |
66 | 755 | this.wrapped = wrapped; |
67 | 755 | } |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | public int compare( T first, T second ) { | |
73 | 3714 | if ( first == null ) |
74 | 65 | return second == null ? 0 : -1; |
75 | ||
76 | 3649 | return second == null ? 1 : wrapped.compare( first, second ); |
77 | } | |
78 | } | |
79 | ||
80 | 5137 | private static class NaturalComparator<T extends Comparable<? super T>> |
81 | implements Comparator<T>, Serializable { | |
82 | ||
83 | private static final long serialVersionUID = -1L; | |
84 | ||
85 | /** | |
86 | * {@inheritDoc} | |
87 | */ | |
88 | public int compare( T first, T second ) { | |
89 | 3635 | return first.compareTo( second ); |
90 | } | |
91 | } | |
92 | } |