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 import org.apache.commons.functor.UnaryFunction;
24 import org.apache.commons.functor.adapter.RightBoundFunction;
25
26
27
28
29
30
31
32
33 public final class Max<T> implements BinaryFunction<T, T, T>, Serializable {
34
35
36
37
38 public static final Max<Comparable<?>> INSTANCE = Max.<Comparable<?>>instance();
39
40
41
42
43 private static final long serialVersionUID = 6514424464263828685L;
44
45 private final Comparator<T> comparator;
46
47
48
49
50 @SuppressWarnings("unchecked")
51 public Max() {
52 this(ComparableComparator.instance());
53 }
54
55
56
57
58
59 public Max(Comparator<T> comparator) {
60 if (comparator == null) {
61 throw new IllegalArgumentException("Comparator argument must not be null");
62 }
63 this.comparator = comparator;
64 }
65
66
67
68
69 public T evaluate(T left, T right) {
70 return (comparator.compare(left, right) >= 0) ? left : right;
71 }
72
73
74
75
76 public boolean equals(Object that) {
77 return that == this || (that instanceof Max<?> && equals((Max<?>) that));
78 }
79
80
81
82
83
84
85 public boolean equals(Max<?> that) {
86 return null != that && comparator.equals(that.comparator);
87 }
88
89
90
91
92 public int hashCode() {
93 return "Max".hashCode() ^ comparator.hashCode();
94 }
95
96
97
98
99 public String toString() {
100 return "Max<" + comparator + ">";
101 }
102
103
104
105
106
107 public static <T extends Comparable<?>> Max<T> instance() {
108 return new Max<T>();
109 }
110
111
112
113
114
115
116 public static <T extends Comparable<?>> UnaryFunction<T, T> instance(T right) {
117 return RightBoundFunction.bind(new Max<T>(), right);
118 }
119
120 }