类属性
Data Model | 数据模型
Getter & Setter
源代码:
public class GetterSetterExample {
@Getter
@Setter
private int age = 10;
@Setter(AccessLevel.PROTECTED)
private String name;
@Override
public String toString() {
return String.format("%s (age: %d)", name, age);
}
}
编译之后的代码:
public class GetterSetterExample {
...
public int getAge() {
return age;
}
...
public void setAge(int age) {
this.age = age;
}
...
protected void setName(String name) {
this.name = name;
}
}
Accessors
@Accessors(fluent = true)
public class AccessorsExample {
@Getter
@Setter
private int age = 10;
}
class PrefixExample {
@Accessors(prefix = "f")
@Getter
private String fName = "Hello, World!";
}
如果设置了 chain 为 true,则仅在 Setter 方法中返回对象,仍然保留标准的 Getter/Setter 风格。
public class AccessorsExample {
private int age = 10;
public int age() {
return this.age;
}
public AccessorsExample age(final int age) {
this.age = age;
return this;
}
}
class PrefixExample {
private String fName = "Hello, World!";
public String getName() {
return this.fName;
}
}
Lazy Getter
源代码:
public class GetterLazyExample {
@Getter(lazy=true) private final double[] cached = expensive();
private double[] expensive() {
...
}
}
编译之后:
public class GetterLazyExample {
private final java.util.concurrent.AtomicReference<java.lang.Object> cached = new java.util.concurrent.AtomicReference<java.lang.Object>();
public double[] getCached() {
java.lang.Object value = this.cached.get();
if (value == null) {
synchronized(this.cached) {
value = this.cached.get();
if (value == null) {
final double[] actualValue = expensive();
value = actualValue == null ? this.cached : actualValue;
this.cached.set(value);
}
}
}
return (double[])(value == this.cached ? null : value);
}
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
}
}
Data
源代码:
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
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;
}
}
编译后:
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;
}
}
}