All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ulisesbocchio.jasyptspringboot.util.Singleton Maven / Gradle / Ivy

There is a newer version: 3.0.5
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy