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.queue;
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 import jegg.Priority;
38
39 /***
40 * A queue for objects that can be extracted from the queue
41 * in order of priority.
42 * <p>
43 * This container is not synchronized.
44 */
45 public class PriorityQueue
46 {
47 /*** Map holding the set of priority lists. */
48 private SortedMap set = new TreeMap();
49
50 /***
51 * Add a prioritized object to the queue.
52 * @param p the priority of the object.
53 * @param o the object to add to the queue.
54 */
55 public synchronized void add(final Priority p, final Object o)
56 {
57 List list = (List) set.get(p);
58 if (null == list)
59 {
60 list = new Vector();
61 set.put(p,list);
62 }
63 list.add(o);
64 }
65
66 /***
67 * Return the next object on the queue in order of priority.
68 * @return the next object on the queue in order of priority.
69 */
70 public synchronized Object next()
71 {
72 for (Iterator it = set.keySet().iterator(); it.hasNext(); )
73 {
74 Priority p = (Priority) it.next();
75 List list = (List) set.get(p);
76 if (null != list && !list.isEmpty())
77 {
78 return list.remove(0);
79 }
80 }
81 return null;
82 }
83
84 /***
85 * Return the next object of a specific priority.
86 * @param p the priority of the next object to return.
87 * @return the next object of the specified priority.
88 */
89 public synchronized Object next(Priority p)
90 {
91 List list = (List) set.get(p);
92 if (null != list && ! list.isEmpty())
93 {
94 return list.remove(0);
95 }
96 return null;
97 }
98
99 /***
100 * return the number of objects remaining on the queue.
101 * @return size of the queue.
102 */
103 public synchronized int size()
104 {
105 int num = 0;
106 for (Iterator it = set.keySet().iterator(); it.hasNext(); )
107 {
108 Priority p = (Priority) it.next();
109 List list = (List) set.get(p);
110 if (null != list)
111 {
112 num += list.size();
113 }
114 }
115 return num;
116 }
117
118 // TODO implement 'size(Priority)'
119 // TODO implement 'iterator()'
120
121 /***
122 * Empty the queue.
123 */
124 public synchronized void clear()
125 {
126 set.clear();
127 }
128
129 /***
130 * Return whether or not the queue is empty.
131 * @return true if the queue is empty; otherwise, false.
132 */
133 public synchronized boolean isEmpty()
134 {
135 return set.isEmpty();
136 }
137 }
This page was automatically generated by Maven