io.rouz.flo.Singleton Maven / Gradle / Ivy
package io.rouz.flo;
import java.util.Objects;
/**
* A singleton supplier decorator.
*
* Ensures that the {@link #get()} method of the wrapped {@link Fn} only is called once.
*/
class Singleton implements Fn {
private final Fn supplier;
private volatile T value;
private Singleton(Fn supplier) {
this.supplier = Objects.requireNonNull(supplier);
}
static Fn create(Fn fn) {
return new Singleton<>(fn);
}
@Override
public T get() {
if (value == null) {
synchronized (this) {
if (value == null) {
value = supplier.get();
}
}
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy