1 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 26 public class ElementSizeQueue implements SortedEvictionQueue 27 { 28 private Map nodeMap; 29 private LinkedList evictionList; 30 private Comparator comparator; 31 32 private Set removalQueue; 33 private int numElements = 0; 34 35 ElementSizeQueue() 36 { 37 nodeMap = new HashMap (); 38 evictionList = new LinkedList (); 39 comparator = new MaxElementComparator(); 40 removalQueue = new HashSet (); 41 } 42 43 public void resortEvictionQueue() 44 { 45 Collections.sort(evictionList, comparator); 46 } 47 48 public NodeEntry getFirstNodeEntry() 49 { 50 try 51 { 52 NodeEntry ne; 53 while ((ne = (NodeEntry) evictionList.getFirst()) != null) 54 { 55 if (removalQueue.contains(ne)) 56 { 57 evictionList.removeFirst(); 58 removalQueue.remove(ne); 59 } 60 else 61 { 62 break; 63 } 64 } 65 return ne; 66 } 67 catch (NoSuchElementException e) 68 { 69 } 71 72 return null; 73 } 74 75 public NodeEntry getNodeEntry(Fqn fqn) 76 { 77 return (NodeEntry) nodeMap.get(fqn); 78 } 79 80 public NodeEntry getNodeEntry(String fqn) 81 { 82 return this.getNodeEntry(Fqn.fromString(fqn)); 83 } 84 85 public boolean containsNodeEntry(NodeEntry entry) 86 { 87 Fqn fqn = entry.getFqn(); 88 return this.getNodeEntry(fqn) != null; 89 } 90 91 public void removeNodeEntry(NodeEntry entry) 92 { 93 NodeEntry ne = (NodeEntry) nodeMap.remove(entry.getFqn()); 94 if (ne != null) 95 { 96 this.removalQueue.add(ne); 102 105 this.numElements -= ne.getNumberOfElements(); 106 } 107 } 108 109 public void addNodeEntry(NodeEntry entry) 110 { 111 if (!this.containsNodeEntry(entry)) 112 { 113 Fqn fqn = entry.getFqn(); 114 entry.queue = this; 115 nodeMap.put(fqn, entry); 116 evictionList.add(entry); 117 this.numElements += entry.getNumberOfElements(); 118 } 119 } 120 121 public int getNumberOfNodes() 122 { 123 return nodeMap.size(); 124 } 125 126 public int getNumberOfElements() 127 { 128 return this.numElements; 129 } 130 131 public void modifyElementCount(int difference) 132 { 133 this.numElements += difference; 134 } 135 136 public void clear() 137 { 138 nodeMap.clear(); 139 evictionList.clear(); 140 removalQueue.clear(); 141 this.numElements = 0; 142 } 143 144 final List getEvictionList() 145 { 146 return evictionList; 147 } 148 149 final Set getRemovalQueue() 150 { 151 return removalQueue; 152 } 153 154 final void prune() 155 { 156 Iterator it = evictionList.iterator(); 157 while (it.hasNext() && removalQueue.size() > 0) 158 { 159 if (removalQueue.remove(it.next())) 160 { 161 it.remove(); 162 } 163 } 164 } 165 166 public Iterator iterate() 167 { 168 return evictionList.iterator(); 169 } 170 171 182 static class MaxElementComparator implements Comparator 183 { 184 MaxElementComparator() 185 { 186 } 187 188 public int compare(Object o, Object o1) 189 { 190 if (o.equals(o1)) 191 { 192 return 0; 193 } 194 NodeEntry ne = (NodeEntry) o; 195 NodeEntry ne2 = (NodeEntry) o1; 196 197 int neNumElements = ne.getNumberOfElements(); 198 int neNumElements2 = ne2.getNumberOfElements(); 199 200 if (neNumElements > neNumElements2) 201 { 202 return -1; 203 } 204 else if (neNumElements < neNumElements2) 205 { 206 return 1; 207 } 208 else if (neNumElements == neNumElements2) 209 { 210 return 0; 211 } 212 213 throw new RuntimeException ("Should never reach this condition"); 214 } 215 } 216 217 } 218 219 220 | Popular Tags |