src/org/sonews/util/TimeoutMap.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
     1.1 --- a/src/org/sonews/util/TimeoutMap.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/util/TimeoutMap.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -31,115 +31,99 @@
     1.4   * @author Christian Lins
     1.5   * @since sonews/0.5.0
     1.6   */
     1.7 -public class TimeoutMap<K,V> extends ConcurrentHashMap<K, V>
     1.8 +public class TimeoutMap<K, V> extends ConcurrentHashMap<K, V>
     1.9  {
    1.10 -  
    1.11 -  private static final long serialVersionUID = 453453467700345L;
    1.12  
    1.13 -  private int                    timeout     = 60000; // 60 sec
    1.14 -  private transient Map<K, Long> timeoutMap  = new HashMap<K, Long>();
    1.15 -  
    1.16 -  /**
    1.17 -   * Constructor.
    1.18 -   * @param timeout Timeout in milliseconds
    1.19 -   */
    1.20 -  public TimeoutMap(final int timeout)
    1.21 -  {
    1.22 -    this.timeout = timeout;
    1.23 -  }
    1.24 -  
    1.25 -  /**
    1.26 -   * Uses default timeout (60 sec).
    1.27 -   */
    1.28 -  public TimeoutMap()
    1.29 -  {
    1.30 -  }
    1.31 -  
    1.32 -  /**
    1.33 -   * 
    1.34 -   * @param key
    1.35 -   * @return true if key is still valid.
    1.36 -   */
    1.37 -  protected boolean checkTimeOut(Object key)
    1.38 -  {
    1.39 -    synchronized(this.timeoutMap)
    1.40 -    {
    1.41 -      if(this.timeoutMap.containsKey(key))
    1.42 -      {
    1.43 -        long keytime = this.timeoutMap.get(key);
    1.44 -        if((System.currentTimeMillis() - keytime) < this.timeout)
    1.45 -        {
    1.46 -          return true;
    1.47 -        }
    1.48 -        else
    1.49 -        {
    1.50 -          remove(key);
    1.51 -          return false;
    1.52 -        }
    1.53 -      }
    1.54 -      else
    1.55 -      {
    1.56 -        return false;
    1.57 -      }
    1.58 -    }
    1.59 -  }
    1.60 -  
    1.61 -  @Override
    1.62 -  public boolean containsKey(Object key)
    1.63 -  {
    1.64 -    return checkTimeOut(key);
    1.65 -  }
    1.66 +	private static final long serialVersionUID = 453453467700345L;
    1.67 +	private int timeout = 60000; // 60 sec
    1.68 +	private transient Map<K, Long> timeoutMap = new HashMap<K, Long>();
    1.69  
    1.70 -  @Override
    1.71 -  public synchronized V get(Object key)
    1.72 -  {
    1.73 -    if(checkTimeOut(key))
    1.74 -    {
    1.75 -      return super.get(key);
    1.76 -    }
    1.77 -    else
    1.78 -    {
    1.79 -      return null;
    1.80 -    }
    1.81 -  }
    1.82 +	/**
    1.83 +	 * Constructor.
    1.84 +	 * @param timeout Timeout in milliseconds
    1.85 +	 */
    1.86 +	public TimeoutMap(final int timeout)
    1.87 +	{
    1.88 +		this.timeout = timeout;
    1.89 +	}
    1.90  
    1.91 -  @Override
    1.92 -  public V put(K key, V value)
    1.93 -  {
    1.94 -    synchronized(this.timeoutMap)
    1.95 -    {
    1.96 -      removeStaleKeys();
    1.97 -      this.timeoutMap.put(key, System.currentTimeMillis());
    1.98 -      return super.put(key, value);
    1.99 -    }
   1.100 -  }
   1.101 +	/**
   1.102 +	 * Uses default timeout (60 sec).
   1.103 +	 */
   1.104 +	public TimeoutMap()
   1.105 +	{
   1.106 +	}
   1.107  
   1.108 -  /**
   1.109 -   * @param arg0
   1.110 -   * @return
   1.111 -   */
   1.112 -  @Override
   1.113 -  public V remove(Object arg0)
   1.114 -  {
   1.115 -    synchronized(this.timeoutMap)
   1.116 -    {
   1.117 -      this.timeoutMap.remove(arg0);
   1.118 -      V val = super.remove(arg0);
   1.119 -      return val;
   1.120 -    }
   1.121 -  }
   1.122 +	/**
   1.123 +	 *
   1.124 +	 * @param key
   1.125 +	 * @return true if key is still valid.
   1.126 +	 */
   1.127 +	protected boolean checkTimeOut(Object key)
   1.128 +	{
   1.129 +		synchronized (this.timeoutMap) {
   1.130 +			if (this.timeoutMap.containsKey(key)) {
   1.131 +				long keytime = this.timeoutMap.get(key);
   1.132 +				if ((System.currentTimeMillis() - keytime) < this.timeout) {
   1.133 +					return true;
   1.134 +				} else {
   1.135 +					remove(key);
   1.136 +					return false;
   1.137 +				}
   1.138 +			} else {
   1.139 +				return false;
   1.140 +			}
   1.141 +		}
   1.142 +	}
   1.143  
   1.144 -  protected void removeStaleKeys()
   1.145 -  {
   1.146 -    synchronized(this.timeoutMap)
   1.147 -    {
   1.148 -      Set<Object> keySet = new HashSet<Object>(this.timeoutMap.keySet());
   1.149 -      for(Object key : keySet)
   1.150 -      {
   1.151 -        // The key/value is removed by the checkTimeOut() method if true
   1.152 -        checkTimeOut(key);
   1.153 -      }
   1.154 -    }
   1.155 -  }
   1.156 -  
   1.157 +	@Override
   1.158 +	public boolean containsKey(Object key)
   1.159 +	{
   1.160 +		return checkTimeOut(key);
   1.161 +	}
   1.162 +
   1.163 +	@Override
   1.164 +	public synchronized V get(Object key)
   1.165 +	{
   1.166 +		if (checkTimeOut(key)) {
   1.167 +			return super.get(key);
   1.168 +		} else {
   1.169 +			return null;
   1.170 +		}
   1.171 +	}
   1.172 +
   1.173 +	@Override
   1.174 +	public V put(K key, V value)
   1.175 +	{
   1.176 +		synchronized (this.timeoutMap) {
   1.177 +			removeStaleKeys();
   1.178 +			this.timeoutMap.put(key, System.currentTimeMillis());
   1.179 +			return super.put(key, value);
   1.180 +		}
   1.181 +	}
   1.182 +
   1.183 +	/**
   1.184 +	 * @param arg0
   1.185 +	 * @return
   1.186 +	 */
   1.187 +	@Override
   1.188 +	public V remove(Object arg0)
   1.189 +	{
   1.190 +		synchronized (this.timeoutMap) {
   1.191 +			this.timeoutMap.remove(arg0);
   1.192 +			V val = super.remove(arg0);
   1.193 +			return val;
   1.194 +		}
   1.195 +	}
   1.196 +
   1.197 +	protected void removeStaleKeys()
   1.198 +	{
   1.199 +		synchronized (this.timeoutMap) {
   1.200 +			Set<Object> keySet = new HashSet<Object>(this.timeoutMap.keySet());
   1.201 +			for (Object key : keySet) {
   1.202 +				// The key/value is removed by the checkTimeOut() method if true
   1.203 +				checkTimeOut(key);
   1.204 +			}
   1.205 +		}
   1.206 +	}
   1.207  }