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

graphql.util.IntraThreadMemoizedSupplier Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.util;

import graphql.Internal;

import java.util.function.Supplier;

import static graphql.Assert.assertNotNull;

/**
 * This memoizing supplier does NOT use synchronised double locking to set its value
 * so on multiple threads it MAY call the delegate again to get a value.
 *
 * @param  for two
 */
@Internal
class IntraThreadMemoizedSupplier implements Supplier {
    private final static Object SENTINEL = new Object() {
    };

    @SuppressWarnings("unchecked")
    private T value = (T) SENTINEL;
    private final Supplier delegate;

    IntraThreadMemoizedSupplier(Supplier delegate) {
        this.delegate = assertNotNull(delegate);
    }

    @Override
    public T get() {
        T t = value;
        if (t == SENTINEL) {
            t = delegate.get();
            value = t;
        }
        return t;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy