Spring Cloud Alibaba-Lombok(五)

簡介

經過lombok提供的註解,咱們能夠編寫更加優雅簡潔的代碼,編譯後的代碼即完整,不存在性能問題。java

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.bash

IntelliJ安裝Lombok

  • 下載插件
  • 引入依賴
<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.8</version>
			<scope>provided</scope>
		</dependency>
複製代碼

經常使用註解說明

  • @Data

Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.ide

@Data
public class User {
  private String name;
  private Integer age;
}
複製代碼
public class User {
    private String name;
    private Integer age;

    public User() {
    }

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

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

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

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

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

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

                return true;
            }
        }
    }

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

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

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

複製代碼
  • @Builder
@Builder
public class User {
  private String name;
  private Integer age;
}
複製代碼
public class User {
    private String name;
    private Integer age;

    User(final String name, final Integer age) {
        this.name = name;
        this.age = age;
    }

    public static User.UserBuilder builder() {
        return new User.UserBuilder();
    }

    public static class UserBuilder {
        private String name;
        private Integer age;

        UserBuilder() {
        }

        public User.UserBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public User.UserBuilder age(final Integer age) {
            this.age = age;
            return this;
        }

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

        public String toString() {
            return "User.UserBuilder(name=" + this.name + ", age=" + this.age + ")";
        }
    }
複製代碼
  • @NoArgsConstructor
@NoArgsConstructor
public class User {
  private String name;
  private Integer age;
}
複製代碼
public class User {
    private String name;
    private Integer age;

    public User() {
    }
}
複製代碼
  • @AllArgsConstructor
@AllArgsConstructor
public class User {
  private String name;
  private Integer age;
}
複製代碼
public class User {
    private String name;
    private Integer age;

    public User(final String name, final Integer age) {
        this.name = name;
        this.age = age;
    }
}
複製代碼
  • @Slf4j
@Slf4j
public class User {
  private String name;
  private Integer age;
}
複製代碼
public class User {
    private static final Logger log = LoggerFactory.getLogger(User.class);
    private String name;
    private Integer age;
    public User() {
    }
}
複製代碼
  • @NonNull
public class User {
  private String name;
  private Integer age;

  public void setName(@NonNull String name) {
    this.name = name;
  }
}
複製代碼
public class User {
    private String name;
    private Integer age;

    public User() {
    }

    public void setName(@NonNull String name) {
        if (name == null) {
            throw new NullPointerException("name is marked non-null but is null");
        } else {
            this.name = name;
        }
    }
}
複製代碼
  • @Cleanup
public static void main(String args[]) throws Exception{
    @Cleanup InputStream inputStream = new FileInputStream(args[0]);
    inputStream.read();
  }
複製代碼
public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream(args[0]);

        try {
            inputStream.read();
        } finally {
            if (Collections.singletonList(inputStream).get(0) != null) {
                inputStream.close();
            }

        }

    }
複製代碼

最後

文章如有謬誤之處,但願廣大讀者指正,互相交流,共同提升。性能

相關文章
相關標籤/搜索