KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > util > SortedSetPriorityQueue


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.util;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.SortedSet JavaDoc;
14 import java.util.TreeSet JavaDoc;
15
16 /**
17  *
18  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
19  * @version $Revision: 1.1.1.1 $ $Date: 2003/06/07 02:18:28 $
20  */

21 public class SortedSetPriorityQueue implements PriorityQueue
22 {
23     private SortedSet JavaDoc contents = null;
24
25     public SortedSetPriorityQueue()
26     {
27         this.contents = new TreeSet JavaDoc();
28     }
29
30     public SortedSetPriorityQueue(Comparator JavaDoc comparator)
31     {
32         this.contents = new TreeSet JavaDoc(comparator);
33     }
34
35     public void enqueue(Object JavaDoc object)
36     {
37         this.contents.add(object);
38     }
39
40     public void enqueue(Collection JavaDoc collection)
41     {
42         this.contents.addAll(collection);
43     }
44
45     public Object JavaDoc dequeue()
46     {
47         if (!this.contents.isEmpty())
48         {
49             Iterator JavaDoc iterator = this.contents.iterator();
50             Object JavaDoc object = iterator.next();
51             iterator.remove();
52             return object;
53         }
54         else
55         {
56             return null;
57         }
58     }
59
60     public Collection JavaDoc dequeue(int maximumItems)
61     {
62         Collection JavaDoc items = new ArrayList JavaDoc(maximumItems);
63         Iterator JavaDoc 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 JavaDoc peek()
74     {
75         return this.contents.first();
76     }
77
78     public Collection JavaDoc peek(int maximumItems)
79     {
80         Collection JavaDoc items = new ArrayList JavaDoc(maximumItems);
81         Iterator JavaDoc 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