1 8 package org.apache.avalon.excalibur.cache; 9 10 16 public final class ValidatingCache 17 implements Cache 18 { 19 private Cache m_cache; 20 private CacheValidator m_validator; 21 22 26 public ValidatingCache( final Cache cache, final CacheValidator validator ) 27 { 28 m_cache = cache; 29 m_validator = validator; 30 } 31 32 private boolean validate( final Object key, final Object value ) 33 { 34 if ( null == m_validator ) 35 { 36 return true; 37 } 38 else 39 { 40 return m_validator.validate( key, value ); 41 } 42 } 43 44 public void addListener( final CacheListener listener ) 45 { 46 m_cache.addListener( listener ); 47 } 48 49 public void removeListener( final CacheListener listener ) 50 { 51 m_cache.removeListener( listener ); 52 } 53 54 public int capacity() 55 { 56 return m_cache.capacity(); 57 } 58 59 public int size() 60 { 61 return m_cache.size(); 62 } 63 64 public Object put( final Object key, final Object value ) 65 { 66 return m_cache.put( key, value ); 67 } 68 69 public Object get( final Object key ) 70 { 71 Object value = m_cache.get( key ); 72 73 if ( ! validate( key, value ) ) 74 { 75 remove( key ); 76 value = null; 77 } 78 79 return value; 80 } 81 82 public Object remove( Object key ) 83 { 84 return m_cache.remove( key ); 85 } 86 87 public boolean containsKey( final Object key ) 88 { 89 boolean contains = false; 90 91 if ( m_cache.containsKey( key ) ) 92 { 93 final Object value = m_cache.get( key ); 94 if ( validate( key, value ) ) 95 { 96 contains = true; 97 } 98 else 99 { 100 remove( key ); 101 } 102 } 103 104 return contains; 105 } 106 107 public void clear() 108 { 109 m_cache.clear(); 110 } 111 } 112 | Popular Tags |