FEBERAPA DA SEMANA

Caros leitores sejam homens ou mulheres, por uma incrível escolha de meus pais nasci no Brasil. Simplesmente não fui consultado, mas imagino que com o avanço na tecnologia em breve será possível…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Immutable Collections in Java using Sealed Types

How to define contractual, structural, and verifiable immutable Java collections.

Designing Immutable Collection using Sealed Types in JDK 15

A class or interface is verifiably immutable if all of the implementations are contractually and structurally immutable, and are restricted to a specific set of classes that can be verified. This is a capability that can now be more easily achieved via Sealed Types in JDK 15. With Sealed Types a developer can restrict the implementations of interfaces and classes to a specified set of types.

Immutable collection implementations for Java are available in the Java Collections Framework (since JDK 9), Google Guava and Eclipse Collections. None of the immutable collection alternatives provide the combination of structural, contractual and verifiable immutability today.

There are structurally immutable collections available in the Java Collection Framework via List.of(), Set.of(), Map.of(). The JDK immutable collections are not contractually immutable, because they implement the mutable List, Set, Map interfaces.

Guava has collection types that are structurally immutable, but not contractually immutable. The immutable collections in Guava implement the mutable JDK interfaces — List, Set, Map. Guava restricts the implementations of the immutable collection types by using abstract classes with package private constructors, which require all implementations to be in the same package. This restriction is a novel design approach and a key component of verifiable immutability, but is still lacking contractual immutability.

The Sealed Types preview in JDK 15 gives developers the capability to finally provide the trifecta of contractual, structural and verifiable immutability in a collections framework. Using the Sealed Types preview feature, we can restrict the implementations of an ImmutableCollection interface using the sealed and permits modifiers.

Similarly, we can restrict the implementations of ImmutableList.

The ImmutableEmptyList implementation of ImmutableList is then declared as final.

Experimenting with Sealed Types in JDK 15 has been interesting and encouraging. I wish this feature was available a decade ago when we first defined the ImmutableCollection hierarchy in Eclipse Collections. I’ve been able to extend the design ideas that we implemented in Eclipse Collections years ago with a feature that provides a more restrictive modeling capability.

A Custom Collections framework in the Deck of Cards Kata

In addition, default methods and static interface methods are used extensively to build the rich interfaces in the framework.

The intent was to use the latest features available in the most current releases of Java where they were proved useful. The latest evolution shows what is possible by leveraging Sealed Types to implement immutable collection types. I’m quite encouraged by the results of the feature so far. I hope that this use case can be used and discussed as an example of the practical applicability of the Sealed Types feature.

The following blogs explain the evolution of the custom collections framework design over the past six months.

I hope you found this blog useful. Check out the source code and give the Deck of Cards kata and other code katas included in the repo a try.

Enjoy!

Add a comment

Related posts:

CODE READABILITY

Programmers complain about readability and talk about bad code and unclean code, and the difficulties they run into trying to understand and maintain that code. What do we mean by code readability…

Everything you need to know about the October Apple Event

Apple just had its virtual keynote and alongside that came some new tech. Four new iPhones with new features and a baby brother to the original HomePod. The HomePod mini is a smaller version of…

Hope and Reason to Live

Every life matters in God’s kingdom, from the youngest to the oldest, to the weakest to the strongest, the informed to the healthy. This week, we celebrate reasons to live, every day, regardless of…