-
Notifications
You must be signed in to change notification settings - Fork 44
@Builder @Builder.Extension
peichhorn edited this page Jul 21, 2012
·
4 revisions
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.
import lombok.Builder;
@Builder
public class Person {
private final String name;
private final String firstname;
private int age;
}
public class Person {
private final String name;
private final String firstname;
private int age;
private Person(Person.$Builder builder) {
this.name = builder.name;
this.firstname = builder.firstname;
this.age = builder.age;
}
public static Person.NameDef person() {
return new Person.$Builder();
}
private static class $Builder implements Person.NameDef, Person.FirstnameDef, Person.OptionalDef {
private String name;
private String firstname;
private int age;
public Person.FirstnameDef name(String name) {
this.name = name;
return this;
}
public Person.OptionalDef firstname(String firstname) {
this.firstname = firstname;
return this;
}
public Person.OptionalDef age(int age) {
this.age = age;
return this;
}
public Person build() {
return new Person(this);
}
}
public static abstract interface FirstnameDef {
public abstract Person.OptionalDef firstname(String paramString);
}
public static abstract interface NameDef {
public abstract Person.FirstnameDef name(String paramString);
}
public static abstract interface OptionalDef {
public abstract OptionalDef age(int paramInt);
public abstract Person build();
}
}
(Documentation pending)
TODO:
- explain the interface mechanism
- explain the default value mechanism
- explain the special treatment of map and collection fields
(Documentation pending)
TODO:
- explain
@Builder.Extension
- explain parameter
convenientMethods
- explain parameter
exclude
- explain parameter
prefix
- explain parameter
callMethods
- explain parameter
allowReset
I am not able to run @Action. If I provide Action1 implementation it works.
implementation private static Action1 println() { return new Action1() { public void apply(final Object o) { System.out.println(o); } };
pom : com.github.peichhorn lombok-pg 0.11.3
<dependency>
<groupId>com.github.peichhorn</groupId>
<artifactId>lombok-pg</artifactId>
<version>0.11.3</version>
<classifier>runtime</classifier>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>