Randgalt / record-builder

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

Adds special handling for collection record components #60

Closed Randgalt closed 3 years ago

Randgalt commented 3 years ago

Adds special handling for record components of type java.util.List, java.util.Set, java.util.Map and java.util.Collection. When the record is built, any components of these types are passed through an added shim method that uses the corresponding immutable collection (e.g. List.copyOf(o)) or an empty immutable collection if the component is null.

This is what the generated builder looks like: CollectionRecordBuilder.java

Closes #56 Closes #58

Randgalt commented 3 years ago

@krzyk I believe this satisfies your request. I didn't see the point in only adding an initializer for collections.

Randgalt commented 3 years ago

cc @danp11

danp11 commented 3 years ago

Very nice and Thanks for adding this!

Only thing Im thinking of is if it should be mentioned in RecordBuilder and in the JavaDoc for useImmutableCollections that @NotNull and interpretNotNullsPattern = true takes precedence over "or an empty immutable collection if the component is {@code null}."

Or maybe it is better to leave that out to not complicate things to much :-)

Thanks again for this PR.

Randgalt commented 3 years ago

@danp11 yeah - I think letting it alone is best. Maybe it can be improved in the future, who knows. If you use a combination of options maybe a different generation mode emerges.