diff -r ed84c8bdd87b -r 8df94bfd3e2f src/org/sonews/util/TimeoutMap.java --- a/src/org/sonews/util/TimeoutMap.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/util/TimeoutMap.java Sun Sep 11 17:01:19 2011 +0200 @@ -31,115 +31,99 @@ * @author Christian Lins * @since sonews/0.5.0 */ -public class TimeoutMap extends ConcurrentHashMap +public class TimeoutMap extends ConcurrentHashMap { - - private static final long serialVersionUID = 453453467700345L; - private int timeout = 60000; // 60 sec - private transient Map timeoutMap = new HashMap(); - - /** - * Constructor. - * @param timeout Timeout in milliseconds - */ - public TimeoutMap(final int timeout) - { - this.timeout = timeout; - } - - /** - * Uses default timeout (60 sec). - */ - public TimeoutMap() - { - } - - /** - * - * @param key - * @return true if key is still valid. - */ - protected boolean checkTimeOut(Object key) - { - synchronized(this.timeoutMap) - { - if(this.timeoutMap.containsKey(key)) - { - long keytime = this.timeoutMap.get(key); - if((System.currentTimeMillis() - keytime) < this.timeout) - { - return true; - } - else - { - remove(key); - return false; - } - } - else - { - return false; - } - } - } - - @Override - public boolean containsKey(Object key) - { - return checkTimeOut(key); - } + private static final long serialVersionUID = 453453467700345L; + private int timeout = 60000; // 60 sec + private transient Map timeoutMap = new HashMap(); - @Override - public synchronized V get(Object key) - { - if(checkTimeOut(key)) - { - return super.get(key); - } - else - { - return null; - } - } + /** + * Constructor. + * @param timeout Timeout in milliseconds + */ + public TimeoutMap(final int timeout) + { + this.timeout = timeout; + } - @Override - public V put(K key, V value) - { - synchronized(this.timeoutMap) - { - removeStaleKeys(); - this.timeoutMap.put(key, System.currentTimeMillis()); - return super.put(key, value); - } - } + /** + * Uses default timeout (60 sec). + */ + public TimeoutMap() + { + } - /** - * @param arg0 - * @return - */ - @Override - public V remove(Object arg0) - { - synchronized(this.timeoutMap) - { - this.timeoutMap.remove(arg0); - V val = super.remove(arg0); - return val; - } - } + /** + * + * @param key + * @return true if key is still valid. + */ + protected boolean checkTimeOut(Object key) + { + synchronized (this.timeoutMap) { + if (this.timeoutMap.containsKey(key)) { + long keytime = this.timeoutMap.get(key); + if ((System.currentTimeMillis() - keytime) < this.timeout) { + return true; + } else { + remove(key); + return false; + } + } else { + return false; + } + } + } - protected void removeStaleKeys() - { - synchronized(this.timeoutMap) - { - Set keySet = new HashSet(this.timeoutMap.keySet()); - for(Object key : keySet) - { - // The key/value is removed by the checkTimeOut() method if true - checkTimeOut(key); - } - } - } - + @Override + public boolean containsKey(Object key) + { + return checkTimeOut(key); + } + + @Override + public synchronized V get(Object key) + { + if (checkTimeOut(key)) { + return super.get(key); + } else { + return null; + } + } + + @Override + public V put(K key, V value) + { + synchronized (this.timeoutMap) { + removeStaleKeys(); + this.timeoutMap.put(key, System.currentTimeMillis()); + return super.put(key, value); + } + } + + /** + * @param arg0 + * @return + */ + @Override + public V remove(Object arg0) + { + synchronized (this.timeoutMap) { + this.timeoutMap.remove(arg0); + V val = super.remove(arg0); + return val; + } + } + + protected void removeStaleKeys() + { + synchronized (this.timeoutMap) { + Set keySet = new HashSet(this.timeoutMap.keySet()); + for (Object key : keySet) { + // The key/value is removed by the checkTimeOut() method if true + checkTimeOut(key); + } + } + } }