This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
LambdaGroovy.java
executable file
·246 lines (207 loc) · 6.58 KB
/
LambdaGroovy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.enumerable.lambda.support.groovy;
import org.enumerable.lambda.Fn0;
import org.enumerable.lambda.Fn1;
import org.enumerable.lambda.Fn2;
import org.enumerable.lambda.Fn3;
import org.enumerable.lambda.annotation.NewLambda;
import org.enumerable.lambda.exception.LambdaWeavingNotEnabledException;
import groovy.lang.Closure;
import sun.org.mozilla.javascript.internal.Function;
/**
* This is class is similar {@link org.enumerable.lambda.Lambda}, but instead of creating
* lambdas inheriting from {@link org.enumerable.lambda.Fn0} it creates lambdas extending
* {@link Function} to be used together with Groovy.
*/
@SuppressWarnings("serial")
public class LambdaGroovy {
public static abstract class ClosureFn0 extends Closure<Object> {
public ClosureFn0() {
super(LambdaGroovy.class);
Fn0.getAndCheckArityForMethod(getImplementingClass(), getMethod());
}
Class<?> getImplementingClass() {
return getClass();
}
String getMethod() {
return "doCall";
}
public abstract Object doCall();
}
public static abstract class ClosureFn1 extends ClosureFn0 {
public Object doCall() {
return doCall(default$1());
}
protected Object default$1() {
return null;
}
public abstract Object doCall(Object a1);
}
public static abstract class ClosureFn2 extends ClosureFn1 {
public Object doCall(Object a1) {
return doCall(a1, default$2());
}
protected Object default$2() {
return null;
}
public abstract Object doCall(Object a1, Object a2);
}
public static abstract class ClosureFn3 extends ClosureFn2 {
public Object doCall(Object a1, Object a2) {
return doCall(a1, a2, default$3());
}
protected Object default$3() {
return null;
}
public abstract Object doCall(Object a1, Object a2, Object a3);
}
/**
* Creates a new function implementing {@link Function} taking no arguments.
*/
@NewLambda
public static ClosureFn0 closure(Object block) {
throw new LambdaWeavingNotEnabledException();
}
/**
* Creates a new function implementing {@link Function} taking one argument.
*/
@NewLambda
public static ClosureFn1 closure(Object a1, Object block) {
throw new LambdaWeavingNotEnabledException();
}
/**
* Creates a new function implementing {@link Function} taking two
* arguments.
*/
@NewLambda
public static ClosureFn2 closure(Object a1, Object a2, Object block) {
throw new LambdaWeavingNotEnabledException();
}
/**
* Creates a new function implementing {@link Function} taking three
* arguments.
*/
@NewLambda
public static ClosureFn3 closure(Object a1, Object a2, Object a3, Object block) {
throw new LambdaWeavingNotEnabledException();
}
/**
* Wraps the {@link Function} in a {@link Fn0}.
*/
public static Fn0<Object> toFn0(final Closure<?> closure) {
return new Fn0<Object>() {
public Object call() {
return closure.call();
}
};
}
/**
* Wraps the {@link Function} in a {@link Fn1}.
*/
public static Fn1<Object, Object> toFn1(final Closure<?> closure) {
return new Fn1<Object, Object>() {
public Object call(Object a1) {
return closure.call(new Object[] { a1 });
}
};
}
/**
* Wraps the {@link Function} in a {@link Fn2}.
*/
public static Fn2<Object, Object, Object> toFn2(final Closure<?> closure) {
return new Fn2<Object, Object, Object>() {
public Object call(Object a1, Object a2) {
return closure.call(new Object[] { a1, a2 });
}
};
}
/**
* Wraps the {@link Function} in a {@link Fn3}.
*/
public static Fn3<Object, Object, Object, Object> toFn3(final Closure<?> closure) {
return new Fn3<Object, Object, Object, Object>() {
public Object call(Object a1, Object a2, Object a3) {
return closure.call(new Object[] { a1, a2, a3 });
}
};
}
/**
* Wraps the {@link Fn0} in a {@link Closure}.
*/
@SuppressWarnings("rawtypes")
public static ClosureFn0 toClosure(final Fn0 fn) {
return new ClosureFn0() {
public Object doCall() {
return fn.call();
}
};
}
/**
* Wraps the {@link Fn1} in a {@link Closure}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ClosureFn1 toClosure(final Fn1 fn) {
return new ClosureFn1() {
public Object doCall() {
return fn.call();
}
public Object doCall(Object a1) {
return fn.call(a1);
}
Class<?> getImplementingClass() {
return fn.getClass();
}
String getMethod() {
return "call";
}
};
}
/**
* Wraps the {@link Fn2} in a {@link Closure}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ClosureFn2 toClosure(final Fn2 fn) {
return new ClosureFn2() {
public Object doCall() {
return fn.call();
}
public Object doCall(Object a1) {
return fn.call(a1);
}
public Object doCall(Object a1, Object a2) {
return fn.call(a1, a2);
}
Class<?> getImplementingClass() {
return fn.getClass();
}
String getMethod() {
return "call";
}
};
}
/**
* Wraps the {@link Fn3} in a {@link Closure}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ClosureFn3 toClosure(final Fn3 fn) {
return new ClosureFn3() {
public Object doCall() {
return fn.call();
}
public Object doCall(Object a1) {
return fn.call(a1);
}
public Object doCall(Object a1, Object a2) {
return fn.call(a1, a2);
}
public Object doCall(Object a1, Object a2, Object a3) {
return fn.call(a1, a2, a3);
}
Class<?> getImplementingClass() {
return fn.getClass();
}
String getMethod() {
return "call";
}
};
}
}