问题
Lets say I have a lombok annotated class like
@Builder
class Band {
String name;
String type;
}
I know I can do:
Band rollingStones = Band.builder().name("Rolling Stones").type("Rock Band").build();
Is there an easy way to create an object of Foo using the existing object as a template and changing one of it's properties?
Something like:
Band nirvana = Band.builder(rollingStones).name("Nirvana");
I can't find this in the lombok documentation.
回答1:
You can use the toBuilder parameter to give your instances a toBuilder() method.
@Builder(toBuilder=true)
class Foo {
int x;
...
}
Foo f0 = Foo.builder().build();
Foo f1 = f0.toBuilder().x(42).build();
From the documentation:
If using @Builder to generate builders to produce instances of your own class (this is always the case unless adding @Builder to a method that doesn't return your own type), you can use @Builder(toBuilder = true) to also generate an instance method in your class called toBuilder(); it creates a new builder that starts out with all the values of this instance.
Disclaimer: I am a lombok developer.
回答2:
Is there an easy way to create an object of Foo using the existing object as a template and changing one of it's properties? (emphasis mine)
If you really want to change a single property, then there's a nicer and more efficient way:
@Wither
class Band {
String name;
String type;
}
Band nirvana = rollingStones.withName("Nirvana");
The wither creates no garbage, but it can change just a single field. For changing many fields, you could use
withA(a).withB(b).withC(c)....
and produce tons of garbage (all intermediate results) but than toBuilder is more efficient and more natural.
来源:https://stackoverflow.com/questions/47069561/build-an-object-from-an-existing-one-using-lombok