Description
John 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