【lombok】@Data - @ToString/@EqualsAndHashCode/@Getter/@Setter/@RequiredArgsCons多注解组合注解

概述

@Data是将@ToString,@EqualsAndHashCode,@Getter/@SetterRequiredArgsConstructor功能捆绑在一起的方便快捷方式。
也就是说,@Data生成通常与简单POJO(普通旧JAVA对象)和组件相关联的所有样板代码:所有字段的getter方法,所有非final字段的setter方法,
涉及类字段的相应toString,equalshashCode实现,还有所有final字段已初始化的构造器,
以及被@NonNull标记且未初始化的所有非final字段,以确保该字段始终不会为null。

@Data就像是在类上隐含@Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor注解一样(除非没有构造器生成,如果任何明确的构造器已存在)。然而这些注解的参数(比如callSuper,includeFieldNamesexclude)不能通过@Data设置。
如果你想为这些参数设置非默认数值,只需要明确的添加注解,@Data足够智能来满足这些注解。

所有的getter和setter方法作用域都是public。可以通过在类或者字段上添加明确的@Getter和/或@Setter注解来覆盖访问级别。
你还可以使用此注解(同AccessLevel.NONE组合使用)来完全禁止生成getter/setter。

所有标记为transient的字段将不被hashCodeequals考虑。所有的静态字段将彻底跳过(任何生成的方法都不会包含,且不会为他们生成setter/getter方法)。

如果类里已经包含某一个方法和即将生成的方法的方法名和参数个数一致,则该将要生成的方法不会被生成,且没有任何警告和错误出现。
打个比方,如果已有一个方法签名为equals(AnyType param),即使从技术上来说,由于具有不同的参数类型,它可能是完全不同的方法,依然不会再生成equals方法。
同样的规则也适用于构造器(任何明确的构造器都会阻止@Data生成一个新的),toStringequals,和所有的getter/setter方法。
你可以在构造器或者方法上标记@lombok.experimental.Tolerate注解来从lombok隐藏它们。

@Data正好也可以处理泛型参数。为了减少构造泛型类对象的样板代码,你可以使用staticConstructor参数来生成私有的构造器,同返回一个新实例的静态方法一样。
这个方法,javac会推断变量类型,因此通过这样申明:

@Data(staticConstructor="of") 
class Foo<T> { private T x;}

你可以通过如此写法来创建新的实例:

Foo.of(5);

而不是:

new Foo<Integer>(5);

使用lombok


import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;

@Data public class DataExample {
  private final String name;
  @Setter(AccessLevel.PACKAGE) private int age;
  private double score;
  private String[] tags;
  
  @ToString(includeFieldNames=true)
  @Data(staticConstructor="of")
  public static class Exercise<T> {
    private final String name;
    private final T value;
  }
}

不使用lombok


 import java.util.Arrays;

public class DataExample {
  private final String name;
  private int age;
  private double score;
  private String[] tags;
  
  public DataExample(String name) {
    this.name = name;
  }
  
  public String getName() {
    return this.name;
  }
  
  void setAge(int age) {
    this.age = age;
  }
  
  public int getAge() {
    return this.age;
  }
  
  public void setScore(double score) {
    this.score = score;
  }
  
  public double getScore() {
    return this.score;
  }
  
  public String[] getTags() {
    return this.tags;
  }
  
  public void setTags(String[] tags) {
    this.tags = tags;
  }
  
  @Override public String toString() {
    return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
  }
  
  protected boolean canEqual(Object other) {
    return other instanceof DataExample;
  }
  
  @Override public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof DataExample)) return false;
    DataExample other = (DataExample) o;
    if (!other.canEqual((Object)this)) return false;
    if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
    if (this.getAge() != other.getAge()) return false;
    if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
    if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
    return true;
  }
  
  @Override public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final long temp1 = Double.doubleToLongBits(this.getScore());
    result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
    result = (result*PRIME) + this.getAge();
    result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
    result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
    return result;
  }
  
  public static class Exercise<T> {
    private final String name;
    private final T value;
    
    private Exercise(String name, T value) {
      this.name = name;
      this.value = value;
    }
    
    public static <T> Exercise<T> of(String name, T value) {
      return new Exercise<T>(name, value);
    }
    
    public String getName() {
      return this.name;
    }
    
    public T getValue() {
      return this.value;
    }
    
    @Override public String toString() {
      return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
    }
    
    protected boolean canEqual(Object other) {
      return other instanceof Exercise;
    }
    
    @Override public boolean equals(Object o) {
      if (o == this) return true;
      if (!(o instanceof Exercise)) return false;
      Exercise<?> other = (Exercise<?>) o;
      if (!other.canEqual((Object)this)) return false;
      if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
      if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
      return true;
    }
    
    @Override public int hashCode() {
      final int PRIME = 59;
      int result = 1;
      result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
      result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
      return result;
    }
  }
}

本章完!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值