1 7 package org.jboss.jms.util; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Comparator ; 12 import java.util.Iterator ; 13 import java.util.SortedSet ; 14 import java.util.TreeSet ; 15 16 21 public class SortedSetPriorityQueue implements PriorityQueue 22 { 23 private SortedSet contents = null; 24 25 public SortedSetPriorityQueue() 26 { 27 this.contents = new TreeSet (); 28 } 29 30 public SortedSetPriorityQueue(Comparator comparator) 31 { 32 this.contents = new TreeSet (comparator); 33 } 34 35 public void enqueue(Object object) 36 { 37 this.contents.add(object); 38 } 39 40 public void enqueue(Collection collection) 41 { 42 this.contents.addAll(collection); 43 } 44 45 public Object dequeue() 46 { 47 if (!this.contents.isEmpty()) 48 { 49 Iterator iterator = this.contents.iterator(); 50 Object object = iterator.next(); 51 iterator.remove(); 52 return object; 53 } 54 else 55 { 56 return null; 57 } 58 } 59 60 public Collection dequeue(int maximumItems) 61 { 62 Collection items = new ArrayList (maximumItems); 63 Iterator iterator = this.contents.iterator(); 64 int i = 0; 65 while (iterator.hasNext() && i++ < maximumItems) 66 { 67 items.add(iterator.next()); 68 iterator.remove(); 69 } 70 return items; 71 } 72 73 public Object peek() 74 { 75 return this.contents.first(); 76 } 77 78 public Collection peek(int maximumItems) 79 { 80 Collection items = new ArrayList (maximumItems); 81 Iterator iterator = this.contents.iterator(); 82 int i = 0; 83 while (iterator.hasNext() && i++ < maximumItems) 84 { 85 items.add(iterator.next()); 86 } 87 return items; 88 } 89 90 public void purge() 91 { 92 this.contents.clear(); 93 } 94 95 public int size() 96 { 97 return this.contents.size(); 98 } 99 100 public boolean isEmpty() 101 { 102 return this.contents.isEmpty(); 103 } 104 } 105 | Popular Tags |