1 8 package org.apache.avalon.excalibur.cache; 9 10 15 public class DefaultCache 16 extends AbstractCache 17 { 18 private ReplacementPolicy m_policy; 19 private CacheStore m_store; 20 21 public DefaultCache( final ReplacementPolicy policy, 22 final CacheStore store ) 23 { 24 m_policy = policy; 25 m_store = store; 26 } 27 28 public int capacity() 29 { 30 return m_store.capacity(); 31 } 32 33 public int size() 34 { 35 return m_store.size(); 36 } 37 38 public Object put( final Object key, final Object value ) 39 { 40 final Object oldValue = remove( key ); 41 42 if ( m_store.isFull() ) 43 { 44 remove( m_policy.selectVictim() ); 45 } 46 47 m_store.put( key, value ); 48 m_policy.add( key ); 49 notifyAdded( key, value ); 50 51 return oldValue; 52 } 53 54 public Object get( final Object key ) 55 { 56 final Object value = m_store.get( key ); 57 m_policy.hit( key ); 58 59 return value; 60 } 61 62 public Object remove( final Object key ) 63 { 64 Object value = null; 65 66 if ( m_store.containsKey( key ) ) 67 { 68 value = m_store.remove( key ); 69 m_policy.remove( key ); 70 notifyRemoved( key, value ); 71 } 72 73 return value; 74 } 75 76 public boolean containsKey( final Object key ) 77 { 78 return m_store.containsKey( key ); 79 } 80 81 public void clear() 82 { 83 final Object [] keys = m_store.keys(); 84 for ( int i = 0; i < keys.length; i++ ) 85 { 86 remove( keys[ i ] ); 87 } 88 } 89 } 90 | Popular Tags |