colus 01/10/27 23:34:51
Added: src/scratchpad/org/apache/avalon/excalibur/cache
TimeoutValidator.java
src/scratchpad/org/apache/avalon/excalibur/cache/test
TimeoutValidatorTestCase.java
Log:
Added TimeoutValidator and it's TestCase.
Revision Changes Path
1.1
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/TimeoutValidator.java
Index: TimeoutValidator.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.excalibur.cache;
import java.util.Map;
import java.util.HashMap;
/**
* General timeout cache validator.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Eung-ju Park</a>
*/
public class TimeoutValidator
implements CacheValidator, CacheListener
{
private long m_timeout;
private Map m_timestamps;
public TimeoutValidator( final long timeout )
{
if ( 0 >= timeout )
{
throw new IllegalArgumentException( "Timeout must be greatter
than 0" );
}
m_timeout = timeout;
m_timestamps = new HashMap();
}
public boolean validate( final Object key, final Object value )
{
final long timestamp = ((Long)m_timestamps.get( key )).longValue();
if ( ( System.currentTimeMillis() - timestamp ) > m_timeout )
{
return false;
}
else
{
return true;
}
}
public void added( final CacheEvent event )
{
m_timestamps.put( event.getKey(),
new Long( System.currentTimeMillis() ) );
}
public void removed( final CacheEvent event )
{
m_timestamps.remove( event.getKey() );
}
}
1.1
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/test/TimeoutValidatorTestCase.java
Index: TimeoutValidatorTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.excalibur.cache.test;
import org.apache.avalon.excalibur.cache.Cache;
import org.apache.avalon.excalibur.cache.LRUCache;
import org.apache.avalon.excalibur.cache.TimeoutValidator;
import junit.framework.TestCase;
/**
* JUnit TestCase for TimeoutValidator.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Eung-ju Park</a>
*/
public class TimeoutValidatorTestCase
extends TestCase
{
public TimeoutValidatorTestCase( final String name )
{
super( name );
}
public void testNotExpired()
throws InterruptedException
{
final TimeoutValidator validator = new TimeoutValidator( 1000 );
final Cache cache = new LRUCache( 10 );
cache.addListener( validator );
cache.setValidator( validator );
cache.put( "K1", "V1" );
Thread.sleep( 100 );
assertTrue( cache.containsKey( "K1" ) );
}
public void testExpired()
throws InterruptedException
{
final TimeoutValidator validator = new TimeoutValidator( 1000 );
final Cache cache = new LRUCache( 10 );
cache.addListener( validator );
cache.setValidator( validator );
cache.put( "K1", "V1" );
Thread.sleep( 2000 );
assertTrue( ! cache.containsKey( "K1" ) );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>