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

net.jbock.common.Suppliers Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
package net.jbock.common;

import java.util.function.Supplier;

public final class Suppliers {

    public static  Supplier memoize(Supplier delegate) {
        return new Cache<>(delegate);
    }

    private static final class Cache implements Supplier {

        boolean initialized;
        T value;

        final Supplier delegate;

        Cache(Supplier delegate) {
            this.delegate = delegate;
        }

        @Override
        public T get() {
            if (!initialized) {
                initialized = true;
                value = delegate.get();
            }
            return value;
        }
    }

    private Suppliers() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy