Skip to content

Commit 23c153a

Browse files
nexx512odrotbohm
authored andcommitted
Consider Vavr collections to be collection like in TypeInformation.
Issue #2511.
1 parent 68bfa9a commit 23c153a

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/main/java/org/springframework/data/util/TypeDiscoverer.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@
5050
* @author Oliver Gierke
5151
* @author Christoph Strobl
5252
* @author Mark Paluch
53+
* @author Jürgen Diez
5354
*/
5455
class TypeDiscoverer<S> implements TypeInformation<S> {
5556

5657
private static final Class<?>[] MAP_TYPES;
58+
private static final Class<?>[] COLLECTION_TYPES;
5759

5860
static {
5961

@@ -67,6 +69,19 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
6769
} catch (ClassNotFoundException o_O) {}
6870

6971
MAP_TYPES = mapTypes.toArray(new Class[0]);
72+
73+
Set<Class<?>> collectionTypes = new HashSet<>();
74+
collectionTypes.add(Collection.class);
75+
76+
try {
77+
collectionTypes.add(ClassUtils.forName("io.vavr.collection.Seq", classLoader));
78+
} catch (ClassNotFoundException o_O) {}
79+
80+
try {
81+
collectionTypes.add(ClassUtils.forName("io.vavr.collection.Set", classLoader));
82+
} catch (ClassNotFoundException o_O) {}
83+
84+
COLLECTION_TYPES = collectionTypes.toArray(new Class[0]);
7085
}
7186

7287
private final Type type;
@@ -332,8 +347,21 @@ public boolean isCollectionLike() {
332347

333348
return rawType.isArray() //
334349
|| Iterable.class.equals(rawType) //
335-
|| Collection.class.isAssignableFrom(rawType) //
336-
|| Streamable.class.isAssignableFrom(rawType);
350+
|| Streamable.class.isAssignableFrom(rawType)
351+
|| isCollection();
352+
}
353+
354+
private boolean isCollection() {
355+
356+
Class<S> type = getType();
357+
358+
for (Class<?> collectionType : COLLECTION_TYPES) {
359+
if (collectionType.isAssignableFrom(type)) {
360+
return true;
361+
}
362+
}
363+
364+
return false;
337365
}
338366

339367
@Nullable

src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* Unit tests for {@link TypeDiscoverer}.
3838
*
3939
* @author Oliver Gierke
40+
* @author Jürgen Diez
4041
*/
4142
@ExtendWith(MockitoExtension.class)
4243
public class TypeDiscovererUnitTests {
@@ -178,6 +179,38 @@ void detectsSubTypes() {
178179
assertThat(type.isSubTypeOf(String.class)).isFalse();
179180
}
180181

182+
@Test
183+
void considerVavrMapToBeAMap() {
184+
185+
TypeInformation<io.vavr.collection.Map> type = from(io.vavr.collection.Map.class);
186+
187+
assertThat(type.isMap()).isTrue();
188+
}
189+
190+
@Test
191+
void considerVavrSetToBeCollectionLike() {
192+
193+
TypeInformation<io.vavr.collection.Set> type = from(io.vavr.collection.Set.class);
194+
195+
assertThat(type.isCollectionLike()).isTrue();
196+
}
197+
198+
@Test
199+
void considerVavrSeqToBeCollectionLike() {
200+
201+
TypeInformation<io.vavr.collection.Seq> type = from(io.vavr.collection.Seq.class);
202+
203+
assertThat(type.isCollectionLike()).isTrue();
204+
}
205+
206+
@Test
207+
void considerVavrListToBeCollectionLike() {
208+
209+
TypeInformation<io.vavr.collection.List> type = from(io.vavr.collection.List.class);
210+
211+
assertThat(type.isCollectionLike()).isTrue();
212+
}
213+
181214
class Person {
182215

183216
Addresses addresses;

0 commit comments

Comments
 (0)