1 8 package org.apache.avalon.excalibur.cache.validator; 9 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import org.apache.avalon.excalibur.cache.CacheEvent; 13 import org.apache.avalon.excalibur.cache.CacheListener; 14 import org.apache.avalon.excalibur.cache.CacheValidator; 15 16 21 public class TimeoutValidator 22 implements CacheValidator, CacheListener 23 { 24 private long m_timeout; 25 private Map m_timestamps; 26 27 public TimeoutValidator( final long timeout ) 28 { 29 if ( 0 >= timeout ) 30 { 31 throw new IllegalArgumentException ( "Timeout must be greatter than 0" ); 32 } 33 34 m_timeout = timeout; 35 36 m_timestamps = new HashMap (); 37 } 38 39 public boolean validate( final Object key, final Object value ) 40 { 41 final long timestamp = ((Long )m_timestamps.get( key )).longValue(); 42 if ( ( System.currentTimeMillis() - timestamp ) > m_timeout ) 43 { 44 return false; 45 } 46 else 47 { 48 return true; 49 } 50 } 51 52 public void added( final CacheEvent event ) 53 { 54 m_timestamps.put( event.getKey(), 55 new Long ( System.currentTimeMillis() ) ); 56 } 57 58 public void removed( final CacheEvent event ) 59 { 60 m_timestamps.remove( event.getKey() ); 61 } 62 } 63 | Popular Tags |