KangoV · GitHub

When copying an object and changing many attributes, multiple intermediate copies are created, which is a waste.

So instead of:

Value changedValue =
    ImmutableValue.copyOf(existingValue)
        .withName("Changed Name")
        .withValues(Arrays.asList("Only new value"))

It would be better to supply a builder function to the copyOf method:

Value changedValue =
    ImmutableValue.copyOf(existingValue, builder -> builder
        .name("Changed Name")
        .values(Arrays.asList("Only new value")));

This would only make a single copy with multiple changes applied. The existing builder could be used, but would require an interface be generated for it.

Read the original on github.com ↗