package dk.thoerup.traininfoservice.banedk; import java.util.concurrent.ConcurrentHashMap; import java.util.Collection; import java.util.Map; import java.util.Set; import com.sun.xml.rpc.processor.schema.UnimplementedFeatureException; public class TimeoutMap implements Map{ class CacheItem { public CacheItem(T v) { value = v; lastupdate = System.currentTimeMillis(); } public long lastupdate; public T value; } private ConcurrentHashMap> cache = new ConcurrentHashMap>(); private long timeout; public TimeoutMap(int timeout) { this.timeout = timeout; } @Override public void clear() { cache.clear(); } @Override public boolean containsKey(Object arg0) { CacheItem item = cache.get(arg0); return (item != null); } @Override public boolean containsValue(Object arg0) { //TODO someday implement this throw new UnsupportedOperationException(); } @Override public Set> entrySet() { //TODO someday implement this throw new UnsupportedOperationException(); } @Override public boolean isEmpty() { return cache.isEmpty(); } @Override public Set keySet() { return cache.keySet(); } @Override public void putAll(Map arg0) { //TODO someday implement this throw new UnsupportedOperationException(); } @Override public V remove(Object arg0) { return cache.remove(arg0).value; } @Override public int size() { return cache.size(); } @Override public Collection values() { //TODO someday implement this throw new UnsupportedOperationException(); } @Override public V get(Object key) { long now = System.currentTimeMillis(); CacheItem item = cache.get(key); if (item != null) { if ( (item.lastupdate+timeout) < now) { //item too old return null; } else { return item.value; //item still good } } else { return null; // no item found } } @Override public V put(K key, V value) { CacheItem item= new CacheItem(value); return cache.put(key, item).value; } }