edu.stanford.nlp.util.Lazy Maven / Gradle / Ivy
package edu.stanford.nlp.util;
import java.util.function.Supplier;
/**
* An instantiation of a lazy object.
*
* @author Gabor Angeli
*/
public abstract class Lazy {
private E implOrNull = null;
public synchronized E get() {
if (implOrNull == null) {
implOrNull = compute();
}
return implOrNull;
}
protected abstract E compute();
public E getIfDefined() {
return implOrNull;
}
public static Lazy from(final E definedElement) {
return new Lazy() {
@Override
protected E compute() {
return definedElement;
}
};
}
public static Lazy of(Supplier fn) {
return new Lazy() {
@Override
protected E compute() {
return fn.get();
}
};
}
}