use the java functional class instead of making our own
This commit is contained in:
parent
8463f45fd8
commit
f809800905
1 changed files with 8 additions and 11 deletions
|
@ -1,24 +1,21 @@
|
||||||
package lol.pyr.znpcsplus.util;
|
package lol.pyr.znpcsplus.util;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class LazyLoader <T> {
|
public class LazyLoader <T> {
|
||||||
private final ObjectProvider<T> provider;
|
private final Supplier<T> supplier;
|
||||||
private T value;
|
private T value;
|
||||||
|
|
||||||
private LazyLoader(ObjectProvider<T> provider) {
|
private LazyLoader(Supplier<T> supplier) {
|
||||||
this.provider = provider;
|
this.supplier = supplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get() {
|
public T get() {
|
||||||
if (value == null) value = provider.provide();
|
if (value == null) value = supplier.get();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> LazyLoader<T> of(ObjectProvider<T> provider) {
|
public static <T> LazyLoader<T> of(Supplier<T> supplier) {
|
||||||
return new LazyLoader<>(provider);
|
return new LazyLoader<>(supplier);
|
||||||
}
|
|
||||||
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface ObjectProvider<T> {
|
|
||||||
T provide();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue