package com.terraforged.core.util.concurrent.cache; import java.util.concurrent.Executor; import java.util.function.Supplier; public class CacheEntry implements Runnable, ExpiringEntry { private volatile T result; private volatile long timestamp; private final Supplier supplier; private CacheEntry(Supplier supplier) { this.supplier = supplier; this.timestamp = System.currentTimeMillis(); } @Override public long getTimestamp() { return timestamp; } @Override public void run() { this.result = supplier.get(); this.timestamp = System.currentTimeMillis(); } public boolean isDone() { return result != null; } public T get() { T value = result; if (value == null) { value = getNow(); } return value; } private T getNow() { T result = supplier.get(); this.result = result; return result; } public static CacheEntry supplyAsync(Supplier supplier, Executor executor) { CacheEntry entry = new CacheEntry<>(supplier); executor.execute(entry); return entry; } }