me.qoomon.gitversioning.commons.Lazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-git-versioning-extension Show documentation
Show all versions of maven-git-versioning-extension Show documentation
Maven Git Versioning Extension
package me.qoomon.gitversioning.commons;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
public final class Lazy implements Supplier {
private volatile Callable initializer;
private T value;
public Lazy(Callable initializer) {
this.initializer = requireNonNull(initializer);
}
public T get() {
if (initializer != null) {
synchronized (this) {
if (initializer != null) {
try {
value = initializer.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
initializer = null;
}
}
}
return value;
}
public static Lazy of(T value) {
return new Lazy<>(() -> value);
}
public static Lazy by(Callable supplier) {
return new Lazy<>(supplier);
}
public static V get(Lazy value) {
if (value != null) {
return value.get();
}
return null;
}
}