Lombok 之 Data & Value

LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:

@Cleanup     

@Getter, @Setter

@ToString

@EqualsAndHashCode

@Constructor

@Data & @Value

@SneakyThrows

@Synchronized

@Getter(lazy=true)

@Log

先说@Data , 作为之前介绍几个annotation的一个统称,可谓整理了方便,@Data直接修饰POJO or beans, getter所有的变量,setter所有不为final的变量。如果你不需要默认的生成方式,直接填写你需要的annotation的就可以了。默认生成的所有的annotation都是public的,如果需要不同权限修饰符可以使用AccessLevel.NONE选项。当然@Data 也可以使用staticConstructor选项生成一个静态方法。
使用Data annotation可以简便的完成自己想要的内容:
Java代码   收藏代码
  1. import lombok.AccessLevel;  
  2. import lombok.Setter;  
  3. import lombok.Data;  
  4. import lombok.ToString;  
  5.   
  6. @Data public class DataExample {  
  7.   private final String name;  
  8.   @Setter(AccessLevel.PACKAGE) private int age;  
  9.   private double score;  
  10.   private String[] tags;  
  11.     
  12.   @ToString(includeFieldNames=true)  
  13.   @Data(staticConstructor="of")  
  14.   public static class Exercise<T> {  
  15.     private final String name;  
  16.     private final T value;  
  17.   }  
  18. }
 
