com.wavefront.common.LazySupplier Maven / Gradle / Ivy
package com.wavefront.common;
import java.util.function.Supplier;
/**
* Caching wrapper with lazy init for a {@code Supplier}
*
* @author [email protected]
*/
public abstract class LazySupplier {
/**
* Lazy-initialize a {@code Supplier}
*
* @param supplier {@code Supplier} to lazy-initialize
* @return lazy wrapped supplier
*/
public static Supplier of(Supplier supplier) {
return new Supplier() {
private volatile T value = null;
@Override
public T get() {
if (value == null) {
synchronized (this) {
if (value == null) {
value = supplier.get();
}
}
}
return value;
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy