-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathQueryable.java
More file actions
38 lines (32 loc) · 1.32 KB
/
Queryable.java
File metadata and controls
38 lines (32 loc) · 1.32 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
package solutions.bellatrix.data.http.contracts;
import com.google.gson.annotations.SerializedName;
import solutions.bellatrix.data.http.infrastructure.QueryParameter;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Objects;
public interface Queryable {
default LinkedList<QueryParameter> toQueryParams() {
try {
var queryParameters = new LinkedList<QueryParameter>();
Class<?> clazz = this.getClass();
while (clazz!=null) {
Field[] fields = clazz.getDeclaredFields();
Arrays.stream(fields).forEach(x -> x.setAccessible(true));
for (Field field : fields) {
if (field.isAnnotationPresent(SerializedName.class)) {
var queryParameterName = field.getAnnotation(SerializedName.class).value();
var value = field.get(this);
if (Objects.nonNull(value)) {
queryParameters.add(new QueryParameter(queryParameterName, value));
}
}
}
clazz = clazz.getSuperclass();
}
return queryParameters;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}