--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutCache.java 2009/10/02 15:06:08 387 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutMap.java 2010/06/07 12:13:10 804 @@ -1,9 +1,14 @@ package dk.thoerup.traininfoservice.banedk; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -public class TimeoutCache { + +public class TimeoutMap implements Map{ class CacheItem { @@ -19,19 +24,76 @@ private ConcurrentHashMap> cache = new ConcurrentHashMap>(); private long timeout; - public TimeoutCache(int timeout) { + public TimeoutMap(int timeout) { this.timeout = timeout; } - - public void put(K k, V v) { - CacheItem item= new CacheItem(v); - cache.put(k, item); + + + @Override + public void clear() { + cache.clear(); + } - - public V get(K k) { + + @Override + public boolean containsKey(Object key) { + return cache.containsKey(key); + } + + @Override + public boolean containsValue(Object arg0) { + return values().contains(arg0); + } + + @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) { + for(K key : arg0.keySet()) { + this.put(key, arg0.get(key) ); + } + } + + @Override + public V remove(Object arg0) { + return cache.remove(arg0).value; + } + + @Override + public int size() { + return cache.size(); + } + + @Override + public Collection values() { + ArrayList values = new ArrayList(); + for (CacheItem item : cache.values()) { + values.add( item.value ); + } + + return values; + + } + + @Override + public V get(Object key) { long now = System.currentTimeMillis(); - CacheItem item = cache.get(k); + CacheItem item = cache.get(key); if (item != null) { @@ -43,7 +105,18 @@ } else { return null; // no item found } + } + + @Override + public V put(K key, V value) { + CacheItem item= new CacheItem(value); + + item = cache.put(key, item); + if (item != null) + return item.value; + else + return null; } }