This is the old documentation of lombok-pg. The new version can be found in the wiki of the github repository. Take me to the new Version then!

@Builder/@Builder.Extension

Overview

This annotation allows you to create a builder pattern for initializing your class. Just specify the prefix for the classes (e.g. 'with', or 'and', etc.) and lombok will create the required methods for you.

With Lombok

01 import lombok.Builder;
02 
03 import java.util.List;
04 
05 @Builder(prefix="with", callMethods="save")
06 class BuilderExample {
07   private final String text;
08   private final int id;
09   private String optionalVal1 = "default";
10   private List<Long> optionalVal2;
11   
12   public boolean save() throws IOException {
13     // do something
14   }
15   
16   @Builder.Extension
17   private void withIdAndText(String id, String text) {
18     this.id = Integer.valueOf(id);
19     this.text = text;
20   }
21   
22   @Builder.Extension
23   private void withOptionalVal1(Class<?> clazz) {
24     this.optionalVal1 = clazz.getSimpleName();
25   }
26 }

Vanilla Java

01 import java.util.List;
02 
03 class BuilderExample {
04   private final String text;
05   private final int id;
06   private String optionalVal1;
07   private List<Long> optionalVal2;
08   
09   public boolean save() throws IOException {
10     // do something
11   }
12 
13   private BuilderExample(final $Builder builder) {
14     this.text = builder.text;
15     this.id = builder.id;
16     this.optionalVal1 = builder.optionalVal1;
17     this.optionalVal2 = builder.optionalVal2;
18   }
19 
20   public static $TextDef builderExample() {
21     return new $Builder();
22   }
23   
24   public static interface $TextDef {
25     $IdDef withText(final String arg0);
26     
27     $OptionalDef withIdAndText(String id, String text);
28   }
29   
30   public static interface $IdDef {
31     $OptionalDef withId(final int arg0);
32   }
33   
34   public static interface $OptionalDef {
35     $OptionalDef withOptionalVal1(final String arg0);
36     
37     $OptionalDef withOptionalVal2(final List<Long> arg0);
38     
39     BuilderExample build();
40     
41     public boolean save() throws IOException;
42     
43     $OptionalDef withOptionalVal1(Class<?> clazz);
44   }
45   
46   private static class $Builder implements $TextDef, $IdDef, $OptionalDef {
47     private String text;
48     private int id;
49     private String optionalVal1 = "default";
50     private List<Long> optionalVal2;
51     
52     public $IdDef withText(final String arg0) {
53       this.text = arg0;
54       return this;
55     }
56     
57     public $OptionalDef withIdAndText(String id, String text) {
58       this.id = Integer.valueOf(id);
59       this.text = text;
60       return this;
61     }
62     
63     public $OptionalDef withId(final int arg0) {
64       this.id = arg0;
65       return this;
66     }
67     
68     public $OptionalDef withOptionalVal1(final String arg0) {
69       this.optionalVal1 = arg0;
70       return this;
71     }
72     
73     public $OptionalDef withOptionalVal2(final List<Long> arg0) {
74       this.optionalVal2 = arg0;
75       return this;
76     }
77     
78     public BuilderExample build() {
79       return new BuilderExample(this);
80     }
81     
82     public boolean save() throws IOException {
83       return build().save();
84     }
85     
86     public $OptionalDef withOptionalVal1(Class<?> clazz) {
87       this.optionalVal1 = clazz.getSimpleName();
88       return this;
89     }
90   }
91 }

Small print

No further information at this point.