KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > impl > PriorityQueue


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of JEGG nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.impl;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.SortedMap JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import java.util.Vector JavaDoc;
36
37
38 /**
39  * A queue for objects that can be extracted from the queue
40  * in order of priority.
41  * <p>
42  * This container is not synchronized.
43  */

44 public class PriorityQueue
45 {
46     /** Map holding the set of priority lists. */
47     private SortedMap JavaDoc set = new TreeMap JavaDoc();
48     
49     /**
50      * Add a prioritized object to the queue.
51      * @param p the priority of the object.
52      * @param o the object to add to the queue.
53      */

54     public synchronized void add(final Priority p, final Object JavaDoc o)
55     {
56         List JavaDoc list = (List JavaDoc) set.get(p);
57         if (null == list)
58         {
59             list = new Vector JavaDoc();
60             set.put(p,list);
61         }
62         list.add(o);
63     }
64     
65     /**
66      * Return the next object on the queue in order of priority.
67      * @return the next object on the queue in order of priority.
68      */

69     public synchronized Object JavaDoc next()
70     {
71         for (Iterator JavaDoc it = set.keySet().iterator(); it.hasNext(); )
72         {
73             Priority p = (Priority) it.next();
74             List JavaDoc list = (List JavaDoc) set.get(p);
75             if (null != list && !list.isEmpty())
76             {
77                 return list.remove(0);
78             }
79         }
80         return null;
81     }
82     
83     /**
84      * Return the next object of a specific priority.
85      * @param p the priority of the next object to return.
86      * @return the next object of the specified priority.
87      */

88     public synchronized Object JavaDoc next(Priority p)
89     {
90         List JavaDoc list = (List JavaDoc) set.get(p);
91         if (null != list && ! list.isEmpty())
92         {
93             return list.remove(0);
94         }
95         return null;
96     }
97     
98     /**
99      * return the number of objects remaining on the queue.
100      * @return size of the queue.
101      */

102     public synchronized int size()
103     {
104         int num = 0;
105         for (Iterator JavaDoc it = set.keySet().iterator(); it.hasNext(); )
106         {
107             Priority p = (Priority) it.next();
108             List JavaDoc list = (List JavaDoc) set.get(p);
109             if (null != list)
110             {
111                 num += list.size();
112             }
113         }
114         return num;
115     }
116     
117     // TODO implement 'size(Priority)'
118
// TODO implement 'iterator()'
119

120     /**
121      * Empty the queue.
122      */

123     public synchronized void clear()
124     {
125         set.clear();
126     }
127     
128     /**
129      * Return whether or not the queue is empty.
130      * @return true if the queue is empty; otherwise, false.
131      */

132     public synchronized boolean isEmpty()
133     {
134         return set.isEmpty();
135     }
136 }
137
Popular Tags