Randgalt / record-builder

Record builder generator for Java records
Apache License 2.0
756 stars 55 forks source link

Support more Collection (and Map) types for immutable collections #151

Open JanecekPetr opened 1 year ago

JanecekPetr commented 1 year ago

Namely, SortedSet and NavigableSet and SortedMap and NavigableMap. All of those have an unmodifiable wrapper available in Collections. While not ideal and slightly worse than the actually immutable copies available via List.copyOf and friends, it's way better to have an unmodifiable wrapper (and a collection initialized to empty when nothing is in) than having null.

This was really surprising to consistently get empty collections everywhere and suddenly there was a null because we changed a Set to SortedSet.

In a perfect world, there would be support for even more types, like what Immutables do: Multisets and Multimaps from Guava, BiMap, all ImmutableCollections, EnumMaps. But let's not jump there yet :)

Randgalt commented 1 year ago

A PR would be appreciated for this.

pawellabaj commented 1 year ago

@JanecekPetr , you can use now unmodifiable views - see #154

pawellabaj commented 1 year ago

@JanecekPetr workaround proposal: use AutoRecord with ARICE extension.

It supports the most important (all ?) interfaces from java.util and com.google.common.collect packages. However, it requires a Guava library for generated records, so it won't suit everyone.