Skip to content

Commit 30033b2

Browse files
committed
DATACMNS-1252 - Improved Vavr collection handling to convert between collection types.
If a query method now uses e.g. a Vavr Set as return type, we now also use a potential source List as input for the LinkedHashSet, even if that changes the characteristics (duplicate policy etc.) of the result. This is consistent with our general handling of collections as we're using a Spring ConversionService for collection mapping anyway. In general the new conversion algorithm is driven by the expected target type first: - i.v.c.Seq -> i.v.c.List - i.v.c.Set -> i.v.c.LinkedHashSet - i.v.c.Map -> i.v.c.LinkedHashMap If none of the declared types is assignable we fall back to the previous algorithm choosing an implementation as close as possible to the original source value: - j.u.List -> i.v.c.List - j.u.Set -> i.v.c.LinkedHashSet - j.u.Map -> i.v.c.LinkedHashMap Removed some obsolete full qualifications of types.
1 parent 66224b3 commit 30033b2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/main/java/org/springframework/data/repository/util/VavrCollections.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,35 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
116116
*/
117117
@Nullable
118118
@Override
119-
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
119+
public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) {
120+
121+
Class<?> targetType = targetDescriptor.getType();
122+
123+
if (io.vavr.collection.Seq.class.isAssignableFrom(targetType)) {
124+
return io.vavr.collection.List.ofAll((Iterable<?>) source);
125+
}
126+
127+
if (io.vavr.collection.Set.class.isAssignableFrom(targetType)) {
128+
return LinkedHashSet.ofAll((Iterable<?>) source);
129+
}
130+
131+
if (io.vavr.collection.Map.class.isAssignableFrom(targetType)) {
132+
return LinkedHashMap.ofAll((Map<?, ?>) source);
133+
}
134+
135+
// No dedicated type asked for, probably Traversable.
136+
// Try to stay as close to the source value.
120137

121138
if (source instanceof List) {
122139
return io.vavr.collection.List.ofAll((Iterable<?>) source);
123140
}
124141

125-
if (source instanceof java.util.Set) {
142+
if (source instanceof Set) {
126143
return LinkedHashSet.ofAll((Iterable<?>) source);
127144
}
128145

129-
if (source instanceof java.util.Map) {
130-
return LinkedHashMap.ofAll((java.util.Map<?, ?>) source);
146+
if (source instanceof Map) {
147+
return LinkedHashMap.ofAll((Map<?, ?>) source);
131148
}
132149

133150
return source;

src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,13 @@ public void vavrSeqIsASupportedPageableType() {
338338
Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
339339
assertThat(allowedPageableTypes).contains(io.vavr.collection.Seq.class);
340340
}
341+
342+
@Test // DATAJPA-1258
343+
public void convertsJavaListsToVavrSet() {
344+
345+
List<String> source = Collections.singletonList("foo");
346+
347+
assertThat(conversionService.convert(source, io.vavr.collection.Set.class)) //
348+
.isInstanceOf(io.vavr.collection.Set.class);
349+
}
341350
}

0 commit comments

Comments
 (0)