1 46 package org.mr.core.util; 47 48 import java.util.ArrayList ; 49 import java.util.Collection ; 50 import java.util.Iterator ; 51 import java.util.LinkedList ; 52 import java.util.List ; 53 import java.util.ListIterator ; 54 import java.util.NoSuchElementException ; 55 import java.util.Random ; 56 58 64 public class PrioritizedList implements List { 65 66 67 int numberOfPriorities; 68 List [] lists ; 69 70 public PrioritizedList(int numberOfPriorities){ 71 this.numberOfPriorities = numberOfPriorities; 72 lists = new List [numberOfPriorities]; 73 for (int i = 0; i < lists.length; i++) { 74 lists[i]= new LinkedList (); 75 } 76 } 77 78 public int size() { 79 int result = 0; 80 for (int i = 0; i < lists.length; i++) { 81 result += lists[i].size() ; 82 } 83 return result; 84 85 } 86 87 public void clear() { 88 for (int i = 0; i < lists.length; i++) { 89 lists[i].clear(); 90 } 91 } 92 93 public boolean isEmpty() { 94 return size() == 0; 95 } 96 97 public Object [] toArray() { 98 return makeListFromLists().toArray(); 100 } 101 102 private List makeListFromLists() { 103 ArrayList result = new ArrayList (); 104 for (int i = 0; i < lists.length; i++) { 105 result.addAll(lists[i]) ; 106 } 107 return result; 108 } 109 110 public Object get(int index) { 111 for (int i = 0; i < lists.length; i++) { 112 if (index < lists[i].size()) 113 return lists[i].get(index); 114 index -= lists[i].size() ; 115 } 116 return null; 117 } 118 119 public Object remove(int index) { 120 int temp = index; 121 for (int i = 0; i < lists.length; i++) { 122 if (temp < lists[i].size()) 123 return lists[i].remove(temp); 124 temp -= lists[i].size() ; 125 } 126 return null; 127 } 128 129 public void add(int index, Object element) { 130 throw new UnsupportedOperationException (); 131 } 132 133 public int indexOf(Object o) { 134 Prioritizeable p = (Prioritizeable) o; 135 int priorityIndex = (numberOfPriorities-1)-p.getPriority(); 136 int index = lists[priorityIndex].indexOf(o); 137 if (index < 0) 138 return -1; 139 for (int i=0; i<priorityIndex ; i++) { 140 index+=lists[i].size(); 141 } 142 return index; 143 } 144 145 public int lastIndexOf(Object o) { 146 Prioritizeable p = (Prioritizeable) o; 147 int priorityIndex = (numberOfPriorities-1)-p.getPriority(); 148 int index = lists[priorityIndex].lastIndexOf(o); 149 if (index < 0) 150 return -1; 151 for (int i=0; i<priorityIndex ; i++) { 152 index+=lists[i].size(); 153 } 154 return index; 155 } 156 157 public boolean add(Object o) { 158 Prioritizeable p = (Prioritizeable) o; 159 return lists[(numberOfPriorities-1)-p.getPriority()].add(o); 160 } 161 162 public boolean contains(Object o) { 163 Prioritizeable p = (Prioritizeable) o; 164 return lists[(numberOfPriorities-1)-p.getPriority()].contains(o); 165 } 166 167 public boolean remove(Object o) { 168 Prioritizeable p = (Prioritizeable) o; 169 return lists[(numberOfPriorities-1)-p.getPriority()].remove(o); 170 } 171 172 public boolean addAll(int index, Collection c) { 173 throw new UnsupportedOperationException (); 174 } 175 176 public boolean addAll(Collection c) { 178 boolean result = false; 179 Object o; 180 boolean temp; 181 Iterator ci = c.iterator(); 182 while (ci.hasNext()) { 183 o = ci.next(); 184 temp = add(o); 185 result = result || temp; 186 } 187 return result; 188 } 189 190 public void addAllToHead(Collection c) { 191 Iterator ci = c.iterator(); 192 Prioritizeable p; 193 int minPriorityIndex = numberOfPriorities-1; 194 while (ci.hasNext()) { 195 p = (Prioritizeable) ci.next(); 196 lists[minPriorityIndex-p.getPriority()].add(0,p); 197 } 198 } 199 200 public void addToHead(Object element){ 201 Prioritizeable p = (Prioritizeable) element; 202 lists[(numberOfPriorities-1)-p.getPriority()].add(0,p); 203 } 204 205 public boolean containsAll(Collection c) { 206 Iterator ci = c.iterator(); 207 Object o; 208 while(ci.hasNext()) { 209 o = ci.next(); 210 if (!contains(o)) 211 return false; 212 } 213 return true; 214 } 215 216 public boolean removeAll(Collection c) { 218 boolean result = false; 219 Iterator ci = c.iterator(); 220 Prioritizeable p; 221 int minPriorityIndex = numberOfPriorities-1; 222 while (ci.hasNext()) { 223 p = (Prioritizeable) ci.next(); 224 if (lists[minPriorityIndex-p.getPriority()].remove(p)) 225 result = true; 226 } 227 return result; 228 } 229 230 public boolean retainAll(Collection c) { 231 clear(); 232 return addAll(c); 233 } 234 235 public Iterator iterator() { 236 return new PriorityIterator(); 237 } 238 239 public List subList(int fromIndex, int toIndex) { 240 return makeListFromLists().subList(fromIndex, toIndex); 242 } 243 244 public ListIterator listIterator() { 245 return makeListFromLists().listIterator(); 247 } 248 249 public ListIterator listIterator(int index) { 250 return makeListFromLists().listIterator(index); 252 } 253 254 public Object set(int index, Object element) { 255 throw new UnsupportedOperationException (); 256 } 257 258 public Object [] toArray(Object [] a) { 259 return makeListFromLists().toArray(a); 261 } 262 263 class PriorityIterator implements Iterator { 265 266 int currentList; 268 boolean nextCalled; 269 270 int currentElement; 272 273 PriorityIterator () { 274 currentList = -1; 275 currentElement = -1; 276 nextCalled = false; 277 } 278 279 public void remove() { 280 if (nextCalled) { 281 lists[currentList].remove(currentElement); 282 currentElement--; 283 nextCalled = false; 284 } 285 else { 286 throw new IllegalStateException (); 287 } 288 } 289 290 public boolean hasNext() { 291 if (currentList >= 0) { 292 if (currentElement >= 0) { 294 if (currentElement < lists[currentList].size()-1) { 296 return true; 298 } 299 else { 300 for (int i=currentList+1 ; i<lists.length ; i++) { 303 if (!lists[i].isEmpty()) { 304 return true; 305 } 306 } 307 return false; 308 } 309 } 310 else { 311 for (int i=currentList ; i<lists.length ; i++) { 313 if (!lists[i].isEmpty()) { 314 return true; 315 } 316 } 317 return false; 318 } 319 } 320 else { 321 for (int i=0 ; i<lists.length ; i++) { 324 if (!lists[i].isEmpty()) { 325 return true; 326 } 327 } 328 return false; 329 } 330 } 332 public Object next() { 333 if (currentList >= 0) { 334 if (currentElement >= 0) { 336 if (currentElement < lists[currentList].size()-1) { 338 currentElement++; 340 nextCalled = true; 341 return lists[currentList].get(currentElement); 342 } 343 else { 344 for (int i=currentList+1 ; i<lists.length ; i++) { 347 if (!lists[i].isEmpty()) { 348 currentList = i; 349 currentElement = 0; 350 nextCalled = true; 351 return lists[currentList].get(currentElement); 352 } 353 } 354 throw new NoSuchElementException ("No more elements exist in the collection"); 355 } 356 } 357 else { 358 for (int i=currentList ; i<lists.length ; i++) { 360 if (!lists[i].isEmpty()) { 361 currentList = i; 362 currentElement = 0; 363 nextCalled = true; 364 return lists[currentList].get(currentElement); 365 } 366 } 367 throw new NoSuchElementException ("No more elements exist in the collection"); 368 } 369 } 370 else { 371 for (int i=0 ; i<lists.length ; i++) { 374 if (!lists[i].isEmpty()) { 375 currentList = i; 376 currentElement = 0; 377 nextCalled = true; 378 return lists[currentList].get(currentElement); 379 } 380 } 381 throw new NoSuchElementException ("No more elements exist in the collection"); 382 } 383 } } 386 453 454 } 455 | Popular Tags |