de.team33.patterns.lazy.e1.Mutual Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lazy-01 Show documentation
Show all versions of lazy-01 Show documentation
Provides generic envelope classes that support lazy initialization
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();
}
};
}
}