你可以让Lombok生成一个getter方法,它会在第一次被调用的时候计算这个值,以后都从缓存中进行获取。如果这个计算过程消耗大量的CPU或者内存资源,那么这个特性就变的非常有价值了。要使用此功能,需要创建一个私有final变量,使用表达式(运算成本较高)对其进行初始化,并使用@Getter(lazy = true)
注释进行标记。这个字段将在生成字段中隐藏,而且其初始化的表达式(运算成本高)只会在getter方法首次调用的被执行。这个操作不存在魔法数据,及时表达式运算结果为Null也会被缓存。而且运算表达式不需要是线程安全的,Lombok会对齐进行加锁。
如果初始化表达式很复杂或包含泛型,我们建议将代码移动到私有(如果可能声明为静态)方法,并调用它。
import lombok.Getter;
public class GetterLazyExample {
@Getter(lazy=true) private final double[] cached = expensive();
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
}
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;
}
}
lombok.getter.lazy.flagUsage = [warning | error] (default: not set)
默认不进行设置,配置后Lombok将会把使用@Getter(lazy=true)
的类标记为错误或者警告。
由于Lombok会将该字段类型转变为AtomicReference
,所以在使用该值的时候务必使用getter方法而不要直接调用该字段。也不要试图直接访问AtomicReference
对象,如果该表达式已经被计算,那么将会指向这个值;但是如果没有计算则会指向null。但是这个特性在未来的版本可能发生改变,所以还是使用gettger安全。
即使您使用doNotUseGetters = true
,其他Lombok注释(如@ToString
)也始终会调用getter。
全部评论