Seems like a cool idea....  However, I'm concerned about the following points in the 
implementation:

If the timer expires after an iterator on the keys or values is obtained, the 
underlying map is modified directly - this means that the next access to the iterator 
(see HashMap javadoc) will throw a ConcurrentModificationException.  To my mind, this 
makes iteration over this map fairly difficult, particularly as many algorithms which 
use maps probably do use iterators to access the data in the map.  A possible solution 
- provide wrapping implementations of Set/Collection which return FilterIterators that 
filter out expired items during iteration - only deleting those items after all 
existing iterators have completed iteration or have been destroyed.  This gets 
complicated quickly, however.  

It seems (perhaps I'm missing something) to be overkill to synchronize all accesses to 
the TimerMap.  I'm not quite sure why this is necessary - I don't think the 
implementation can guarantee that an object will be expired at the instant the timer 
runs out - I don't believe TimerTask provides this guarantee.  Therefore, the 
synchronization seems to be unneeded - the expired object will be removed "soon" after 
the expiry time is reached.  

Cheers, 
-AMT

-----Original Message-----
From: Joseph Rosenblum [mailto:[EMAIL PROTECTED] 
Sent: Sunday, November 23, 2003 11:04 PM
To: [EMAIL PROTECTED]
Subject: TimerMap


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/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to