chris@1: /*
chris@1: * SONEWS News Server
chris@1: * see AUTHORS for the list of contributors
chris@1: *
chris@1: * This program is free software: you can redistribute it and/or modify
chris@1: * it under the terms of the GNU General Public License as published by
chris@1: * the Free Software Foundation, either version 3 of the License, or
chris@1: * (at your option) any later version.
chris@1: *
chris@1: * This program is distributed in the hope that it will be useful,
chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@1: * GNU General Public License for more details.
chris@1: *
chris@1: * You should have received a copy of the GNU General Public License
chris@1: * along with this program. If not, see .
chris@1: */
chris@1:
chris@1: package org.sonews.util;
chris@1:
chris@1: import java.util.HashMap;
chris@1: import java.util.HashSet;
chris@1: import java.util.Map;
chris@1: import java.util.Set;
chris@1: import java.util.concurrent.ConcurrentHashMap;
chris@1:
chris@1: /**
chris@1: * Implementation of a Map that will loose its stored values after a
chris@1: * configurable amount of time.
chris@1: * This class may be used to cache config values for example.
chris@1: * @author Christian Lins
chris@1: * @since sonews/0.5.0
chris@1: */
chris@1: public class TimeoutMap extends ConcurrentHashMap
chris@1: {
chris@1:
chris@1: private static final long serialVersionUID = 453453467700345L;
chris@1:
chris@1: private int timeout = 60000; // 60 sec
chris@1: private transient Map timeoutMap = new HashMap();
chris@1:
chris@1: /**
chris@1: * Constructor.
chris@1: * @param timeout Timeout in milliseconds
chris@1: */
chris@1: public TimeoutMap(final int timeout)
chris@1: {
chris@1: this.timeout = timeout;
chris@1: }
chris@1:
chris@1: /**
chris@1: * Uses default timeout (60 sec).
chris@1: */
chris@1: public TimeoutMap()
chris@1: {
chris@1: }
chris@1:
chris@1: /**
chris@1: *
chris@1: * @param key
chris@1: * @return true if key is still valid.
chris@1: */
chris@1: protected boolean checkTimeOut(Object key)
chris@1: {
chris@1: synchronized(this.timeoutMap)
chris@1: {
chris@1: if(this.timeoutMap.containsKey(key))
chris@1: {
chris@1: long keytime = this.timeoutMap.get(key);
chris@1: if((System.currentTimeMillis() - keytime) < this.timeout)
chris@1: {
chris@1: return true;
chris@1: }
chris@1: else
chris@1: {
chris@1: remove(key);
chris@1: return false;
chris@1: }
chris@1: }
chris@1: else
chris@1: {
chris@1: return false;
chris@1: }
chris@1: }
chris@1: }
chris@1:
chris@1: @Override
chris@1: public boolean containsKey(Object key)
chris@1: {
chris@1: return checkTimeOut(key);
chris@1: }
chris@1:
chris@1: @Override
chris@1: public synchronized V get(Object key)
chris@1: {
chris@1: if(checkTimeOut(key))
chris@1: {
chris@1: return super.get(key);
chris@1: }
chris@1: else
chris@1: {
chris@1: return null;
chris@1: }
chris@1: }
chris@1:
chris@1: @Override
chris@1: public V put(K key, V value)
chris@1: {
chris@1: synchronized(this.timeoutMap)
chris@1: {
chris@1: removeStaleKeys();
chris@1: this.timeoutMap.put(key, System.currentTimeMillis());
chris@1: return super.put(key, value);
chris@1: }
chris@1: }
chris@1:
chris@1: /**
chris@1: * @param arg0
chris@1: * @return
chris@1: */
chris@1: @Override
chris@1: public V remove(Object arg0)
chris@1: {
chris@1: synchronized(this.timeoutMap)
chris@1: {
chris@1: this.timeoutMap.remove(arg0);
chris@1: V val = super.remove(arg0);
chris@1: return val;
chris@1: }
chris@1: }
chris@1:
chris@1: protected void removeStaleKeys()
chris@1: {
chris@1: synchronized(this.timeoutMap)
chris@1: {
chris@1: Set