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 org.apache.commons.functor.BinaryProcedure;
20 import org.apache.commons.functor.Procedure;
21 import org.apache.commons.lang3.Validate;
22
23
24
25
26
27
28
29
30
31
32
33 public final class RightBoundProcedure<A> implements Procedure<A> {
34
35 private final BinaryProcedure<? super A, Object> procedure;
36
37 private final Object param;
38
39
40
41
42
43
44
45 @SuppressWarnings("unchecked")
46 public <R> RightBoundProcedure(BinaryProcedure<? super A, ? super R> procedure, R arg) {
47 this.procedure =
48 (BinaryProcedure<? super A, Object>) Validate.notNull(procedure,
49 "BinaryProcedure argument was null");
50 this.param = arg;
51 }
52
53
54
55
56 public void run(A obj) {
57 procedure.run(obj, param);
58 }
59
60
61
62
63 @Override
64 public boolean equals(Object obj) {
65 if (obj == this) {
66 return true;
67 }
68 if (!(obj instanceof RightBoundProcedure<?>)) {
69 return false;
70 }
71 RightBoundProcedure<?> that = (RightBoundProcedure<?>) obj;
72 return this.procedure.equals(that.procedure)
73 && (null == param ? null == that.param : param.equals(that.param));
74 }
75
76
77
78
79 @Override
80 public int hashCode() {
81 int hash = "RightBoundProcedure".hashCode();
82 hash <<= 2;
83 hash ^= procedure.hashCode();
84 if (null != param) {
85 hash <<= 2;
86 hash ^= param.hashCode();
87 }
88 return hash;
89 }
90
91
92
93
94 @Override
95 public String toString() {
96 return "RightBoundProcedure<" + procedure + "(?," + param + ")>";
97 }
98
99
100
101
102
103
104
105
106
107 public static <L, R> RightBoundProcedure<L> bind(BinaryProcedure<? super L, ? super R> procedure, R arg) {
108 return null == procedure ? null : new RightBoundProcedure<L>(procedure, arg);
109 }
110
111 }