本篇文章会讲到:@Accessors、@Builder 注解使用。
1、@Accessors使用
注注解在属性(类)上,配置getter和setter方法的生成结果,分别有三个属性:fluent、chain、prefix。
fluent的中文含义是流畅的,设置为true,则getter和setter方法的方法名都是基础属性名,且setter方法返回当前对象。
chain的中文含义是链式的,设置为true,则setter方法返回当前对象。
prefix的中文含义是前缀,用于生成getter和setter方法的字段名会忽视指定前缀(遵守驼峰命名)。
import lombok.Data; import lombok.experimental.Accessors;
@Data @Accessors(chain = true) public class Example {
private String name; private String phone; private int age;
}
编译后:
public class Example {
private String name;
private String phone;
private int age;
public String getName() {
return name;
}
public Example setName(String name) {
this.name = name;
return this;
}
public String getPhone() {
return phone;
}
public Example setPhone(String phone) {
this.phone = phone;
return this;
}
public int getAge() {
return age;
}
public Example setAge(int age) {
this.age = age;
return this;
}
}
验证结果:
Example example = new Example()
.setName("piao")
.setAge(24)
.setPhone("138");
2、@Builder使用
builder 是现在比较推崇的一种构建值对象的方式。该描述符用于将类改造成 builder(建造者)模式,用在类、方法或者构造函数上。
import lombok.Builder; import lombok.Singular; import java.util.Set;
@Builder public class BuilderExample { @Builder.Default private long created = System.currentTimeMillis(); private String name; private int age; @Singular private Set
occupations; }
编译后:
import java.util.Set;
public class BuilderExample {
private long created;
private String name;
private int age;
private Set<String> occupations;
BuilderExample(String name, int age, Set<String> occupations) {
this.name = name;
this.age = age;
this.occupations = occupations;
}
private static long $default$created() {
return System.currentTimeMillis();
}
public static BuilderExampleBuilder builder() {
return new BuilderExampleBuilder();
}
public static class BuilderExampleBuilder {
private long created;
private boolean created$set;
private String name;
private int age;
private java.util.ArrayList<String> occupations;
BuilderExampleBuilder() {
}
public BuilderExampleBuilder created(long created) {
this.created = created;
this.created$set = true;
return this;
}
public BuilderExampleBuilder name(String name) {
this.name = name;
return this;
}
public BuilderExampleBuilder age(int age) {
this.age = age;
return this;
}
public BuilderExampleBuilder occupation(String occupation) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.add(occupation);
return this;
}
public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.addAll(occupations);
return this;
}
public BuilderExampleBuilder clearOccupations() {
if (this.occupations != null) {
this.occupations.clear();
}
return this;
}
public BuilderExample build() {
// complicated switch statement to produce a compact properly sized immutable set omitted.
Set<String> occupations = ...;
return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations);
}
@java.lang.Override
public String toString() {
return "BuilderExample.BuilderExampleBuilder(created = " + this.created + ", name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
}
}
}
验证结果:
BuilderExample example = BuilderExample.builder()
.name("piao")
.age(24)
.occupation("ONE")
.occupation("TWO")
.build();