Clover coverage report - Coverage timestamp: do jan 22 2004 21:12:32 CET FRAMES NO FRAMES |
file stats: | LOC: | 111 |  | Methods: | 6
| NCLOC: | 41 |  | Classes: | 1
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="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">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 | 6 |
         public FIFOCache() {
36 | 6 |
             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 | 6 |
             try {
41 | 6 |
                 Class.forName("java.util.LinkedHashSet");
42 | 6 |
                 list = new LinkedHashSet();
43 | 6 |
                 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 | 12 |
         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 | 66 |
         protected void itemPut(Object key) {
76 | 66 |
             if (!list.contains(key)) {
77 | 60 |
                 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 | 15 |
         protected Object removeItem() {
89 | 15 |
             Object toRemove;
90 |  |
91 | 15 |
             if (isSet) {
92 | 15 |
                 Iterator it = list.iterator();
93 | 15 |
                 toRemove = it.next();
94 | 15 |
                 it.remove();
95 |  |
             } else {
96 | 0 |
                 toRemove = ((List) list).remove(0);
97 |  |
             }
98 |  |
99 | 15 |
             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 | 45 |
         protected void itemRemoved(Object key) {
108 | 45 |
             list.remove(key);
109 |  |
         }
110 |  |
     }
111 |  |