1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.composite;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.functor.Function;
22 import org.apache.commons.functor.Procedure;
23 import org.apache.commons.functor.UnaryProcedure;
24
25
26
27
28
29
30 public class TransformedProcedure implements Procedure, Serializable {
31
32
33
34 private static final long serialVersionUID = -4111958123789033410L;
35
36
37 private static final int HASH_SHIFT = 2;
38
39
40
41
42
43 private static final class Helper<X> implements Procedure, Serializable {
44
45
46
47 private static final long serialVersionUID = -4093503167446891318L;
48 private Function<? extends X> function;
49 private UnaryProcedure<? super X> procedure;
50
51
52
53
54
55
56 private Helper(Function<? extends X> function, UnaryProcedure<? super X> procedure) {
57 this.function = function;
58 this.procedure = procedure;
59 }
60
61
62
63
64 public void run() {
65 procedure.run(function.evaluate());
66 }
67 }
68
69 private final Helper<?> helper;
70
71
72
73
74
75
76
77 public <X> TransformedProcedure(Function<? extends X> function, UnaryProcedure<? super X> procedure) {
78 this.helper = new Helper<X>(function, procedure);
79 }
80
81
82
83
84 public final void run() {
85 helper.run();
86 }
87
88
89
90
91 @Override
92 public final boolean equals(Object obj) {
93 return obj == this || obj instanceof TransformedProcedure
94 && equals((TransformedProcedure) obj);
95 }
96
97
98
99
100
101
102 public final boolean equals(TransformedProcedure that) {
103 return that != null && that.helper.function.equals(this.helper.function)
104 && that.helper.procedure.equals(this.helper.procedure);
105 }
106
107
108
109
110 @Override
111 public int hashCode() {
112 int result = "TransformedProcedure".hashCode();
113 result <<= HASH_SHIFT;
114 result |= helper.procedure.hashCode();
115 result <<= HASH_SHIFT;
116 result |= helper.function.hashCode();
117 return result;
118 }
119
120
121
122
123 @Override
124 public String toString() {
125 return "TransformedProcedure<" + helper.function + "; " + helper.procedure + ">";
126 }
127 }