|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FIFOCache.java | 100% | 80% | 83,3% | 83,3% |
|
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="mailto:chris@swebtec.com">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 | 8 | public FIFOCache() { |
32 | 8 | super(); |
33 | } | |
34 | ||
35 | /** | |
36 | * Constructs a FIFO Cache of the specified capacity. | |
37 | * | |
38 | * @param capacity The maximum cache capacity. | |
39 | */ | |
40 | 0 | public FIFOCache(int capacity) { |
41 | 0 | this(); |
42 | 0 | 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 | 16 | protected void itemRetrieved(Object 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 | 120 | protected void itemPut(Object key) { |
62 | 120 | if (!list.contains(key)) { |
63 | 112 | 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 | 28 | protected Object removeItem() { |
75 | 28 | Iterator it = list.iterator(); |
76 | 28 | Object toRemove = it.next(); |
77 | 28 | it.remove(); |
78 | ||
79 | 28 | 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 | 84 | protected void itemRemoved(Object key) { |
88 | 84 | list.remove(key); |
89 | } | |
90 | } |
|