Skip to content

Commit 5b56895

Browse files
nexx512odrotbohm
nexx512
authored andcommitted
Consider Vavr collections to be collection like in TypeInformation.
Issue #2511.
1 parent 44a472c commit 5b56895

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252
* @author Oliver Gierke
5353
* @author Christoph Strobl
5454
* @author Mark Paluch
55+
* @author Jürgen Diez
5556
*/
5657
class TypeDiscoverer<S> implements TypeInformation<S> {
5758

5859
private static final Class<?>[] MAP_TYPES;
60+
private static final Class<?>[] COLLECTION_TYPES;
5961

6062
static {
6163

@@ -69,6 +71,19 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
6971
} catch (ClassNotFoundException o_O) {}
7072

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

7489
private final Type type;
@@ -369,8 +384,21 @@ public boolean isCollectionLike() {
369384

370385
return rawType.isArray() //
371386
|| Iterable.class.equals(rawType) //
372-
|| Collection.class.isAssignableFrom(rawType) //
373-
|| Streamable.class.isAssignableFrom(rawType);
387+
|| Streamable.class.isAssignableFrom(rawType)
388+
|| isCollection();
389+
}
390+
391+
private boolean isCollection() {
392+
393+
Class<S> type = getType();
394+
395+
for (Class<?> collectionType : COLLECTION_TYPES) {
396+
if (collectionType.isAssignableFrom(type)) {
397+
return true;
398+
}
399+
}
400+
401+
return false;
374402
}
375403

376404
/*

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

+33
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* Unit tests for {@link TypeDiscoverer}.
3939
*
4040
* @author Oliver Gierke
41+
* @author Jürgen Diez
4142
*/
4243
@ExtendWith(MockitoExtension.class)
4344
public class TypeDiscovererUnitTests {
@@ -179,6 +180,38 @@ void detectsSubTypes() {
179180
assertThat(type.isSubTypeOf(String.class)).isFalse();
180181
}
181182

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

184217
Addresses addresses;

0 commit comments

Comments
 (0)