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

reactivefeign.utils.LazyInitialized Maven / Gradle / Ivy

package reactivefeign.utils;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

public class LazyInitialized {

    private final AtomicReference cachedValue = new AtomicReference<>();
    private final Supplier supplier;

    public LazyInitialized(Supplier supplier) {
        this.supplier = supplier;
    }

    public V get(){
        V result = cachedValue.get();
        if (result == null) {
            result = supplier.get();
            if (!cachedValue.compareAndSet(null, result)) {
                return cachedValue.get();
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy