本篇文章会讲到:@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 {
1private String name; 2 3private String phone; 4 5private int age;}
编译后:
1public class Example { 2 3 private String name; 4 5 private String phone; 6 7 private int age; 8 9 public String getName() { 10 return name; 11 } 12 13 public Example setName(String name) { 14 this.name = name; 15 return this; 16 } 17 18 public String getPhone() { 19 return phone; 20 } 21 22 public Example setPhone(String phone) { 23 this.phone = phone; 24 return this; 25 } 26 27 public int getAge() { 28 return age; 29 } 30 31 public Example setAge(int age) { 32 this.age = age; 33 return this; 34 } 35}
验证结果:
1Example example = new Example() 2 .setName("piao") 3 .setAge(24) 4 .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<String> occupations; }
编译后:
1import java.util.Set; 2 3public class BuilderExample { 4 private long created; 5 private String name; 6 private int age; 7 private Set<String> occupations; 8 9 BuilderExample(String name, int age, Set<String> occupations) { 10 this.name = name; 11 this.age = age; 12 this.occupations = occupations; 13 } 14 15 private static long $default$created() { 16 return System.currentTimeMillis(); 17 } 18 19 public static BuilderExampleBuilder builder() { 20 return new BuilderExampleBuilder(); 21 } 22 23 public static class BuilderExampleBuilder { 24 private long created; 25 private boolean created$set; 26 private String name; 27 private int age; 28 private java.util.ArrayList<String> occupations; 29 30 BuilderExampleBuilder() { 31 } 32 33 public BuilderExampleBuilder created(long created) { 34 this.created = created; 35 this.created$set = true; 36 return this; 37 } 38 39 public BuilderExampleBuilder name(String name) { 40 this.name = name; 41 return this; 42 } 43 44 public BuilderExampleBuilder age(int age) { 45 this.age = age; 46 return this; 47 } 48 49 public BuilderExampleBuilder occupation(String occupation) { 50 if (this.occupations == null) { 51 this.occupations = new java.util.ArrayList<String>(); 52 } 53 54 this.occupations.add(occupation); 55 return this; 56 } 57 58 public BuilderExampleBuilder occupations(Collection<? extends String> occupations) { 59 if (this.occupations == null) { 60 this.occupations = new java.util.ArrayList<String>(); 61 } 62 63 this.occupations.addAll(occupations); 64 return this; 65 } 66 67 public BuilderExampleBuilder clearOccupations() { 68 if (this.occupations != null) { 69 this.occupations.clear(); 70 } 71 72 return this; 73 } 74 75 public BuilderExample build() { 76 // complicated switch statement to produce a compact properly sized immutable set omitted. 77 Set<String> occupations = ...; 78 return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations); 79 } 80 81 @java.lang.Override 82 public String toString() { 83 return "BuilderExample.BuilderExampleBuilder(created = " + this.created + ", name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")"; 84 } 85 } 86}
验证结果:
1BuilderExample example = BuilderExample.builder() 2 .name("piao") 3 .age(24) 4 .occupation("ONE") 5 .occupation("TWO") 6 .build();