@Builder @Builder.Extension - peichhorn/lombok-pg GitHub Wiki
@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.
Example
With Lombok
import lombok.Builder;
@Builder
public class Person {
private final String name;
private final String firstname;
private int age;
}
Vanilla Java
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();
}
}
Behind the Scenes
(Documentation pending)
TODO:
- explain the interface mechanism
- explain the default value mechanism
- explain the special treatment of map and collection fields
Configuration
(Documentation pending)
TODO:
- explain
@Builder.Extension
- explain parameter
convenientMethods
- explain parameter
exclude
- explain parameter
prefix
- explain parameter
callMethods
- explain parameter
allowReset