reactivefeign.utils.LazyInitialized Maven / Gradle / Ivy
package reactivefeign.utils;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
public class LazyInitialized {
private final AtomicReference cachedValue = new AtomicReference<>();
private final Supplier supplier;
public LazyInitialized(Supplier supplier) {
this.supplier = supplier;
}
public V get(){
V result = cachedValue.get();
if (result == null) {
result = supplier.get();
if (!cachedValue.compareAndSet(null, result)) {
return cachedValue.get();
}
}
return result;
}
}