de.team33.patterns.lazy.narvi.Mutual Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lazy-narvi Show documentation
Show all versions of lazy-narvi Show documentation
Provides generic envelope classes that support lazy initialization
The newest version!
package de.team33.patterns.lazy.narvi;
import de.team33.patterns.exceptional.dione.XSupplier;
class Mutual {
private volatile XSupplier backing;
Mutual(final XSupplier initial) {
this.backing = provident(initial);
}
private XSupplier definite(final T value) {
return () -> value;
}
private XSupplier provident(final XSupplier initial) {
return new XSupplier() {
@Override
public synchronized T get() throws X {
if (backing == this) {
backing = definite(initial.get());
}
return backing.get();
}
};
}
T get() throws X {
return backing.get();
}
}