package com.terraforged.core.util.concurrent.cache; import com.terraforged.core.util.concurrent.ThreadPool; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.Future; public class CacheEntry implements ExpiringEntry { private volatile long timestamp; private final Future task; public CacheEntry(Future task) { this.task = task; this.timestamp = System.currentTimeMillis(); } @Override public long getTimestamp() { return timestamp; } public boolean isDone() { return task.isDone(); } public T get() { if (task instanceof ForkJoinTask) { return ((ForkJoinTask) task).join(); } try { return task.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } } public static CacheEntry supply(Future future) { return new CacheEntry<>(future); } public static CacheEntry supplyAsync(Callable callable, ThreadPool executor) { return new CacheEntry<>(executor.submit(callable)); } }