@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; }
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 = (resultPRIME) + (this.getName() == null ? 43 : this.getName().hashCode()); result = (resultPRIME) + this.getAge(); result = (resultPRIME) + (int)(temp1 ^ (temp1 >>> 32)); result = (resultPRIME) + Arrays.deepHashCode(this.getTags()); return result; }
public static class Exercise<T> { private final String name; private final T value;
public class NonNullExample extends Something { private String name;
public NonNullExample(@NonNull Person person) { super(“Hello”); this.name = person.getName(); } }
不使用Lombok:
import lombok.NonNull;
public class NonNullExample extends Something { private String name;
public NonNullExample(@NonNull Person person) { super(“Hello”); if (person == null) { throw new NullPointerException(“person”); } this.name = person.getName(); } }
2.4 @Cleanup
该注解能帮助我们自动调用close()方法,很大的简化了代码。
示例如下:
import lombok.Cleanup;
import java.io.*;
public class CleanupExample { public static void main(String[] args) throws IOException { @Cleanup InputStream in = new FileInputStream(args[0]); @Cleanup OutputStream out = new FileOutputStream(args[1]); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } }
如不使用Lombok,则需如下:
import java.io.*;
public class CleanupExample { public static void main(String[] args) throws IOException { InputStream in = new FileInputStream(args[0]); try { OutputStream out = new FileOutputStream(args[1]); try { byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } finally { if (out != null) { out.close(); } } } finally { if (in != null) { in.close(); } } } }
@ToString(exclude=“id”) public class ToStringExample { private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square(5, 10); private String[] tags; private int id;
public String getName() { return this.getName(); }
@ToString(callSuper=true, includeFieldNames=true) public static class Square extends Shape { private final int width, height;
public class ToStringExample { private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square(5, 10); private String[] tags; private int id;
public String getName() { return this.getName(); }
public static class Square extends Shape { private final int width, height;
@RequiredArgsConstructor(staticName = “of”) @AllArgsConstructor(access = AccessLevel.PROTECTED) public class ConstructorExample<T> { private int x, y; @NonNull private T description;
@NoArgsConstructor public static class NoArgsExample { @NonNull private String field; } }
不使用Lombok的示例如下:
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
private ConstructorExample(T description) { if (description == null) throw new NullPointerException(“description”); this.description = description; }
public static <T> ConstructorExample<T> of(T description) { return new ConstructorExample<T>(description); }
@java.beans.ConstructorProperties({“x”, “y”, “description”}) protected ConstructorExample(int x, int y, T description) { if (description == null) throw new NullPointerException(“description”); this.x = x; this.y = y; this.description = description; }
public static class NoArgsExample { @NonNull private String field;
同步首发:http://www.yuanrengu.com/index.php/20180324.html1 Lombok背景介绍官方介绍如下:Project Lombok makes java a spicier language by adding 'handlers' that know how to build and compile simple, boilerplat...