import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.util.concurrent.TimeUnit; public class Cooldown { private final Cache cache; public Cooldown(int timeout, TimeUnit unit) { cache = CacheBuilder.newBuilder() .expireAfterWrite(timeout, unit) .build(); } public void insert(T key) { cache.put(key, System.currentTimeMillis()); } public boolean isCooled(T key) { return cache.getIfPresent(key) == null; } public void clear() { cache.invalidateAll(); } public void clear(T key) { cache.invalidate(key); } public long getTimeLeft(T key) { Long time = cache.getIfPresent(key); if (time == null) return 0; return time - System.currentTimeMillis(); } }