Skip to content

#3334: Support returning unmodifiable collections and maps#3356

Open
j-be wants to merge 5 commits intomapstruct:mainfrom
j-be:j-be/3334-Unmodifiable-Collections-and-Maps
Open

#3334: Support returning unmodifiable collections and maps#3356
j-be wants to merge 5 commits intomapstruct:mainfrom
j-be:j-be/3334-Unmodifiable-Collections-and-Maps

Conversation

@j-be
Copy link
Copy Markdown

@j-be j-be commented Aug 6, 2023

With the introduction of record into the language, immutable datatypes are a great way to move stuff around in Java in an efficient and safe (as in thread-safe) way.

Providing immutable - or in Java terms: unmodifiable - collections provides another piece to the puzzle.

This PR introduces a new annotation, @Unmodifiable, which if put on apstract mapper methods lets them return unmodifiable collections.

Fixes #3334

@filiphr filiphr self-requested a review March 3, 2024 12:34
@filiphr
Copy link
Copy Markdown
Member

filiphr commented Mar 3, 2024

Thanks for your work on this @j-be. I managed to review it and it is a really good direction. There are some things that are still missing:

  • Need support for mapping Stream to List, Collection etc. (this is part of the StreamMappingMethod
  • We need error handling for mapping to types that do not support unmodifiable e.g. if you do @IterableMapping(unmodifiable = true) when the return type is ArrayList or some other type of collection that cannot be called on Collections.unmodifiableXXX then we need to have a compile error from MapStruct instead of leaving the compile error on the generated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add option to map Collections to Immutable Lists

2 participants