Randgalt / record-builder

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

Options to create unmodifiableLXYZ #56

Closed danp11 closed 3 years ago

danp11 commented 3 years ago

Hi again:-)

Just thinking if it could be a good idea to have an new RecordBuilder.Options like "useUnmodifiableCollections" or similar?

And then for List/Map/Set use Collections.unmodifiableXYZ in the builder..?

What is your thoughts around this suggestion?

Have a nice weekend :-)

/Dan

danp11 commented 3 years ago

The option if one want's immutable collections in the record is to specify this directly in the record like:

public record Book (String title, int numPages, List chapters) {

public Book {
    chapters = List.copyOf(chapters);
}

}

But IMO it would be nicer to let the builder take care of this via the suggested option. And instead of Collections.unmodifiableXYZ instead use the copyOf() method on List/Map/Set

danp11 commented 3 years ago

Hi, I'll try to fix this one.

PS I don't think the issue "Add emptyDefaultForCollections options #58" is a duplicate of this enhancement.

From my understanding #58 seems to be more like the option emptyDefaultForOptional but for Collections DS

My intention in this ticket is that we do a copyOf() in the build method of the builder.

/Dan

Randgalt commented 3 years ago

@danp11 have you started? I think I'll handle this issue and yours in one PR.

danp11 commented 3 years ago

Hi @Randgalt

No, I haven't started so feel free to implement this suggestion if you want.

Thanks,

/Dan