KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cache > DefaultCache


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;
9
10 /**
11  * Default <code>Cache</code> implementation.
12  *
13  * @author <a HREF="mailto:colus@apache.org">Eung-ju Park</a>
14  */

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 JavaDoc put( final Object JavaDoc key, final Object JavaDoc value )
39     {
40         final Object JavaDoc 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 JavaDoc get( final Object JavaDoc key )
55     {
56         final Object JavaDoc value = m_store.get( key );
57         m_policy.hit( key );
58
59         return value;
60     }
61
62     public Object JavaDoc remove( final Object JavaDoc key )
63     {
64         Object JavaDoc 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 JavaDoc key )
77     {
78         return m_store.containsKey( key );
79     }
80
81     public void clear()
82     {
83         final Object JavaDoc[] keys = m_store.keys();
84         for ( int i = 0; i < keys.length; i++ )
85         {
86             remove( keys[ i ] );
87         }
88     }
89 }
90
Popular Tags