Lombok使用4:链式调用

本篇文章会讲到:@Accessors、@Builder 注解使用。

1、@Accessors使用

  • @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 是现在比较推崇的一种构建值对象的方式。该描述符用于将类改造成 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();

更多阅读

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我