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

de.team33.patterns.lazy.e1.Mutual Maven / Gradle / Ivy

There is a newer version: 1.13.0
Show newest version
package de.team33.patterns.lazy.e1;

import de.team33.patterns.exceptional.e1.XSupplier;

@SuppressWarnings("BoundedWildcard")
class Mutual {

    @SuppressWarnings("PackageVisibleField")
    volatile XSupplier backing;

    Mutual(final XSupplier initial) {
        this.backing = initiation(initial);
    }

    private XSupplier initiation(final XSupplier initial) {
        return new XSupplier() {
            private final Object monitor = new Object();

            @SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
            @Override
            public final T get() throws X {
                synchronized (monitor) {
                    if (backing == this) {
                        final T result = initial.get();
                        backing = () -> result;
                    }
                }
                return backing.get();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy