com.ulisesbocchio.jasyptspringboot.util.Singleton Maven / Gradle / Ivy
package com.ulisesbocchio.jasyptspringboot.util;
import java.util.function.Supplier;
/**
* Singleton initializer class that uses an internal supplier to supply the singleton instance. The supplier
* originally checks whether the instanceSupplier
* has been initialized or not, but after initialization the instance supplier is changed to avoid extra logic
* execution.
*/
public final class Singleton implements Supplier {
private boolean initialized = false;
private volatile Supplier instanceSupplier;
public Singleton(final Supplier original) {
instanceSupplier = () -> {
synchronized (original) {
if (!initialized) {
final R singletonInstance = original.get();
instanceSupplier = () -> singletonInstance;
initialized = true;
}
return instanceSupplier.get();
}
};
}
@Override
public R get() {
return instanceSupplier.get();
}
}