spring-projects / spring-projects/spring-data-commons

Support for immutable collection types [DATACMNS-1381]

Open
#1,817 17 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

in: mapping type: enhancement
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.