com.slimgears.util.stream.ThreadLazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stream-utils Show documentation
Show all versions of stream-utils Show documentation
General purpose utils / module: stream-utils
package com.slimgears.util.stream;
import java.util.function.Supplier;
public class ThreadLazy extends ThreadLocal implements Supplier {
private final Supplier supplier;
private ThreadLazy(Supplier supplier) {
this.supplier = supplier;
}
@Override
public T get() {
T instance = super.get();
if (instance == null) {
instance = supplier.get();
set(instance);
}
return instance;
}
public static ThreadLazy of(Supplier supplier) {
return new ThreadLazy<>(supplier);
}
}