Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lombok - @Builder pattern in multiple shots

I use @Builder of lombok project, so consider I have this example:

@Builder
public class Client {

    private @Getter @Setter Integer id;
    private @Getter @Setter String name;

}

Which is equivalent to:

public class Client {

    private @Getter @Setter Integer id;
    private @Getter @Setter String name;

    public static class Builder {

        private Integer id;
        private String name;

        private Builder() {
        }

        public Builder id(final Integer value) {
            this.id = value;
            return this;
        }

        public Builder name(final String value) {
            this.name = value;
            return this;
        }

        public Client build() {
            return new Client(this);
        }
    }

    public static Client.Builder builder() {
        return new Client.Builder();
    }

    private Client(final Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
    }
}

When I try to set all the the fields in one shot there are no problem:

public static void main(String[] args) {
    Client client = Client.builder()
            .id(123)
            .name("name")
            .build();
}

Output:

Client{id=123, name=name}

Now, consider I want to make it in multiple shots. For example:

public static void main(String[] args) {
    Client client = Client.builder()
            .id(123)//<-------------------------- Set just the id
            .build();

    client = Client.builder()
            .name("name")//<--------------------- Set name
            .build();

}

This logically return null for id:

Client{id=null, name=name}

Generally, without lombok, I solve this issue with adding a new constructor in the Builder class which take the same object:

public static class Builder {
    // ...
    public Builder(Client client) {
        this.id = client.id;
        this.name = client.name;
    }
    // ...
}

Then I pass my object to that constructor:

Client client = Client.builder()
        .id(123)
        .build();

client = new Client.Builder(client)//<---------------- Like this 
        .name("name")
        .build();

This solves my issue, but I can't arrive to solve it with lombok. Is there any way to solve this issue?

like image 905
YCF_L Avatar asked Dec 02 '22 10:12

YCF_L


2 Answers

You can use the toBuilder property to do that.

@Builder(toBuilder=true)
public class Client {
    private @Getter @Setter Integer id;
    private @Getter @Setter String name;
}

and then you can use it like that

public void main(String[] args){
    Client client = Client.builder()
        .id(123)
        .build();

    client = client.toBuilder()
        .name("name")
        .build();
}
like image 193
eric.v Avatar answered Dec 27 '22 23:12

eric.v


Depending on your use case, you can also keep a reference to an existing builder. You can have multiple to the build method.

Disclosure: I am a lombok developer.

like image 43
Roel Spilker Avatar answered Dec 27 '22 23:12

Roel Spilker