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