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

htsjdk.samtools.util.Lazy Maven / Gradle / Ivy

There is a newer version: 4.1.3
Show newest version
package htsjdk.samtools.util;

import java.util.function.Supplier;

/**
 * Simple utility for building an on-demand (lazy) object-initializer.
 * 
 * Works by accepting an initializer describing how to build the on-demand object, which is only called once and only after the first
 * invocation of {@link #get()} (or it may not be called at all).
 * 
 * @author mccowan
 */
public class Lazy {
    private final Supplier initializer;
    private boolean isInitialized = false;
    private T instance;

    public Lazy(final Supplier initializer) {
        this.initializer = initializer;
    }

    /** Returns the instance associated with this {@link Lazy}, initializing it if necessary. */
    public synchronized T get() {
        if (!isInitialized) {
            this.instance = initializer.get();
            isInitialized = true;
        }
        return instance;
    }

    /** Describes how to build the instance of the lazy object.
     * @deprecated since 1/2017 use a {@link Supplier} instead
     * */
    @FunctionalInterface
    @Deprecated
    public interface LazyInitializer extends Supplier {
        /** Returns the desired object instance. */
        T make();

        @Override
        default T get(){
            return make();
        }
    }

    public boolean isInitialized() {
        return isInitialized;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy