Support for immutable collection types [DATACMNS-1381]
See original GitHub issueJohn Butler opened DATACMNS-1381 and commented
Currently the MappingMongoConverter makes a static call to CollectionFactory.createCollection and CollectionFactory.createMap. And currently CollectionFactory does not support any of the Guava Immutable collection. Until Java comes up with a List interface that expresses that it is Immutable, I believe there is still good reason to declare class fields as ImmutableList instead of just List. For this reason it would be create to provide a mechanism to support these collections: ImmutableList, ImmutableSet, ImmutableMultiSet, ImmutableMap, ImmutableBiMap.
In order to do this the create method would need to take a set of values to populated in the created collection or a Builder interface would need to be returned.
Use Case: I have a domain object as below…
public class MyDomain{
private List<String> listOfValues;
}
I want the above to be thread-safe and so I design it to be immutable. When I create it I appropriate use ImmutableList. However, in the above there are two issues: 1) the getter returns List which gives the caller no indication that it is immutable 2) when marshalled via Spring Data it is created as a mutable List thereby breaking my thread-safety through immutability. Therefore the below would be better:
public class MyDomain{
private ImmutableList<String> listOfValues;
}
Issue Links:
-
SPR-16797 Allow creation of immutable collections through CollectionFactory
-
DATACMNS-1322 Add support for immutable objects in PersistentPropertyAccessor
1 votes, 4 watchers
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (2 by maintainers)
There’s been progress on this in the last couple of days. I’ve tweaked the custom collection support in #2619 and added the necessary adapters to support Eclipse Collections on repository methods in #2618. Both of that is available in the 2.7 and 3.0 snapshots.
The actual model support, i.e. using Eclipse Collections in domain types that are mapped using the Spring Data mapping infrastructure, is not yet available as I didn’t want to rush that support in so short before the 2.7 GA release. We’re going to give this another round of thought after 2.7 GA. It’s likely to land in 3.0 but also might in a potential 2.8 should we end up deciding to even release that.
#2619 introduced a
CustomCollectionRegistrar
configurable via aspring.factories
file so that support for other custom collections can even be added from the outside. Just put the implementation of that interface into a JAR, add the config file, and it’s picked up by Spring Data at runtime.I’l leave this ticket around to eventually implement the custom collection support for Spring Data mapped domain types.
Thanks for bringing this up again, Donald. Based on the recent work to more broadly support Vavr collection types, I’ve encoded some key team decisions into a prototype that allows plugging custom collection types. That looks good so far, and I was able to add draft support for Eclipse Collections through that mechanism.
As a first step, this allows Eclipse collection types to be used as return and parameter values for query methods. The usage in the domain model itself will require additional steps, which we haven’t decided on yet.