KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > FIFOQueue


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.eviction;
8
9 import org.jboss.cache.Fqn;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.LinkedHashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * FIFO Eviction Queue implementation for FIFO Policy.
17  *
18  * @author Daniel Huang (dhuang@jboss.org)
19  * @version $Revision: 1.4 $
20  */

21 public class FIFOQueue implements EvictionQueue
22 {
23    private Map JavaDoc nodeMap;
24    private int numElements = 0;
25
26    FIFOQueue()
27    {
28       nodeMap = new LinkedHashMap JavaDoc();
29       // We use a LinkedHashMap here because we want to maintain FIFO ordering and still get the benefits of
30
// O(n) = 1 for add/remove/search.
31
}
32
33    public NodeEntry getFirstNodeEntry()
34    {
35 /* Iterator it = nodeMap.keySet().iterator();
36       if(it.hasNext()) {
37          return (NodeEntry) nodeMap.get(it.next());
38       }
39
40       return null; */

41
42       // this code path is *slightly* faster when profiling. 20ms faster iterating over 200000 entries in queue.
43
if (nodeMap.size() > 0)
44       {
45          return (NodeEntry) nodeMap.values().iterator().next();
46       }
47
48       return null;
49    }
50
51    public NodeEntry getNodeEntry(Fqn fqn)
52    {
53       return (NodeEntry) nodeMap.get(fqn);
54    }
55
56    public NodeEntry getNodeEntry(String JavaDoc fqn)
57    {
58       return this.getNodeEntry(Fqn.fromString(fqn));
59    }
60
61    public boolean containsNodeEntry(NodeEntry entry)
62    {
63       Fqn fqn = entry.getFqn();
64       return this.getNodeEntry(fqn) != null;
65    }
66
67    public void removeNodeEntry(NodeEntry entry)
68    {
69       NodeEntry e = (NodeEntry) nodeMap.remove(entry.getFqn());
70       this.numElements -= e.getNumberOfElements();
71    }
72
73    public void addNodeEntry(NodeEntry entry)
74    {
75       if (!this.containsNodeEntry(entry))
76       {
77          entry.queue = this;
78          nodeMap.put(entry.getFqn(), entry);
79          this.numElements += entry.getNumberOfElements();
80       }
81    }
82
83    public int getNumberOfNodes()
84    {
85       return nodeMap.size();
86    }
87
88    public int getNumberOfElements()
89    {
90       return this.numElements;
91    }
92
93    public void modifyElementCount(int difference)
94    {
95       this.numElements += difference;
96    }
97
98    public void clear()
99    {
100       nodeMap.clear();
101       this.numElements = 0;
102    }
103
104    public Iterator JavaDoc iterate()
105    {
106       return nodeMap.values().iterator();
107    }
108 }
109
Popular Tags