org/sonews/util/TimeoutMap.java
changeset 33 f9bf183447d1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/util/TimeoutMap.java	Sun Aug 29 17:03:21 2010 +0200
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.util;
    1.23 +
    1.24 +import java.util.HashMap;
    1.25 +import java.util.HashSet;
    1.26 +import java.util.Map;
    1.27 +import java.util.Set;
    1.28 +import java.util.concurrent.ConcurrentHashMap;
    1.29 +
    1.30 +/**
    1.31 + * Implementation of a Map that will loose its stored values after a 
    1.32 + * configurable amount of time.
    1.33 + * This class may be used to cache config values for example.
    1.34 + * @author Christian Lins
    1.35 + * @since sonews/0.5.0
    1.36 + */
    1.37 +public class TimeoutMap<K,V> extends ConcurrentHashMap<K, V>
    1.38 +{
    1.39 +  
    1.40 +  private static final long serialVersionUID = 453453467700345L;
    1.41 +
    1.42 +  private int                    timeout     = 60000; // 60 sec
    1.43 +  private transient Map<K, Long> timeoutMap  = new HashMap<K, Long>();
    1.44 +  
    1.45 +  /**
    1.46 +   * Constructor.
    1.47 +   * @param timeout Timeout in milliseconds
    1.48 +   */
    1.49 +  public TimeoutMap(final int timeout)
    1.50 +  {
    1.51 +    this.timeout = timeout;
    1.52 +  }
    1.53 +  
    1.54 +  /**
    1.55 +   * Uses default timeout (60 sec).
    1.56 +   */
    1.57 +  public TimeoutMap()
    1.58 +  {
    1.59 +  }
    1.60 +  
    1.61 +  /**
    1.62 +   * 
    1.63 +   * @param key
    1.64 +   * @return true if key is still valid.
    1.65 +   */
    1.66 +  protected boolean checkTimeOut(Object key)
    1.67 +  {
    1.68 +    synchronized(this.timeoutMap)
    1.69 +    {
    1.70 +      if(this.timeoutMap.containsKey(key))
    1.71 +      {
    1.72 +        long keytime = this.timeoutMap.get(key);
    1.73 +        if((System.currentTimeMillis() - keytime) < this.timeout)
    1.74 +        {
    1.75 +          return true;
    1.76 +        }
    1.77 +        else
    1.78 +        {
    1.79 +          remove(key);
    1.80 +          return false;
    1.81 +        }
    1.82 +      }
    1.83 +      else
    1.84 +      {
    1.85 +        return false;
    1.86 +      }
    1.87 +    }
    1.88 +  }
    1.89 +  
    1.90 +  @Override
    1.91 +  public boolean containsKey(Object key)
    1.92 +  {
    1.93 +    return checkTimeOut(key);
    1.94 +  }
    1.95 +
    1.96 +  @Override
    1.97 +  public synchronized V get(Object key)
    1.98 +  {
    1.99 +    if(checkTimeOut(key))
   1.100 +    {
   1.101 +      return super.get(key);
   1.102 +    }
   1.103 +    else
   1.104 +    {
   1.105 +      return null;
   1.106 +    }
   1.107 +  }
   1.108 +
   1.109 +  @Override
   1.110 +  public V put(K key, V value)
   1.111 +  {
   1.112 +    synchronized(this.timeoutMap)
   1.113 +    {
   1.114 +      removeStaleKeys();
   1.115 +      this.timeoutMap.put(key, System.currentTimeMillis());
   1.116 +      return super.put(key, value);
   1.117 +    }
   1.118 +  }
   1.119 +
   1.120 +  /**
   1.121 +   * @param arg0
   1.122 +   * @return
   1.123 +   */
   1.124 +  @Override
   1.125 +  public V remove(Object arg0)
   1.126 +  {
   1.127 +    synchronized(this.timeoutMap)
   1.128 +    {
   1.129 +      this.timeoutMap.remove(arg0);
   1.130 +      V val = super.remove(arg0);
   1.131 +      return val;
   1.132 +    }
   1.133 +  }
   1.134 +
   1.135 +  protected void removeStaleKeys()
   1.136 +  {
   1.137 +    synchronized(this.timeoutMap)
   1.138 +    {
   1.139 +      Set<Object> keySet = new HashSet<Object>(this.timeoutMap.keySet());
   1.140 +      for(Object key : keySet)
   1.141 +      {
   1.142 +        // The key/value is removed by the checkTimeOut() method if true
   1.143 +        checkTimeOut(key);
   1.144 +      }
   1.145 +    }
   1.146 +  }
   1.147 +  
   1.148 +}