Skip to content

Consider vavr collections to be collection like #2511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/java/org/springframework/data/util/TypeDiscoverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @author Jürgen Diez
*/
class TypeDiscoverer<S> implements TypeInformation<S> {

private static final Class<?>[] MAP_TYPES;
private static final Class<?>[] COLLECTION_TYPES;

static {

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

MAP_TYPES = mapTypes.toArray(new Class[0]);

Set<Class<?>> collectionTypes = new HashSet<>();
collectionTypes.add(Collection.class);

try {
collectionTypes.add(ClassUtils.forName("io.vavr.collection.Seq", classLoader));
} catch (ClassNotFoundException o_O) {}

try {
collectionTypes.add(ClassUtils.forName("io.vavr.collection.Set", classLoader));
} catch (ClassNotFoundException o_O) {}

COLLECTION_TYPES = collectionTypes.toArray(new Class[0]);
}

private final Type type;
Expand Down Expand Up @@ -369,8 +384,21 @@ public boolean isCollectionLike() {

return rawType.isArray() //
|| Iterable.class.equals(rawType) //
|| Collection.class.isAssignableFrom(rawType) //
|| Streamable.class.isAssignableFrom(rawType);
|| Streamable.class.isAssignableFrom(rawType)
|| isCollection();
}

private boolean isCollection() {

Class<S> type = getType();

for (Class<?> collectionType : COLLECTION_TYPES) {
if (collectionType.isAssignableFrom(type)) {
return true;
}
}

return false;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* Unit tests for {@link TypeDiscoverer}.
*
* @author Oliver Gierke
* @author Jürgen Diez
*/
@ExtendWith(MockitoExtension.class)
public class TypeDiscovererUnitTests {
Expand Down Expand Up @@ -179,6 +180,38 @@ void detectsSubTypes() {
assertThat(type.isSubTypeOf(String.class)).isFalse();
}

@Test
void considerVavrMapToBeAMap() {

TypeInformation<io.vavr.collection.Map> type = from(io.vavr.collection.Map.class);

assertThat(type.isMap()).isTrue();
}

@Test
void considerVavrSetToBeCollectionLike() {

TypeInformation<io.vavr.collection.Set> type = from(io.vavr.collection.Set.class);

assertThat(type.isCollectionLike()).isTrue();
}

@Test
void considerVavrSeqToBeCollectionLike() {

TypeInformation<io.vavr.collection.Seq> type = from(io.vavr.collection.Seq.class);

assertThat(type.isCollectionLike()).isTrue();
}

@Test
void considerVavrListToBeCollectionLike() {

TypeInformation<io.vavr.collection.List> type = from(io.vavr.collection.List.class);

assertThat(type.isCollectionLike()).isTrue();
}

class Person {

Addresses addresses;
Expand Down