I have the following class with Lombok builder:
@Builder(toBuilder = true)
@Data
public class TaskSettings {
private List<Task> tasks;
public static class TaskSettingBuilder {
private List<Task> tasks = new ArrayList<>();
public TaskSettingBuilder longLivedTask(Duration duration) {
// construct long-lived task
tasks.add(new Task());
return this;
}
public TaskSettingBuilder shortLivedTask(Duration duration) {
// construct short-lived task
tasks.add(new Task());
return this;
}
}
}
I have a custom builder which can take info for different types of tasks, construct and appropriate task to add to the list. This works fine but when I use the toBuilder
implementation, it uses the original list instead of copying it. So if I add something to the new instance, old instance would be updated:
TaskSettings ts = TaskSettings.builder()
.longLivedTask(Duration.ofDays(365))
.build();
TaskSettings ts2 = ts.toBuilder().shortLivedTask(Duration.ZERO).build();
ts
would now have the short-lived task. I tried @Singular
annotation but that generates builder method that accepts Task
object rather than allowing me to construct it. I know I can move the tasking building to a builder of the Task
object instead, but I would like to do it as part of the TaskSettings
. How do I accomplish this?