1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.adapter;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.functor.BinaryFunction;
22 import org.apache.commons.functor.Function;
23 import org.apache.commons.lang3.Validate;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public final class FullyBoundFunction<T> implements Function<T>, Serializable {
43
44
45
46 private static final long serialVersionUID = -8588331452137525985L;
47
48 private final BinaryFunction<Object, Object, ? extends T> function;
49
50 private final Object left;
51
52 private final Object right;
53
54
55
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63 public <L, R> FullyBoundFunction(BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
64 this.function =
65 (BinaryFunction<Object, Object, T>) Validate.notNull(function,
66 "BinaryFunction argument was null");
67 this.left = left;
68 this.right = right;
69 }
70
71
72
73
74 public T evaluate() {
75 return function.evaluate(left, right);
76 }
77
78
79
80
81 @Override
82 public boolean equals(Object that) {
83 return that == this || (that instanceof FullyBoundFunction<?> && equals((FullyBoundFunction<?>) that));
84 }
85
86
87
88
89
90
91 public boolean equals(FullyBoundFunction<?> that) {
92 return null != that && function.equals(that.function)
93 && (null == left ? null == that.left : left.equals(that.left))
94 && (null == right ? null == that.right : right.equals(that.right));
95 }
96
97
98
99
100 @Override
101 public int hashCode() {
102 int hash = "FullyBoundFunction".hashCode();
103 hash <<= 2;
104 hash ^= function.hashCode();
105 hash <<= 2;
106 if (null != left) {
107 hash ^= left.hashCode();
108 }
109 hash <<= 2;
110 if (null != right) {
111 hash ^= right.hashCode();
112 }
113 return hash;
114 }
115
116
117
118
119 @Override
120 public String toString() {
121 return "FullyBoundFunction<" + function + "(" + left + ", " + right + ")>";
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public static <L, R, T> FullyBoundFunction<T> bind(
135 BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
136 return null == function ? null : new FullyBoundFunction<T>(function, left, right);
137 }
138
139 }