Skip to content

Commit 160b798

Browse files
committed
GeoResult mapping refactoring
1 parent 0df0964 commit 160b798

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/main/java/com/arangodb/springframework/core/convert/ArangoEntityReader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
public interface ArangoEntityReader {
3232

33+
// FIXME: remove
3334
default <R> R read(Class<R> type, JsonNode source) {
3435
return read(type, source, TransactionMappingContext.EMPTY);
3536
}

src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@
2020

2121
package com.arangodb.springframework.repository.query;
2222

23-
import java.lang.reflect.Method;
2423
import java.util.Collection;
25-
import java.util.HashMap;
2624
import java.util.LinkedList;
2725
import java.util.List;
28-
import java.util.Map;
2926
import java.util.Optional;
3027
import java.util.Set;
3128
import java.util.Spliterators;
3229
import java.util.stream.Collectors;
3330
import java.util.stream.StreamSupport;
3431

35-
import com.arangodb.springframework.core.mapping.TransactionMappingContext;
3632
import com.fasterxml.jackson.databind.JsonNode;
3733
import org.springframework.data.domain.Page;
3834
import org.springframework.data.domain.PageImpl;
@@ -66,29 +62,6 @@ public class ArangoResultConverter {
6662
private final ArangoOperations operations;
6763
private final Class<?> domainClass;
6864

69-
private static Map<Object, Method> TYPE_MAP = new HashMap<>();
70-
71-
/**
72-
* Build static map of all supported return types and the method used to convert them
73-
*/
74-
static {
75-
try {
76-
TYPE_MAP.put(List.class, ArangoResultConverter.class.getMethod("convertList"));
77-
TYPE_MAP.put(Iterable.class, ArangoResultConverter.class.getMethod("convertList"));
78-
TYPE_MAP.put(Collection.class, ArangoResultConverter.class.getMethod("convertList"));
79-
TYPE_MAP.put(Page.class, ArangoResultConverter.class.getMethod("convertPage"));
80-
TYPE_MAP.put(Slice.class, ArangoResultConverter.class.getMethod("convertPage"));
81-
TYPE_MAP.put(Set.class, ArangoResultConverter.class.getMethod("convertSet"));
82-
TYPE_MAP.put(ArangoCursor.class, ArangoResultConverter.class.getMethod("convertArangoCursor"));
83-
TYPE_MAP.put(GeoResult.class, ArangoResultConverter.class.getMethod("convertGeoResult"));
84-
TYPE_MAP.put(GeoResults.class, ArangoResultConverter.class.getMethod("convertGeoResults"));
85-
TYPE_MAP.put(GeoPage.class, ArangoResultConverter.class.getMethod("convertGeoPage"));
86-
TYPE_MAP.put(Optional.class, ArangoResultConverter.class.getMethod("convertOptional"));
87-
TYPE_MAP.put("array", ArangoResultConverter.class.getMethod("convertArray"));
88-
} catch (final NoSuchMethodException e) {
89-
e.printStackTrace();
90-
}
91-
}
9265

9366
/**
9467
* @param accessor
@@ -112,18 +85,42 @@ public ArangoResultConverter(final ArangoParameterAccessor accessor, final Arang
11285
*/
11386
public Object convertResult(final Class<?> type) {
11487
try {
115-
if (type.isArray()) {
116-
return TYPE_MAP.get("array").invoke(this);
117-
}
118-
if (!TYPE_MAP.containsKey(type)) {
119-
return getNext(result);
120-
}
121-
return TYPE_MAP.get(type).invoke(this);
88+
return convert(type);
12289
} catch (final Exception e) {
12390
throw new MappingException(String.format("Can't convert result to type %s!", type.getName()), e);
12491
}
12592
}
12693

94+
private Object convert(Class<?> type) {
95+
if (type.isArray()) {
96+
return convertArray();
97+
} else if (List.class.equals(type)) {
98+
return convertList();
99+
} else if (Iterable.class.equals(type)) {
100+
return convertList();
101+
} else if (Collection.class.equals(type)) {
102+
return convertList();
103+
} else if (Page.class.equals(type)) {
104+
return convertPage();
105+
} else if (Slice.class.equals(type)) {
106+
return convertPage();
107+
} else if (Set.class.equals(type)) {
108+
return convertSet();
109+
} else if (ArangoCursor.class.equals(type)) {
110+
return convertArangoCursor();
111+
} else if (GeoResult.class.equals(type)) {
112+
return convertGeoResult();
113+
} else if (GeoResults.class.equals(type)) {
114+
return convertGeoResults();
115+
} else if (GeoPage.class.equals(type)) {
116+
return convertGeoPage();
117+
} else if (Optional.class.equals(type)) {
118+
return convertOptional();
119+
} else {
120+
return getNext(result);
121+
}
122+
}
123+
127124
/**
128125
* Creates a Set return type from the given cursor
129126
*
@@ -142,20 +139,23 @@ private Set<?> buildSet(final ArangoCursor<?> cursor) {
142139
*/
143140
private GeoResult<?> buildGeoResult(final ArangoCursor<?> cursor) {
144141
GeoResult<?> geoResult = null;
142+
// FIXME
145143
while (cursor.hasNext() && geoResult == null) {
146144
final Object object = cursor.next();
147145
if (!(object instanceof JsonNode)) {
146+
// FIXME
148147
continue;
149148
}
150149

151150
final JsonNode slice = (JsonNode) object;
152151
final JsonNode distSlice = slice.get("_distance");
153152
final Double distanceInMeters = distSlice.isDouble() ? distSlice.doubleValue() : null;
154153
if (distanceInMeters == null) {
154+
// FIXME
155155
continue;
156156
}
157157

158-
final Object entity = operations.getConverter().read(domainClass, slice, TransactionMappingContext.EMPTY);
158+
final Object entity = operations.getConverter().read(domainClass, slice);
159159
final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
160160
geoResult = new GeoResult<>(entity, distance);
161161
}
@@ -170,17 +170,19 @@ private GeoResult<?> buildGeoResult(final ArangoCursor<?> cursor) {
170170
*/
171171
private GeoResult<?> buildGeoResult(final Object object) {
172172
if (object == null || !(object instanceof JsonNode)) {
173+
// FIXME
173174
return null;
174175
}
175176

176177
final JsonNode slice = (JsonNode) object;
177178
final JsonNode distSlice = slice.get("_distance");
178179
final Double distanceInMeters = distSlice.isDouble() ? distSlice.doubleValue() : null;
179180
if (distanceInMeters == null) {
181+
// FIXME
180182
return null;
181183
}
182184

183-
final Object entity = operations.getConverter().read(domainClass, slice, TransactionMappingContext.EMPTY);
185+
final Object entity = operations.getConverter().read(domainClass, slice);
184186
final Distance distance = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS);
185187
return new GeoResult<>(entity, distance);
186188
}

0 commit comments

Comments
 (0)