KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > policy > FIFOMaintenancePolicy


1 /*
2 Copyright 2004 Philip Jacob <phil@whirlycott.com>
3                     Seth Fitzsimmons <seth@note.amherst.edu>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */

17
18 package com.whirlycott.cache.policy;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Map.Entry;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.whirlycott.cache.CacheConfiguration;
31 import com.whirlycott.cache.CacheMaintenancePolicy;
32 import com.whirlycott.cache.ManagedCache;
33 import com.whirlycott.cache.Messages;
34
35 /**
36  * This policy removes cache items in the order in which they were added.
37  *
38  * @author Phil Jacob
39  */

40 public class FIFOMaintenancePolicy implements CacheMaintenancePolicy {
41     
42     private static final Log log = LogFactory.getLog(FIFOMaintenancePolicy.class);
43     
44     protected ManagedCache managedCache = null;
45
46     protected int maxSize;
47
48     public void performMaintenance() {
49         log.debug(Messages.getString("FIFOMaintenancePolicy.performing_fifo_maintenance")); //$NON-NLS-1$
50

51         final Object JavaDoc[] args = {
52                 new Integer JavaDoc(maxSize),
53                 new Integer JavaDoc(managedCache.size())
54         };
55         log.debug( Messages.getCompoundString("CacheMaintenancePolicy.report_items", args) ); //$NON-NLS-1$
56

57         //Sort the entries in the cache.
58
final List JavaDoc entries = new ArrayList JavaDoc(managedCache.entrySet());
59         int currentSize = managedCache.size();
60         if (maxSize < currentSize) {
61             final Object JavaDoc[] args1 = {
62                     new Integer JavaDoc(currentSize - maxSize)
63             };
64             log.debug( Messages.getCompoundString("CacheMaintenancePolicy.clearing_approximately", args1) ); //$NON-NLS-1$
65
Collections.sort(entries, new AddedComparator());
66             final List JavaDoc removeThese = entries.subList(0, currentSize - maxSize);
67             for (Iterator JavaDoc i = removeThese.iterator(); i.hasNext();) {
68                 final Map.Entry JavaDoc entry = (Entry) i.next();
69                 if (entry != null) {
70                     //log.trace("Removing: " + entry.getKey());
71
managedCache.remove(entry.getKey());
72                 }
73             }
74             log.debug(Messages.getString("FIFOMaintenancePolicy.new_size") + managedCache.size()); //$NON-NLS-1$
75
}
76         
77     }
78
79     public void setCache(final ManagedCache _cache) {
80         managedCache = _cache;
81     }
82
83     public void setConfiguration(final CacheConfiguration _configuration) {
84         maxSize = _configuration.getMaxSize();
85     }
86 }
Popular Tags