package shcm.shsupercm.fabric.citresewn.cit; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.function.Function; public abstract class CITCache { public long lastCachedStamp = 0; public static class Single extends CITCache { public WeakReference> cit = null; public final Function> realtime; public Single(Function> realtime) { this.realtime = realtime; } public WeakReference> get(CITContext context) { if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { this.cit = new WeakReference<>(this.realtime.apply(context)); this.lastCachedStamp = System.currentTimeMillis(); } return this.cit; } } public static class MultiList extends CITCache { public List>> cit = null; public final Function>> realtime; public MultiList(Function>> realtime) { this.realtime = realtime; } public List>> get(CITContext context) { if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { this.cit = new ArrayList<>(); for (CIT realtimeCIT : this.realtime.apply(context)) this.cit.add(new WeakReference<>(realtimeCIT)); this.lastCachedStamp = System.currentTimeMillis(); } return cit; } } }