forked from rethinkdb/rethinkdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunc.java
More file actions
49 lines (45 loc) · 1.63 KB
/
Func.java
File metadata and controls
49 lines (45 loc) · 1.63 KB
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
<%inherit file="../AstSubclass.java" />
<%block name="add_imports">
import com.rethinkdb.model.ReqlLambda;
import com.rethinkdb.utils.Internals;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Arrays;
import java.util.List;
</%block>
<%block name="member_vars">
private static AtomicInteger varId = new AtomicInteger();
</%block>
<%block name="special_methods">\
private static int nextVarId(){
return varId.incrementAndGet();
}
</%block>
<%block name="constructors">\
protected Func(Arguments args){
super(TermType.FUNC, args, null);
}
</%block>
<%block name="static_factories">\
public static Func fromLambda(ReqlLambda function) {
if (function instanceof ReqlFunction0) {
return new Func(Arguments.make(new MakeArray(Arrays.asList()), Internals.toReqlAst(((ReqlFunction0) function).apply())));
}
% for i in range(1, max_arity+1):
else if (function instanceof ReqlFunction${i}) {
ReqlFunction${i} func${i} = (ReqlFunction${i}) function;
% for j in range(1, i+1):
int var${j} = nextVarId();
% endfor
List<Integer> varIds = Arrays.asList(
${", ".join("var%s"%(j,) for j in range(1, j+1))});
Object appliedFunction = func${i}.apply(
${", ".join("new Var(var%s)"%(j,) for j in range(1, j+1))}
);
return new Func(Arguments.make(new MakeArray(varIds), Internals.toReqlAst(appliedFunction)));
}
% endfor
else {
throw new ReqlDriverError("Arity of ReqlLambda not recognized!");
}
}
</%block>