Peter Penzov
Peter Penzov

Reputation: 1668

Configuring lombok for builder

I want to avoid multiple constructors, so I want to use a builder design pattern, by using lombok library, it can be more easier, so I want to annotate class of ContractDTO with this library annotation:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
class ContractDTO {

    private Integer id;  
    private String name;
    private Integer acquirerId;    
    private Integer terminalId;    
    private String merchantId;

}

then your code can be :

...
.map(g -> new ContractDTO().toBuilder()
        .name(g.getName())
        .merchantName(g.getMerchantId())
        .build()
)....

But when I try to compile the code I get cannot find symbol [ERROR] symbol: method toBuilder()

Probably I need to generate the code in advance?

Upvotes: 2

Views: 620

Answers (2)

AYOUB_MA
AYOUB_MA

Reputation: 48

By default your IDE cant detect what lombok has generated ,so, to avoid the compilation errors which appear after you added some annotations, i suggest you to install the lombok plugin into your IDE, so you can have your classes generated anc detected by your IDE in real time.

Upvotes: 0

Anurag Dwivedi
Anurag Dwivedi

Reputation: 174

You can use it like this:

 ContractDTO.builder()
    .name(g.getName())
    .merchantName(g.getMerchantId())
    .build();

If we want to create copies or near-copies of objects, we can add the property toBuilder = true to the @Builder annotation. This tells Lombok to add a toBuilder() method to our Class. When we invoke the toBuilder() method, it returns a builder initialized with the properties of the instance it is called on.

Upvotes: 2

Related Questions