Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

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?

Upvotes: 4

Views: 5618

Answers (2)

Roel Spilker
Roel Spilker

Reputation: 34562

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.

Upvotes: 9

eric.v
eric.v

Reputation: 615

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();
}

Upvotes: 12

Related Questions