-
Notifications
You must be signed in to change notification settings - Fork 23
/
AnnotationOperationMethodBuilder.java
234 lines (219 loc) · 13.2 KB
/
AnnotationOperationMethodBuilder.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
/*
* Copyright (c) 2023 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.repository;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
enum AnnotationOperationMethodBuilder implements Function<MethodMetadata, List<String>> {
INSERT {
@Override
public List<String> apply(MethodMetadata methodMetadata) {
Parameter parameter = getParameter(methodMetadata, "The insert method must have only one parameter");
var returnType = methodMetadata.getReturnType();
boolean isVoid = AnnotationOperationMethodBuilder.isVoid(returnType);
boolean isInt = isInt(returnType);
boolean isLong = isLong(returnType);
if(parameter.isGeneric()){
if(isVoid) {
return Collections.singletonList( "this.template.insert(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.template.insert(" + parameter.name() + ")",
"int result = (int)java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
} else if (isLong) {
return List.of("this.template.insert(" + parameter.name() + ")",
"long result = java.util.stream.StreamSupport.stream(" + parameter.name() + ".spliterator(), false).count()");
}
return Collections.singletonList( "var result = StreamSupport.stream(this.template.insert(" + parameter.name() + ").spliterator(), false).toList()");
} else if(parameter.isArray()){
if(isVoid) {
return Collections.singletonList("this.template.insert(java.util.Arrays.stream(" + parameter.name() + ").toList())");
} else if(isInt){
return List.of("this.template.insert(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"int result = " + parameter.name() + ".length");
}else if(isLong){
return List.of("this.template.insert(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"long result = (long)" + parameter.name() + ".length");
}
return List.of("var insertResult = this.template.insert(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"var result = java.util.stream.StreamSupport.stream(insertResult.spliterator(), false).toArray("+
parameter.arrayType()+"::new)");
}
if(isVoid) {
return Collections.singletonList( "this.template.insert(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.template.insert(" + parameter.name() + ")", "int result = 1");
} else if(isLong){
return List.of("this.template.insert(" + parameter.name() + ")", "long result = 1L");
}
return Collections.singletonList( "var result = this.template.insert(" + parameter.name() + ")");
}
}, SAVE {
@Override
public List<String> apply(MethodMetadata methodMetadata) {
Parameter parameter = getParameter(methodMetadata, "The save method must have only one parameter");
var returnType = methodMetadata.getReturnType();
boolean isVoid = isVoid(returnType);
boolean isInt = isInt(returnType);
boolean isLong = isLong(returnType);
if(parameter.isGeneric()){
if(isVoid) {
return Collections.singletonList( "this.saveAll(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.saveAll(" + parameter.name() + ")",
"int result = (int)java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
}else if(isLong){
return List.of("this.saveAll(" + parameter.name() + ")",
"long result = java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
}
return Collections.singletonList( "var result = this.saveAll(" + parameter.name() + ")");
} else if(parameter.isArray()){
if(isVoid) {
return Collections.singletonList("this.saveAll(java.util.Arrays.stream(" + parameter.name() + ").toList())");
} else if(isInt){
return List.of("this.saveAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"int result = " + parameter.name() + ".length");
} else if(isLong){
return List.of("this.saveAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"long result = (long)" + parameter.name() + ".length");
}
return List.of("var saveResult = this.saveAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"var result = java.util.stream.StreamSupport.stream(saveResult.spliterator(), false).toArray("+
parameter.arrayType()+"::new)");
}
if(isVoid) {
return Collections.singletonList( "this.template.insert(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.template.insert(" + parameter.name() + ")", "int result = 1");
} else if(isLong){
return List.of("this.template.insert(" + parameter.name() + ")", "long result = 1");
}
return Collections.singletonList( "var result = this.template.insert(" + parameter.name() + ")");
}
},
UPDATE {
@Override
public List<String> apply(MethodMetadata methodMetadata) {
Parameter parameter = getParameter(methodMetadata, "The insert method must have only one parameter");
var returnType = methodMetadata.getReturnType();
boolean isVoid = isVoid(returnType);
boolean isInt = AnnotationOperationMethodBuilder.isInt(returnType);
boolean isLong = isLong(returnType);
if(parameter.isGeneric()){
if(isVoid) {
return Collections.singletonList( "this.template.update(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.template.update(" + parameter.name() + ")",
"int result = (int)java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
} else if(isLong){
return List.of("this.template.update(" + parameter.name() + ")",
"long result = java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
}
return Collections.singletonList( "var result = StreamSupport.stream(this.template.update(" + parameter.name() + ").spliterator(), false).toList()");
} else if(parameter.isArray()){
if(isVoid) {
return Collections.singletonList("this.template.update(java.util.Arrays.stream(" + parameter.name() + ").toList())");
} else if(isInt){
return List.of("this.template.update(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"int result = " + parameter.name() + ".length");
} else if(isLong){
return List.of("this.template.update(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"long result = (long)" + parameter.name() + ".length");
}
return List.of("var insertResult = this.template.update(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"var result = java.util.stream.StreamSupport.stream(insertResult.spliterator(), false).toArray("+
parameter.arrayType()+"::new)");
}
if(isVoid) {
return Collections.singletonList( "this.template.update(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.template.update(" + parameter.name() + ")", "int result = 1");
}else if(isLong){
return List.of("this.template.update(" + parameter.name() + ")", "long result = 1L");
}
return Collections.singletonList( "var result = this.template.update(" + parameter.name() + ")");
}
},
DELETE {
@Override
public List<String> apply(MethodMetadata methodMetadata) {
Parameter parameter = getParameter(methodMetadata, "The insert method must have only one parameter");
var returnType = methodMetadata.getReturnType();
boolean isVoid = isVoid(returnType);
boolean isInt = isInt(returnType);
boolean isLong = isLong(returnType);
boolean isBoolean = isBoolean(returnType);
if(parameter.isGeneric()){
if(isVoid) {
return Collections.singletonList( "this.deleteAll(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.deleteAll(" + parameter.name() + ")",
"int result = (int)java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
} else if(isLong){
return List.of("this.deleteAll(" + parameter.name() + ")",
"long result = java.util.stream.StreamSupport.stream("+ parameter.name()+ ".spliterator(), false).count()");
} else if (isBoolean) {
return List.of("this.deleteAll(" + parameter.name() + ")",
"boolean result = true");
}
return List.of( "this.deleteAll(" + parameter.name() + ")", "var result = "+parameter.name());
} else if(parameter.isArray()){
if(isVoid) {
return Collections.singletonList("this.deleteAll(java.util.Arrays.stream(" + parameter.name() + ").toList())");
} else if(isInt){
return List.of("this.deleteAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"int result = " + parameter.name() + ".length");
} else if(isLong){
return List.of("this.deleteAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"long result = (long)" + parameter.name() + ".length");
} else if (isBoolean) {
return List.of("this.deleteAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"boolean result = true");
}
return List.of("var insertResult = this.deleteAll(java.util.Arrays.stream(" + parameter.name() + ").toList())",
"var result = java.util.stream.StreamSupport.stream(insertResult.spliterator(), false).toArray("+
parameter.arrayType()+"::new)");
}
if(isVoid) {
return Collections.singletonList( "this.delete(" + parameter.name() + ")");
} else if(isInt){
return List.of("this.delete(" + parameter.name() + ")", "int result = 1");
} else if(isLong){
return List.of("this.delete(" + parameter.name() + ")", "long result = 1L");
} else if(isBoolean){
return List.of("this.delete(" + parameter.name() + ")", "boolean result = true");
}
return List.of("this.delete(" + parameter.name() + ")", "var result =" + parameter.name());
}
};
private static Parameter getParameter(MethodMetadata methodMetadata, String s) {
List<Parameter> parameters = methodMetadata.getParameters();
if (parameters.size() != 1) {
throw new IllegalStateException(s);
}
return parameters.get(0);
}
private static boolean isInt(String returnType) {
return returnType.equals(Integer.TYPE.getName())|| returnType.equals(Integer.class.getName());
}
private static boolean isLong(String returnType) {
return returnType.equals(Long.TYPE.getName())|| returnType.equals(Long.class.getName());
}
private static boolean isBoolean(String returnType) {
return returnType.equals(Boolean.TYPE.getName())|| returnType.equals(Boolean.class.getName());
}
private static boolean isVoid(String returnType) {
return returnType.equals(Void.TYPE.getName());
}
}