KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cache > validator > TimeoutValidator


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.cache.validator;
9
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
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 /**
17  * General timeout cache validator.
18  *
19  * @author <a HREF="mailto:colus@apache.org">Eung-ju Park</a>
20  */

21 public class TimeoutValidator
22     implements CacheValidator, CacheListener
23 {
24     private long m_timeout;
25     private Map JavaDoc m_timestamps;
26
27     public TimeoutValidator( final long timeout )
28     {
29         if ( 0 >= timeout )
30         {
31             throw new IllegalArgumentException JavaDoc( "Timeout must be greatter than 0" );
32         }
33
34         m_timeout = timeout;
35
36         m_timestamps = new HashMap JavaDoc();
37     }
38
39     public boolean validate( final Object JavaDoc key, final Object JavaDoc value )
40     {
41         final long timestamp = ((Long JavaDoc)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 JavaDoc( System.currentTimeMillis() ) );
56     }
57
58     public void removed( final CacheEvent event )
59     {
60         m_timestamps.remove( event.getKey() );
61     }
62 }
63
Popular Tags