org.rx.util.Lazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.util;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.rx.util.function.Func;
@RequiredArgsConstructor
public final class Lazy {
private Func func;
private volatile T value;
public boolean isValueCreated() {
return value != null;
}
@SneakyThrows
public T getValue() {
if (value == null) {
synchronized (this) {
if (value == null) {
value = func.invoke();
func = null;
}
}
}
return value;
}
public Lazy(@NonNull Func func) {
this.func = func;
}
}