com.ulisesbocchio.jasyptspringboot.util.Singleton Maven / Gradle / Ivy
The newest version!
package com.ulisesbocchio.jasyptspringboot.util;
import java.util.function.BiFunction;
import java.util.function.Function;
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.
*
* @author Sergio.U.Bocchio
* @version $Id: $Id
*/
public final class Singleton implements Supplier {
private boolean initialized = false;
private volatile Supplier instanceSupplier;
/**
* from.
*
* @param original a {@link java.util.function.Supplier} object
* @param a R class
* @return a {@link com.ulisesbocchio.jasyptspringboot.util.Singleton} object
*/
public static Singleton from(final Supplier original) {
return new Singleton<>(original);
}
/**
* from.
*
* @param original a {@link java.util.function.Function} object
* @param arg0 a T object
* @param a T class
* @param a R class
* @return a {@link com.ulisesbocchio.jasyptspringboot.util.Singleton} object
*/
public static Singleton from(final Function original, T arg0) {
return fromLazy(original, () -> arg0);
}
/**
* from.
*
* @param original a {@link java.util.function.BiFunction} object
* @param arg0 a T object
* @param arg1 a U object
* @param a T class
* @param a U class
* @param a R class
* @return a {@link com.ulisesbocchio.jasyptspringboot.util.Singleton} object
*/
public static Singleton from(final BiFunction original, T arg0, U arg1) {
return fromLazy(original, () -> arg0, () -> arg1);
}
/**
* fromLazy.
*
* @param original a {@link java.util.function.Function} object
* @param arg0Supplier a {@link java.util.function.Supplier} object
* @param a T class
* @param a R class
* @return a {@link com.ulisesbocchio.jasyptspringboot.util.Singleton} object
*/
public static Singleton fromLazy(final Function original, Supplier arg0Supplier) {
return from(() -> original.apply(arg0Supplier.get()));
}
/**
* fromLazy.
*
* @param original a {@link java.util.function.BiFunction} object
* @param arg0Supplier a {@link java.util.function.Supplier} object
* @param arg1Supplier a {@link java.util.function.Supplier} object
* @param a T class
* @param a U class
* @param a R class
* @return a {@link com.ulisesbocchio.jasyptspringboot.util.Singleton} object
*/
public static Singleton fromLazy(final BiFunction original, Supplier arg0Supplier, Supplier arg1Supplier) {
return from(() -> original.apply(arg0Supplier.get(), arg1Supplier.get()));
}
/**
* Constructor for Singleton.
*
* @param original a {@link java.util.function.Supplier} object
*/
public Singleton(final Supplier original) {
instanceSupplier = () -> {
synchronized (original) {
if (!initialized) {
final R singletonInstance = original.get();
instanceSupplier = () -> singletonInstance;
initialized = true;
}
return instanceSupplier.get();
}
};
}
/** {@inheritDoc} */
@Override
public R get() {
return instanceSupplier.get();
}
}