Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 218   Methods: 18
NCLOC: 158   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ElementSizeQueue.java 70% 88.7% 94.4% 85.7%
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.Collections;
 12    import java.util.Comparator;
 13    import java.util.HashMap;
 14    import java.util.HashSet;
 15    import java.util.Iterator;
 16    import java.util.LinkedList;
 17    import java.util.List;
 18    import java.util.Map;
 19    import java.util.NoSuchElementException;
 20    import java.util.Set;
 21   
 22    /**
 23    * @author Daniel Huang
 24    * @version $Revision: 1.3 $
 25    */
 26    public class ElementSizeQueue implements SortedEvictionQueue
 27    {
 28    private Map<Fqn, NodeEntry> nodeMap;
 29    private LinkedList<NodeEntry> evictionList;
 30    private Comparator<NodeEntry> comparator;
 31   
 32    private Set<NodeEntry> removalQueue;
 33    private int numElements = 0;
 34   
 35  19 ElementSizeQueue()
 36    {
 37  19 nodeMap = new HashMap<Fqn, NodeEntry>();
 38  19 evictionList = new LinkedList<NodeEntry>();
 39  19 comparator = new MaxElementComparator();
 40  19 removalQueue = new HashSet<NodeEntry>();
 41    }
 42   
 43  18 public void resortEvictionQueue()
 44    {
 45  18 Collections.sort(evictionList, comparator);
 46    }
 47   
 48  7625 public NodeEntry getFirstNodeEntry()
 49    {
 50  7625 try
 51    {
 52  7625 NodeEntry ne;
 53  ? while ((ne = evictionList.getFirst()) != null)
 54    {
 55  15196 if (removalQueue.contains(ne))
 56    {
 57  7593 evictionList.removeFirst();
 58  7593 removalQueue.remove(ne);
 59    }
 60    else
 61    {
 62  7603 break;
 63    }
 64    }
 65  7603 return ne;
 66    }
 67    catch (NoSuchElementException e)
 68    {
 69    //
 70    }
 71   
 72  22 return null;
 73    }
 74   
 75  47914 public NodeEntry getNodeEntry(Fqn fqn)
 76    {
 77  47914 return nodeMap.get(fqn);
 78    }
 79   
 80  13 public NodeEntry getNodeEntry(String fqn)
 81    {
 82  13 return this.getNodeEntry(Fqn.fromString(fqn));
 83    }
 84   
 85  20639 public boolean containsNodeEntry(NodeEntry entry)
 86    {
 87  20639 Fqn fqn = entry.getFqn();
 88  20639 return this.getNodeEntry(fqn) != null;
 89    }
 90   
 91  10104 public void removeNodeEntry(NodeEntry entry)
 92    {
 93  10104 NodeEntry ne = nodeMap.remove(entry.getFqn());
 94  10104 if (ne != null)
 95    {
 96    // don't remove directly from the LinkedList otherwise we will incur a O(n) = n
 97    // performance penalty for every removal! In the prune method for LFU, we will iterate the LinkedList through ONCE
 98    // doing a single O(n) = n operation and removal. This is much preferred over running O(n) = n every single time
 99    // remove is called. There is also special logic in the getFirstNodeEntry that will know to check
 100    // the removalQueue before returning.
 101  10104 this.removalQueue.add(ne);
 102    /* if(!evictionList.remove(ne)) {
 103    throw new RuntimeException("");
 104    } */
 105  10104 this.numElements -= ne.getNumberOfElements();
 106    }
 107    }
 108   
 109  18138 public void addNodeEntry(NodeEntry entry)
 110    {
 111  18138 if (!this.containsNodeEntry(entry))
 112    {
 113  18138 Fqn fqn = entry.getFqn();
 114  18138 entry.queue = this;
 115  18138 nodeMap.put(fqn, entry);
 116  18138 evictionList.add(entry);
 117  18138 this.numElements += entry.getNumberOfElements();
 118    }
 119    }
 120   
 121  7127 public int getNumberOfNodes()
 122    {
 123  7127 return nodeMap.size();
 124    }
 125   
 126  9 public int getNumberOfElements()
 127    {
 128  9 return this.numElements;
 129    }
 130   
 131  953 public void modifyElementCount(int difference)
 132    {
 133  953 this.numElements += difference;
 134    }
 135   
 136  0 public void clear()
 137    {
 138  0 nodeMap.clear();
 139  0 evictionList.clear();
 140  0 removalQueue.clear();
 141  0 this.numElements = 0;
 142    }
 143   
 144  1 final List<NodeEntry> getEvictionList()
 145    {
 146  1 return evictionList;
 147    }
 148   
 149  1 final Set<NodeEntry> getRemovalQueue()
 150    {
 151  1 return removalQueue;
 152    }
 153   
 154  32 final void prune()
 155    {
 156  32 Iterator<NodeEntry> it = evictionList.iterator();
 157  32 while (it.hasNext() && removalQueue.size() > 0)
 158    {
 159  4999 if (removalQueue.remove(it.next()))
 160    {
 161  2500 it.remove();
 162    }
 163    }
 164    }
 165   
 166  4 public Iterator<NodeEntry> iterate()
 167    {
 168  4 return evictionList.iterator();
 169    }
 170   
 171    /**
 172    * Comparator class for Max Elements.
 173    * <p/>
 174    * This class will sort the eviction queue in the correct eviction order.
 175    * The top of the list should evict before the bottom of the list.
 176    * <p/>
 177    * The sort is based on descending order of numElements.
 178    * <p/>
 179    * Note: this class has a natural ordering that is inconsistent with equals as defined by the java.lang.Comparator
 180    * contract.
 181    */
 182    static class MaxElementComparator implements Comparator<NodeEntry>
 183    {
 184  19 MaxElementComparator()
 185    {
 186    }
 187   
 188  30616 public int compare(NodeEntry ne1, NodeEntry ne2)
 189    {
 190  30616 if (ne1.equals(ne2))
 191    {
 192  0 return 0;
 193    }
 194   
 195  30616 int neNumElements = ne1.getNumberOfElements();
 196  30616 int neNumElements2 = ne2.getNumberOfElements();
 197   
 198  30616 if (neNumElements > neNumElements2)
 199    {
 200  1044 return -1;
 201    }
 202  29572 else if (neNumElements < neNumElements2)
 203    {
 204  2833 return 1;
 205    }
 206  26739 else if (neNumElements == neNumElements2)
 207    {
 208  26739 return 0;
 209    }
 210   
 211  0 throw new RuntimeException("Should never reach this condition");
 212    }
 213   
 214    }
 215   
 216    }
 217   
 218