KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > base > algorithm > FIFOCache


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.base.algorithm;
6
7 import java.util.*;
8
9 /**
10  * FIFO (First In First Out) based queue algorithm for the cache.
11  *
12  * No synchronization is required in this class since the
13  * <code>AbstractConcurrentReadCache</code> already takes care of any
14  * synchronization requirements.
15  *
16  * @version $Revision: 1.3 $
17  * @author <a HREF="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
18  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
19  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
20  */

21 public class FIFOCache extends AbstractConcurrentReadCache {
22
23     /**
24      * A queue containing all cache keys
25      */

26     private Collection list = new LinkedHashSet();
27
28     /**
29      * Constructs a FIFO Cache.
30      */

31     public FIFOCache() {
32         super();
33     }
34
35     /**
36      * Constructs a FIFO Cache of the specified capacity.
37      *
38      * @param capacity The maximum cache capacity.
39      */

40     public FIFOCache(int capacity) {
41         this();
42         maxEntries = capacity;
43     }
44
45     /**
46      * An object was retrieved from the cache. This implementation
47      * does noting since this event has no impact on the FIFO algorithm.
48      *
49      * @param key The cache key of the item that was retrieved.
50      */

51     protected void itemRetrieved(Object JavaDoc key) {
52     }
53
54     /**
55      * An object was put in the cache. This implementation just adds
56      * the key to the end of the list if it doesn't exist in the list
57      * already.
58      *
59      * @param key The cache key of the item that was put.
60      */

61     protected void itemPut(Object JavaDoc key) {
62         if (!list.contains(key)) {
63             list.add(key);
64         }
65     }
66
67     /**
68      * An item needs to be removed from the cache. The FIFO implementation
69      * removes the first element in the list (ie, the item that has been in
70      * the cache for the longest time).
71      *
72      * @return The key of whichever item was removed.
73      */

74     protected Object JavaDoc removeItem() {
75         Iterator it = list.iterator();
76         Object JavaDoc toRemove = it.next();
77         it.remove();
78
79         return toRemove;
80     }
81
82     /**
83      * Remove specified key since that object has been removed from the cache.
84      *
85      * @param key The cache key of the item that was removed.
86      */

87     protected void itemRemoved(Object JavaDoc key) {
88         list.remove(key);
89     }
90 }
91
Popular Tags