Closed jmax01 closed 9 years ago
This is exotic. Which means lombok won't do it; lombok does boilerplate, and boilerplate is defined as non-exotic.
We always have to weigh exoticness against difficulty of handwriting it. Fortunately, here the call is easy: Adding a 'newBuilder' instance method is easy to do and doesn't require you to modify it when you add or remove fields (you'd have to modify it when you add or remove generics params but that happens far less often).
Similar to
toBuilder=true
, add the ability to have aB newBuilder()
instance method generated via@Builder(newBuilder=true)
so buildable objects can be treated generically.A great thing about
@Builder
is that by declaring an empty*Builder
class one can have the generated builder implement interfaces:Suppose we want to be able to change a value in a generic fashion and instead of returning the builder we return the built object?
We simply add a
WithBuild<T>
interface to our builder and make sure the builder type (B
) inpopulateName(B builder)
implementsWithBuild<T>
:Now suppose we want to create new objects with different values from existing objects.
We need to add
toBuilder=true
to our@Builder
annotation and introduce the interfaceWithBuilder<T extends WithBuilder<T,B>,B extends WithBuild<T>>
that has aB toBuilder()
method.We also add a new field
type
as BtoBuilder()
on a single valued type is not very valuable.It is important to note that aside from implementing the
WithBuilder
interface we do not have to modify the class:Now suppose we want to change a subset of fields in some cases and change them all in other cases?
In that case we need both a
B toBuilder()
andB newBuilder()
method on theWithBuilder
interface.However since
B builder()
is static and not an instance method it is not visible generically and thus we are required to implement aB newBuilder()
instance method on every object that implements WithBuilder.Automatically adding a
B newBuilder()
instance method would eliminate the need for this boilerplate: