question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for immutable collection types [DATACMNS-1381]

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
odrotbohmcommented, May 4, 2022

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 a spring.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.

1reaction
odrotbohmcommented, May 2, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Creating Immutable Lists, Sets, and Maps - Oracle Help Center
Convenience static factory methods on the List, Set, and Map interfaces, which were added in JDK 9, let you easily create immutable lists,...
Read more >
.NET Framework - Immutable Collections | Microsoft Learn
This article provides a detailed discussion of the design, implementation and performance of immutable collections to enable you to effectively use them and ......
Read more >
Immutable Collections in Java with Sealed Types | foojay
Designing Immutable Collection using Sealed Types in JDK 15. How to define contractual, structural, and verifiable immutable Java collections.
Read more >
Java Immutable Collections - Stack Overflow
Unmodifiable collections are usually read-only views (wrappers) of other collections. You can't add, remove or clear them, ...
Read more >
Immutable object - Wikipedia
In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found