I have defined a class with a builder and now I would like to limit boilerplate code using Lombok's @Builder annotation.
public class ClientApp {
private UUID clientId;
ClientApp(UUID clientId) {
this.clientId = clientId;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private UUID clientId;
public Builder clientId(String clientId) {
return clientId(UUID.fromString(clientId));
}
public Builder clientId(UUID clientId) {
this.clientId = clientId;
return this;
}
public ClientApp build() {
return new ClientApp(this.clientId);
}
}
public Builder clientId(String clientId) {
return clientId(UUID.fromString(clientId));
}
}
However, the annotation will not generate clientId(String) method, only clientId(UUID). How can I generate it with Lombok?
Well, lombok will not generate this for you but you can use @Builder with and have a ClientAppBuilder class containing the one method that accepts a String and routes it to the other, to be generated, method. Possibly you need to mark your method with @Tolerate, otherwise Lombok will not generate the UUID accepting method.
Disclosure: I am a Lombok developer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With