1 package com.thoughtworks.xstream.core.util; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 5 import java.util.Iterator ; 6 7 15 public class PrioritizedList { 16 17 22 private final LinkedItem pointerToFirst = new LinkedItem(null, 0, null); 23 24 private int lowestPriority = Integer.MAX_VALUE; 25 26 29 public void add(Object item) { 30 add(item, 0); 31 } 32 33 public void add(Object item, int priority) { 34 LinkedItem current = pointerToFirst; 38 while(current.next != null && priority < current.next.priority) { 39 current = current.next; 40 } 41 current.next = new LinkedItem(item, priority, current.next); 42 if (priority < lowestPriority) { 43 lowestPriority = priority; 44 } 45 } 46 47 public Iterator iterator() { 48 return new LinkedItemIterator(pointerToFirst.next); 49 } 50 51 public Object firstOfLowestPriority() { 52 for(LinkedItem current = pointerToFirst.next; current != null; current = current.next) { 53 if (current.priority == lowestPriority) { 54 return current.value; 55 } 56 } 57 return null; 58 } 59 60 private static class LinkedItem { 61 62 final Object value; 63 final int priority; 64 65 LinkedItem next; 66 67 public LinkedItem(Object value, int priority, LinkedItem next) { 68 this.value = value; 69 this.priority = priority; 70 this.next = next; 71 } 72 73 } 74 75 private static class LinkedItemIterator implements Iterator { 76 77 private LinkedItem current; 78 79 public LinkedItemIterator(LinkedItem current) { 80 this.current = current; 81 } 82 83 public void remove() { 84 throw new UnsupportedOperationException (); 85 } 86 87 public boolean hasNext() { 88 return current != null; 89 } 90 91 public Object next() { 92 Object result = current.value; 93 current = current.next; 94 return result; 95 } 96 97 } 98 99 } 100 | Popular Tags |