使用后:
Java代码   收藏代码
  1. import java.util.Arrays;  
  2.   
  3. public class DataExample {  
  4.   private final String name;  
  5.   private int age;  
  6.   private double score;  
  7.   private String[] tags;  
  8.     
  9.   public DataExample(String name) {  
  10.     this.name = name;  
  11.   }  
  12.     
  13.   public String getName() {  
  14.     return this.name;  
  15.   }  
  16.     
  17.   void setAge(int age) {  
  18.     this.age = age;  
  19.   }  
  20.     
  21.   public int getAge() {  
  22.     return this.age;  
  23.   }  
  24.     
  25.   public void setScore(double score) {  
  26.     this.score = score;  
  27.   }  
  28.     
  29.   public double getScore() {  
  30.     return this.score;  
  31.   }  
  32.     
  33.   public String[] getTags() {  
  34.     return this.tags;  
  35.   }  
  36.     
  37.   public void setTags(String[] tags) {  
  38.     this.tags = tags;  
  39.   }  
  40.     
  41.   @Override public String toString() {  
  42.     return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";  
  43.   }  
  44.     
  45.   protected boolean canEqual(Object other) {  
  46.     return other instanceof DataExample;  
  47.   }  
  48.     
  49.   @Override public boolean equals(Object o) {  
  50.     if (o == thisreturn true;  
  51.     if (!(o instanceof DataExample)) return false;  
  52.     DataExample other = (DataExample) o;  
  53.     if (!other.canEqual((Object)this)) return false;  
  54.     if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;  
  55.     if (this.getAge() != other.getAge()) return false;  
  56.     if (Double.compare(this.getScore(), other.getScore()) != 0return false;  
  57.     if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;  
  58.     return true;  
  59.   }  
  60.     
  61.   @Override public int hashCode() {  
  62.     final int PRIME = 59;  
  63.     int result = 1;  
  64.     final long temp1 = Double.doubleToLongBits(this.getScore());  
  65.     result = (result*PRIME) + (this.getName() == null ? 0 : this.getName().hashCode());  
  66.     result = (result*PRIME) + this.getAge();  
  67.     result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));  
  68.     result = (result*PRIME) + Arrays.deepHashCode(this.getTags());  
  69.     return result;  
  70.   }  
  71.     
  72.   public static class Exercise<T> {  
  73.     private final String name;  
  74.     private final T value;  
  75.       
  76.     private Exercise(String name, T value) {  
  77.       this.name = name;  
  78.       this.value = value;  
  79.     }  
  80.       
  81.     public static <T> Exercise<T> of(String name, T value) {  
  82.       return new Exercise<T>(name, value);  
  83.     }  
  84.       
  85.     public String getName() {  
  86.       return this.name;  
  87.     }  
  88.       
  89.     public T getValue() {  
  90.       return this.value;  
  91.     }  
  92.       
  93.     @Override public String toString() {  
  94.       return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";  
  95.     }  
  96.       
  97.     protected boolean canEqual(Object other) {  
  98.       return other instanceof Exercise;  
  99.     }  
  100.       
  101.     @Override public boolean equals(Object o) {  
  102.       if (o == thisreturn true;  
  103.       if (!(o instanceof Exercise)) return false;  
  104.       Exercise<?> other = (Exercise<?>) o;  
  105.       if (!other.canEqual((Object)this)) return false;  
  106.       if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;  
  107.       if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;  
  108.       return true;  
  109.     }  
  110.       
  111.     @Override public int hashCode() {  
  112.       final int PRIME = 59;  
  113.       int result = 1;  
  114.       result = (result*PRIME) + (this.getName() == null ? 0 : this.getName().hashCode());  
  115.       result = (result*PRIME) + (this.getValue() == null ? 0 : this.getValue().hashCode());  
  116.       return result;  
  117.     }  
  118.   }  
  119. }
  与@Data相对应的@Value, 两个annotation的主要区别就是如果变量不加@NonFinal ,@Value会给所有的弄成final的。当然如果是final的话,就没有set方法了。
Java代码   收藏代码
  1. import lombok.AccessLevel;  
  2. import lombok.experimental.NonFinal;  
  3. import lombok.experimental.Value;  
  4. import lombok.experimental.Wither;  
  5. import lombok.ToString;  
  6.   
  7. @Value public class ValueExample {  
  8.   String name;  
  9.   @Wither(AccessLevel.PACKAGE) @NonFinal int age;  
  10.   double score;  
  11.   protected String[] tags;  
  12.     
  13.   @ToString(includeFieldNames=true)  
  14.   @Value(staticConstructor="of")  
  15.   public static class Exercise<T> {  
  16.     String name;  
  17.     T value;  
  18.   }  
  19. }  
使用后:
Java代码   收藏代码
  1. import java.util.Arrays;  
  2.   
  3. public final class ValueExample {  
  4.   private final String name;  
  5.   private int age;  
  6.   private final double score;  
  7.   protected final String[] tags;  
  8.     
  9.   @java.beans.ConstructorProperties({"name""age""score""tags"})  
  10.   public ValueExample(String name, int age, double score, String[] tags) {  
  11.     this.name = name;  
  12.     this.age = age;  
  13.     this.score = score;  
  14.     this.tags = tags;  
  15.   }  
  16.     
  17.   public String getName() {  
  18.     return this.name;  
  19.   }  
  20.     
  21.   public int getAge() {  
  22.     return this.age;  
  23.   }  
  24.     
  25.   public double getScore() {  
  26.     return this.score;  
  27.   }  
  28.     
  29.   public String[] getTags() {  
  30.     return this.tags;  
  31.   }  
  32.     
  33.   @java.lang.Override  
  34.   public boolean equals(Object o) {  
  35.     if (o == thisreturn true;  
  36.     if (!(o instanceof ValueExample)) return false;  
  37.     final ValueExample other = (ValueExample)o;  
  38.     final Object this$name = this.getName();  
  39.     final Object other$name = other.getName();  
  40.     if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;  
  41.     if (this.getAge() != other.getAge()) return false;  
  42.     if (Double.compare(this.getScore(), other.getScore()) != 0return false;  
  43.     if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;  
  44.     return true;  
  45.   }  
  46.     
  47.   @java.lang.Override  
  48.   public int hashCode() {  
  49.     final int PRIME = 59;  
  50.     int result = 1;  
  51.     final Object $name = this.getName();  
  52.     result = result * PRIME + ($name == null ? 0 : $name.hashCode());  
  53.     result = result * PRIME + this.getAge();  
  54.     final long $score = Double.doubleToLongBits(this.getScore());  
  55.     result = result * PRIME + (int)($score >>> 32 ^ $score);  
  56.     result = result * PRIME + Arrays.deepHashCode(this.getTags());  
  57.     return result;  
  58.   }  
  59.     
  60.   @java.lang.Override  
  61.   public String toString() {  
  62.     return "ValueExample(name=" + getName() + ", age=" + getAge() + ", score=" + getScore() + ", tags=" + Arrays.deepToString(getTags()) + ")";  
  63.   }  
  64.     
  65.   ValueExample withAge(int age) {  
  66.     return this.age == age ? this : new ValueExample(name, age, score, tags);  
  67.   }  
  68.     
  69.   public static final class Exercise<T> {  
  70.     private final String name;  
  71.     private final T value;  
  72.       
  73.     private Exercise(String name, T value) {  
  74.       this.name = name;  
  75.       this.value = value;  
  76.     }  
  77.       
  78.     public static <T> Exercise<T> of(String name, T value) {  
  79.       return new Exercise<T>(name, value);  
  80.     }  
  81.       
  82.     public String getName() {  
  83.       return this.name;  
  84.     }  
  85.       
  86.     public T getValue() {  
  87.       return this.value;  
  88.     }  
  89.       
  90.     @java.lang.Override  
  91.     public boolean equals(Object o) {  
  92.       if (o == thisreturn true;  
  93.       if (!(o instanceof ValueExample.Exercise)) return false;  
  94.       final Exercise<?> other = (Exercise<?>)o;  
  95.       final Object this$name = this.getName();  
  96.       final Object other$name = other.getName();  
  97.       if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;  
  98.       final Object this$value = this.getValue();  
  99.       final Object other$value = other.getValue();  
  100.       if (this$value == null ? other$value != null : !this$value.equals(other$value)) return false;  
  101.       return true;  
  102.     }  
  103.       
  104.     @java.lang.Override  
  105.     public int hashCode() {  
  106.       final int PRIME = 59;  
  107.       int result = 1;  
  108.       final Object $name = this.getName();  
  109.       result = result * PRIME + ($name == null ? 0 : $name.hashCode());  
  110.       final Object $value = this.getValue();  
  111.       result = result * PRIME + ($value == null ? 0 : $value.hashCode());  
  112.       return result;  
  113.     }  
  114.       
  115.     @java.lang.Override  
  116.     public String toString() {  
  117.       return "ValueExample.Exercise(name=" + getName() + ", value=" + getValue() + ")";  
  118.     }  
  119.   }  
  120. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值