KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > cache > LRUCacheMap


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.cache;
20
21 import java.util.*;
22 import java.util.logging.*;
23 import java.util.logging.Logger JavaDoc;
24
25 import org.apache.commons.collections.map.*;
26
27 /**
28  * An extension of the Apache commons <code>LRUMap</code> which handles
29  * the removal of cached objects such that dependencies are kept and
30  * listeners get notified on object removal.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.1 $
34  *
35  */

36 public class LRUCacheMap extends LRUMap {
37
38
39     private boolean m_bIsDependantAware = false;
40     
41     /**
42      * Logger for this class.
43      */

44     private static final Logger JavaDoc m_logger = Logger.getLogger(LRUCacheMap.class.getName());
45
46     /**
47      * Construct new <code>LRUCacheMap</code> with size 100.
48      */

49     public LRUCacheMap() {
50         super(100,true);
51     }
52
53     /**
54      * Construct new <code>LRUCacheMap</code> with specified size.
55      *
56      * @param size size for map
57      */

58     public LRUCacheMap(int size) {
59         super(size,true);
60     }
61     
62     /* (non-Javadoc)
63      * @see org.apache.commons.collections.map.LRUMap#removeLRU(org.apache.commons.collections.map.AbstractLinkedMap.LinkEntry)
64      */

65     protected boolean removeLRU(AbstractLinkedMap.LinkEntry entry) {
66         
67         boolean bRemove = true;
68         
69         Object JavaDoc obj = entry.getValue();
70         
71         if(m_logger.isLoggable(Level.FINEST)) {
72             m_logger.logp(Level.FINEST, this.getClass().getName(), "removeLRU", "Removing " + obj);
73         }
74         
75         if(obj instanceof CacheableObject) {
76             ((CacheableObject)obj).notifyCacheListeners();
77         }
78         
79         if(m_bIsDependantAware == true
80                 && obj instanceof DependableCacheObject
81                 && ((DependableCacheObject)obj).hasDependants()) {
82             bRemove = false;
83         }
84         
85         return bRemove;
86     }
87
88     
89     /* (non-Javadoc)
90      * @see java.util.Map#clear()
91      */

92     public void clear() {
93         
94         for (Iterator iter = keySet().iterator(); iter.hasNext();) {
95             String JavaDoc sKey = (String JavaDoc) iter.next();
96             Object JavaDoc obj = get(sKey);
97             
98             if(obj instanceof CacheableObject) {
99                 ((CacheableObject)obj).notifyCacheListeners();
100             }
101         }
102         super.clear();
103     }
104     
105     /**
106      * Sets whether this cache checks objects for dependancies before
107      * paging the object out. If, when checking for dependancies, the object is
108      * found to have dependances the object will not be paged out.
109      *
110      *
111      * @param bIsDepAware <code>true</code> if the cache should check dependancies
112      * of an object before paging it out of the cache.
113      */

114     public void setDependancyAware(boolean bIsDepAware) {
115         m_bIsDependantAware = bIsDepAware;
116     }
117     
118     /**
119      * Returns <code>true</code> if this cache will check an object for
120      * dependancies before paging the object out.
121      *
122      * @return<code>true</code> if this cache will check an object for
123      * dependancies before paging the object out.
124      */

125     public boolean isDependancyAware() {
126         return m_bIsDependantAware;
127     }
128 }
129
Popular Tags