Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 153   Methods: 14
NCLOC: 99   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MRUQueue.java 62.5% 97.1% 92.9% 91.1%
coverage coverage
 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.HashMap;
 12    import java.util.Iterator;
 13    import java.util.Map;
 14    import java.util.NoSuchElementException;
 15   
 16    /**
 17    * MRU Eviction Queue implementation.
 18    * <p/>
 19    * This nodeMap is sorted by MRU. The first entry in the nodeMap
 20    * will also be the most recently used entry. The sort is implicit
 21    * based on a Stack that we can implicitly sort to the top by moving
 22    * a node that is used to the top of the eviction stack.
 23    *
 24    * @author Daniel Huang (dhuang@jboss.org)
 25    * @version $Revision: 1.8 $
 26    */
 27    public class MRUQueue implements EvictionQueue
 28    {
 29    // we use our own Stack/Linked List implementation here because it guarantees O(n) = 1 for add, remove, get and
 30    // we can sort it in order of MRU implicitly while still getting O(n) = 1 access time
 31    // throughout.
 32    Map<Fqn, EvictionListEntry> nodeMap;
 33    EvictionQueueList list;
 34    private int numElements = 0;
 35   
 36  14 MRUQueue()
 37    {
 38  14 nodeMap = new HashMap<Fqn, EvictionListEntry>();
 39  14 list = new EvictionQueueList();
 40    }
 41   
 42    /**
 43    * This call moves a NodeEntry to the top of the stack.
 44    * <p/>
 45    * When a node is visited this method should be called to guarantee MRU ordering.
 46    *
 47    * @param fqn Fqn of the nodeEntry to move to the top of the stack.
 48    */
 49  54 void moveToTopOfStack(Fqn fqn)
 50    {
 51  54 EvictionListEntry le = nodeMap.remove(fqn);
 52  54 if (le != null)
 53    {
 54  54 list.remove(le);
 55  54 list.addToTop(le);
 56  54 nodeMap.put(le.node.getFqn(), le);
 57    }
 58    }
 59   
 60    /**
 61    * Will return the first entry in the nodeMap.
 62    * <p/>
 63    * The first entry in this nodeMap will also be the most recently used entry.
 64    *
 65    * @return The first node entry in nodeMap.
 66    */
 67  10461 public NodeEntry getFirstNodeEntry()
 68    {
 69  10461 try
 70    {
 71  10461 return list.getFirst().node;
 72    }
 73    catch (NoSuchElementException e)
 74    {
 75    //
 76    }
 77   
 78  19 return null;
 79    }
 80   
 81  21021 public NodeEntry getNodeEntry(Fqn fqn)
 82    {
 83  21021 EvictionListEntry le = nodeMap.get(fqn);
 84  21021 if (le != null)
 85  77 return le.node;
 86   
 87  20944 return null;
 88    }
 89   
 90  12 public NodeEntry getNodeEntry(String fqn)
 91    {
 92  12 return this.getNodeEntry(Fqn.fromString(fqn));
 93    }
 94   
 95  10663 public boolean containsNodeEntry(NodeEntry entry)
 96    {
 97  10663 return nodeMap.containsKey(entry.getFqn());
 98    }
 99   
 100  10444 public void removeNodeEntry(NodeEntry entry)
 101    {
 102  10444 EvictionListEntry le = nodeMap.remove(entry.getFqn());
 103  10444 if (le != null)
 104    {
 105  10444 list.remove(le);
 106  10444 this.numElements -= le.node.getNumberOfElements();
 107    }
 108    }
 109   
 110  10663 public void addNodeEntry(NodeEntry entry)
 111    {
 112  10663 if (!this.containsNodeEntry(entry))
 113    {
 114  10663 entry.queue = this;
 115  10663 EvictionListEntry le = new EvictionListEntry(entry);
 116  10663 list.addToBottom(le);
 117  10663 nodeMap.put(entry.getFqn(), le);
 118  10663 this.numElements += entry.getNumberOfElements();
 119    }
 120    }
 121   
 122  10412 public int getNumberOfNodes()
 123    {
 124  10412 return list.size();
 125    }
 126   
 127  6 public int getNumberOfElements()
 128    {
 129  6 return this.numElements;
 130    }
 131   
 132  3 public void modifyElementCount(int difference)
 133    {
 134  3 this.numElements += difference;
 135    }
 136   
 137  2 public void clear()
 138    {
 139  2 nodeMap.clear();
 140  2 list.clear();
 141  2 this.numElements = 0;
 142    }
 143   
 144  0 public Iterator iterate()
 145    {
 146  0 return list.iterator();
 147    }
 148   
 149  1 public String toString()
 150    {
 151  1 return list.toString();
 152    }
 153    }