1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.comparator;
18
19 import java.io.Serializable;
20 import java.util.Comparator;
21
22 import org.apache.commons.functor.BinaryFunction;
23
24
25
26
27
28
29
30
31 public final class ComparatorFunction<T> implements BinaryFunction<T, T, Integer>, Serializable {
32
33
34
35
36 public static final ComparatorFunction<Comparable<?>> INSTANCE = ComparatorFunction.<Comparable<?>>instance();
37
38
39
40
41 private static final long serialVersionUID = 1642024869929206095L;
42
43 private final Comparator<? super T> comparator;
44
45
46
47
48
49 public ComparatorFunction(Comparator<? super T> comparator) {
50 if (comparator == null) {
51 throw new IllegalArgumentException("Comparator must not be null");
52 }
53 this.comparator = comparator;
54 }
55
56
57
58
59 public Integer evaluate(T left, T right) {
60 return Integer.valueOf(comparator.compare(left, right));
61 }
62
63
64
65
66 public boolean equals(Object that) {
67 return that == this || (that instanceof ComparatorFunction<?> && equals((ComparatorFunction<?>) that));
68 }
69
70
71
72
73
74
75 public boolean equals(ComparatorFunction<?> that) {
76 return null != that && comparator.equals(that.comparator);
77 }
78
79
80
81
82 public int hashCode() {
83 return "ComparatorFunction".hashCode() ^ comparator.hashCode();
84 }
85
86
87
88
89 public String toString() {
90 return "ComparatorFunction<" + comparator + ">";
91 }
92
93
94
95
96
97 @SuppressWarnings("unchecked")
98 public static <T> ComparatorFunction<T> instance() {
99 return new ComparatorFunction<T>(ComparableComparator.INSTANCE);
100 }
101 }