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.UnaryFunction;
23
24
25
26
27
28
29 public class TransformedFunction<T> implements Function<T>, Serializable {
30
31
32
33 private static final long serialVersionUID = 1201423110871342081L;
34
35
36
37
38
39 private static final class Helper<X, T> implements Function<T>, Serializable {
40
41
42
43 private static final long serialVersionUID = -7177784125292465809L;
44 private Function<? extends X> preceding;
45 private UnaryFunction<? super X, ? extends T> following;
46
47
48
49
50
51
52 private Helper(Function<? extends X> preceding, UnaryFunction<? super X, ? extends T> following) {
53 this.preceding = preceding;
54 this.following = following;
55 }
56
57
58
59
60 public T evaluate() {
61 return following.evaluate(preceding.evaluate());
62 }
63 }
64
65 private final Helper<?, T> helper;
66
67
68
69
70
71
72
73 public <X> TransformedFunction(Function<? extends X> preceding,
74 UnaryFunction<? super X, ? extends T> following) {
75 this.helper = new Helper<X, T>(preceding, following);
76 }
77
78
79
80
81 public final T evaluate() {
82 return helper.evaluate();
83 }
84
85
86
87
88 @Override
89 public final boolean equals(Object obj) {
90 return obj == this || obj instanceof TransformedFunction<?> && equals((TransformedFunction<?>) obj);
91 }
92
93
94
95
96
97
98 public final boolean equals(TransformedFunction<?> that) {
99 return that != null && that.helper.preceding.equals(this.helper.preceding)
100 && that.helper.following.equals(this.helper.following);
101 }
102
103
104
105
106 @Override
107 public int hashCode() {
108 int result = "TransformedFunction".hashCode();
109 result <<= 2;
110 result |= helper.following.hashCode();
111 result <<= 2;
112 result |= helper.preceding.hashCode();
113 return result;
114 }
115
116
117
118
119 @Override
120 public String toString() {
121 return "TransformedFunction<" + helper.preceding + "; " + helper.following + ">";
122 }
123 }