1 29 package jegg.impl; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.SortedMap ; 34 import java.util.TreeMap ; 35 import java.util.Vector ; 36 37 38 44 public class PriorityQueue 45 { 46 47 private SortedMap set = new TreeMap (); 48 49 54 public synchronized void add(final Priority p, final Object o) 55 { 56 List list = (List ) set.get(p); 57 if (null == list) 58 { 59 list = new Vector (); 60 set.put(p,list); 61 } 62 list.add(o); 63 } 64 65 69 public synchronized Object next() 70 { 71 for (Iterator it = set.keySet().iterator(); it.hasNext(); ) 72 { 73 Priority p = (Priority) it.next(); 74 List list = (List ) set.get(p); 75 if (null != list && !list.isEmpty()) 76 { 77 return list.remove(0); 78 } 79 } 80 return null; 81 } 82 83 88 public synchronized Object next(Priority p) 89 { 90 List list = (List ) set.get(p); 91 if (null != list && ! list.isEmpty()) 92 { 93 return list.remove(0); 94 } 95 return null; 96 } 97 98 102 public synchronized int size() 103 { 104 int num = 0; 105 for (Iterator it = set.keySet().iterator(); it.hasNext(); ) 106 { 107 Priority p = (Priority) it.next(); 108 List list = (List ) set.get(p); 109 if (null != list) 110 { 111 num += list.size(); 112 } 113 } 114 return num; 115 } 116 117 120 123 public synchronized void clear() 124 { 125 set.clear(); 126 } 127 128 132 public synchronized boolean isEmpty() 133 { 134 return set.isEmpty(); 135 } 136 } 137 | Popular Tags |