KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cache > store > FlipSpacesStore


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.store;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * @author <a HREF="mailto:lawrence_mccay-iii@hp.com">Larry McCay</a>
17  */

18 public class FlipSpacesStore
19     extends AbstractCacheStore
20 {
21     /**
22      * the data space which stores the most recently accessed objects
23      */

24     private Map JavaDoc m_newCache = null;
25
26     /**
27      * the data space which stores accessed items which have not been accessed since the last space swap. At the time
28      * <code>copySpaces</code> is called, objects still stored within this space are removed from the cache.
29      */

30     private Map JavaDoc m_oldCache = null;
31
32     /**
33      * the size at which the cache is deemed to be full
34      */

35     private int m_capacity;
36
37     /**
38      * Sets up the data spaces and sets the capacity of the cache
39      *
40      * @param capacity - the size at which the cache is deemed full
41      */

42     public FlipSpacesStore( final int capacity )
43     {
44         if ( capacity < 1 ) throw new IllegalArgumentException JavaDoc( "Specified capacity must be at least 1" );
45
46         m_capacity = capacity;
47         m_newCache = new HashMap JavaDoc( m_capacity );
48         m_oldCache = new HashMap JavaDoc( m_capacity );
49     }
50
51     /**
52      * Puts a given name value pair into the newCache.
53      * By invoking a get for the Object associated with the name before doing the actual put - we insure that the
54      * name value pair lives in the newCache data space. After executing the put - we determine if the cache is full
55      * - if so swap the data spaces - effectively clearing the newCache.
56      *
57      * @param key - name or key of the Object to be cached
58      * @param value - the actual cached Object
59      * @return the Object previously associated with the given name or key
60      */

61     public Object JavaDoc put( final Object JavaDoc key, final Object JavaDoc value )
62     {
63         Object JavaDoc old = null;
64         get( key );
65         old = m_newCache.put( key, value );
66         if ( isFull() ) // cache full?
67
{
68             copySpaces();
69         }
70         return old;
71     }
72
73     /**
74      * Removes the Object associated with the given name from the both spaces of this cache store.
75      * By doing a get before removing the object we insure that the object if in the cache has been moved to the newCache
76      *
77      * @param key - name or key associated with the Object to be removed
78      * @return the removed Object
79      */

80     public Object JavaDoc remove( final Object JavaDoc key )
81     {
82         Object JavaDoc cr = get( key );
83
84         return m_newCache.remove( key );
85     }
86
87     /**
88      * Gets the cached object associated with the given name.
89      * If the object does not exist within the newCache the old is checked. If the cache is determined to be full
90      * the spaces are swapped - effectively clearing the newCache. The object is then put into the newCache.
91      *
92      * @param key - the name or key of the requested object
93      * @return the requested Object
94      */

95     public Object JavaDoc get( final Object JavaDoc key )
96     {
97         Object JavaDoc value = null;
98         if ( m_newCache.containsKey( key ) )
99         {
100             value = m_newCache.get( key ); // try new space
101
}
102         else
103         {
104             if ( m_oldCache.containsKey( key ) )
105             {
106                 value = m_oldCache.get( key ); // try old space
107
if ( isFull() ) // cache full?
108
{
109                     copySpaces();
110                 }
111                 m_oldCache.remove( key ); // remove from old space
112
m_newCache.put( key, value ); // move to new space
113
}
114         }
115         return value;
116     }
117
118     /**
119      * Erase the oldCache - releasing those objects that are still considered old by the time the newCache has been
120      * determined to be full. Move the newCache to old and the previously oldCache to newCache effectively clearing
121      * it. Over time accessing objects will move them from the oldCache to the newCache leaving those objects behind
122      * that shall be cleared as the newCache is determined to be full again.
123      */

124     private void copySpaces()
125     {
126         m_oldCache.clear(); // erase old space
127
final Map JavaDoc temp = m_oldCache; // flip spaces
128
m_oldCache = m_newCache;
129         m_newCache = temp;
130     }
131
132     /**
133      * Gets the current size of the newCache.
134      * @return newCacheSize
135      */

136     public int size()
137     {
138         return m_newCache.size();
139     }
140
141     /**
142      * Gets the capacity for the cache. Once the cache size has reached the capacity it is considered full.
143      * @return cache capacity
144      */

145     public int capacity()
146     {
147         return m_capacity;
148     }
149
150     /**
151      * Checks if a given key exists within either of the spaces - old and new Caches.
152      * @return true if the key exists within this cache
153      */

154     public boolean containsKey( final Object JavaDoc key )
155     {
156         boolean rc = m_newCache.containsKey( key );
157         if ( !rc )
158         {
159             rc = m_oldCache.containsKey( key );
160         }
161         return rc;
162     }
163
164     /**
165      * Gets array of keys from both caches or spaces.
166      * @return array of all the keys within this cache
167      */

168     public Object JavaDoc[] keys()
169     {
170         final Set JavaDoc newKeys = m_newCache.keySet();
171         final Set JavaDoc oldKeys = m_oldCache.keySet();
172         final ArrayList JavaDoc keys = new ArrayList JavaDoc( newKeys );
173         keys.addAll( oldKeys );
174         return keys.toArray();
175     }
176 }
Popular Tags