|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FIFOCache.java | 75% | 77,8% | 83,3% | 78,6% |
|
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.1 $ | |
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 | * A queue containing all cache keys | |
24 | */ | |
25 | private Collection list; | |
26 | ||
27 | /** | |
28 | * A flag indicating whether we are using a List or a Set for the key collection | |
29 | */ | |
30 | private boolean isSet = false; | |
31 | ||
32 | /** | |
33 | * Constructs a FIFO Cache. | |
34 | */ | |
35 | 8 | public FIFOCache() { |
36 | 8 | super(); |
37 | ||
38 | // Check if we're running under JRE 1.4+. If so we can use a LinkedHashSet | |
39 | // instead of a LinkedList for a big performance boost when removing elements. | |
40 | 8 | try { |
41 | 8 | Class.forName("java.util.LinkedHashSet"); |
42 | 8 | list = new LinkedHashSet(); |
43 | 8 | isSet = true; |
44 | } catch (ClassNotFoundException e) { | |
45 | 0 | list = new LinkedList(); |
46 | } | |
47 | } | |
48 | ||
49 | /** | |
50 | * Constructs a FIFO Cache of the specified capacity. | |
51 | * | |
52 | * @param capacity The maximum cache capacity. | |
53 | */ | |
54 | 0 | public FIFOCache(int capacity) { |
55 | 0 | this(); |
56 | 0 | maxEntries = capacity; |
57 | } | |
58 | ||
59 | /** | |
60 | * An object was retrieved from the cache. This implementation | |
61 | * does noting since this event has no impact on the FIFO algorithm. | |
62 | * | |
63 | * @param key The cache key of the item that was retrieved. | |
64 | */ | |
65 | 16 | protected void itemRetrieved(Object key) { |
66 | } | |
67 | ||
68 | /** | |
69 | * An object was put in the cache. This implementation just adds | |
70 | * the key to the end of the list if it doesn't exist in the list | |
71 | * already. | |
72 | * | |
73 | * @param key The cache key of the item that was put. | |
74 | */ | |
75 | 120 | protected void itemPut(Object key) { |
76 | 120 | if (!list.contains(key)) { |
77 | 112 | list.add(key); |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * An item needs to be removed from the cache. The FIFO implementation | |
83 | * removes the first element in the list (ie, the item that has been in | |
84 | * the cache for the longest time). | |
85 | * | |
86 | * @return The key of whichever item was removed. | |
87 | */ | |
88 | 28 | protected Object removeItem() { |
89 | 28 | Object toRemove; |
90 | ||
91 | 28 | if (isSet) { |
92 | 28 | Iterator it = list.iterator(); |
93 | 28 | toRemove = it.next(); |
94 | 28 | it.remove(); |
95 | } else { | |
96 | 0 | toRemove = ((List) list).remove(0); |
97 | } | |
98 | ||
99 | 28 | return toRemove; |
100 | } | |
101 | ||
102 | /** | |
103 | * Remove specified key since that object has been removed from the cache. | |
104 | * | |
105 | * @param key The cache key of the item that was removed. | |
106 | */ | |
107 | 84 | protected void itemRemoved(Object key) { |
108 | 84 | list.remove(key); |
109 | } | |
110 | } |
|