Randgalt / record-builder

Record builder generator for Java records

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Options to create unmodifiableLXYZ

danp11 opened this issue · comments

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

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

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

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

Hi @Randgalt

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

Thanks,

/Dan