码迷,mamicode.com
首页 > 其他好文 > 详细

lombok @Builder注解

时间:2018-12-27 00:31:56      阅读:1620      评论:0      收藏:0      [点我收藏+]

标签:size   prime   mpi   public   creat   intellij   使用   static   返回   

这里对lombok的@Builder@Data组合的使用示例

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class People {
    private String name;
    private String sex;
    private int age;
}
@Test
    public void testBuilderAnnotation(){
        People.PeopleBuilder builder = People.builder();
        People.PeopleBuilder peopleBuilder = builder.name("LuoTianyan").age(23);
        if(true){
            peopleBuilder.sex("female");
        }
        People luoTianyan = peopleBuilder.build();
        System.out.println(luoTianyan.toString());
        //People(name=LuoTianyan, sex=female, age=23)

        People people = new People("LuoTianyan","female",23);
        System.out.println(luoTianyan.equals(people));
        //true
    }

class People加上了@Builder@Data注解后,多了一个静态内部类PeopleBuilder,People调用静态方法builder生成PeopleBuilder对象,PeopleBuilder可以使用".属性名(属性值)"的方式进行属性设置,再调用build()方法就生成了People对象,并且如果两个对象的属性如果相同,就会认为这两个对象相等,即重写了hashCode和equls方法。

 

在Intellij IDEA下,查看反编译的文件People.class;

可以看到,生成的有:

Getter和Setter方法;

访问类型是private无参构造方法,访问类型为defaul的全部参数的构造方法;

重写hashCodeequals、toString方法,则People可以做为Map的key;

访问类型为public的静态方法builder,返回的是People.PeopleBuilder对象,非单例;

访问类型为public的静态内部类PeopleBuilder,该类主要有build方法,返回类型是People;

最后还有个canEqual方法,判断是否与People同类型。

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


public class People {
    private String name;
    private String sex;
    private int age;

    People(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public static People.PeopleBuilder builder() {
        return new People.PeopleBuilder();
    }

    private People() {
    }

    public String getName() {
        return this.name;
    }

    public String getSex() {
        return this.sex;
    }

    public int getAge() {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof People)) {
            return false;
        } else {
            People other = (People)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label39: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label39;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label39;
                    }

                    return false;
                }

                Object this$sex = this.getSex();
                Object other$sex = other.getSex();
                if (this$sex == null) {
                    if (other$sex != null) {
                        return false;
                    }
                } else if (!this$sex.equals(other$sex)) {
                    return false;
                }

                if (this.getAge() != other.getAge()) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof People;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }

    public String toString() {
        return "People(name=" + this.getName() + ", sex=" + this.getSex() + ", age=" + this.getAge() + ")";
    }

    public static class PeopleBuilder {
        private String name;
        private String sex;
        private int age;

        PeopleBuilder() {
        }

        public People.PeopleBuilder name(String name) {
            this.name = name;
            return this;
        }

        public People.PeopleBuilder sex(String sex) {
            this.sex = sex;
            return this;
        }

        public People.PeopleBuilder age(int age) {
            this.age = age;
            return this;
        }

        public People build() {
            return new People(this.name, this.sex, this.age);
        }

        public String toString() {
            return "People.PeopleBuilder(name=" + this.name + ", sex=" + this.sex + ", age=" + this.age + ")";
        }
    }
}

 

lombok @Builder注解

标签:size   prime   mpi   public   creat   intellij   使用   static   返回   

原文地址:https://www.cnblogs.com/theRhyme/p/10181103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!