Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 24411 invoked from network); 24 Nov 2003 07:03:50 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 24 Nov 2003 07:03:50 -0000 Received: (qmail 30455 invoked by uid 500); 24 Nov 2003 07:03:25 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 30401 invoked by uid 500); 24 Nov 2003 07:03:24 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 30388 invoked from network); 24 Nov 2003 07:03:24 -0000 Received: from unknown (HELO pintail.mail.pas.earthlink.net) (207.217.120.122) by daedalus.apache.org with SMTP; 24 Nov 2003 07:03:24 -0000 Received: from h-67-101-84-88.lsanca54.dynamic.covad.net ([67.101.84.88] helo=[192.168.1.100]) by pintail.mail.pas.earthlink.net with esmtp (Exim 3.33 #1) id 1AOAky-0002jq-00 for commons-dev@jakarta.apache.org; Sun, 23 Nov 2003 23:03:36 -0800 Mime-Version: 1.0 (Apple Message framework v606) To: commons-dev@jakarta.apache.org Message-Id: <622AB1BE-1E4C-11D8-B7D9-000393A577F4@25thstreet.net> Content-Type: multipart/mixed; boundary=Apple-Mail-3--991554746 From: Joseph Rosenblum Subject: TimerMap Date: Sun, 23 Nov 2003 23:04:02 -0800 X-Mailer: Apple Mail (2.606) X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N --Apple-Mail-3--991554746 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Commons Developers, The attached class is a very simple implementation of the java.util.Map interface that gives each key a TTL (time to live) in the map. I've found this extremely useful as a backing store for certain types of caches (where you want to expire items based on time in cache, as opposed to LRU, etc..) and for tracking services that I want check the availability of. It's called TimerMap and takes a long TTL in the constructor. I'd love to donate this code to the commons, please let me know if you find it useful. -Joe Joseph Rosenblum | 25th Street Networks Easy, Reliable Web Hosting @ http://www.25thstreet.net/ --Apple-Mail-3--991554746 Content-Transfer-Encoding: 7bit Content-Type: text/plain; x-unix-mode=0644; name="TimerMap.java" Content-Disposition: attachment; filename=TimerMap.java package net.twentyfifth.util; import java.util.*; /** * This class will expire the keys in a given map after the interval * specified in the constructor. Note: the behavior of this class is such * that if you configure a TimerMap with a 5 minute timeout, and if 4:59 * after you add a certain key, you re-add a duplicate of that key, the * original TimerTask to remove the key will run 1 second later (and then * again 5 minutes later). * * @author Joseph Rosenblum */ public class TimerMap implements Map { protected final long delay; private final Map timerMap = Collections.synchronizedMap(new HashMap()); private Timer timer; public TimerMap(long delay) { this.delay = delay; timer = new Timer(); } public void finalize() { timer.cancel(); } public long getDelay(Object key) { return delay; } public int size() { return this.timerMap.size(); } public boolean isEmpty() { return this.timerMap.isEmpty(); } public boolean containsKey(Object key) { return this.timerMap.containsKey(key); } public boolean containsValue(Object value) { return this.timerMap.containsValue(value); } public Object get(Object key) { return this.timerMap.get(key); } public Object put(Object key, Object value) { Object prevKey = this.timerMap.put(key, value); timer.schedule(new TimedMapKey(key), delay); return prevKey; } public Object remove(Object key) { return this.timerMap.remove(key); } public void putAll(Map t) { Iterator i = t.keySet().iterator(); while (i.hasNext()) { Object key = i.next(); Object val = t.get(key); this.put(key, val); } } public void clear() { } public Set keySet() { return this.timerMap.keySet(); } public Collection values() { return this.timerMap.values(); } public Set entrySet() { return this.timerMap.entrySet(); } /** * Basically wraps map keys in a TimerTask that * will remove them from the timerMap after the delay * interval has expired. */ public final class TimedMapKey extends TimerTask { private final Object key; TimedMapKey(Object key) { this.key = key; } public void run() { timerMap.remove(key); } } } --Apple-Mail-3--991554746 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org --Apple-Mail-3--991554746--