1
2
3
4
5
6
7
8
9
10
11
12
13 package nl.tudelft.simulation.messaging.queues;
14
15 import java.util.Comparator;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
18
19 import nl.tudelft.simulation.messaging.Message;
20
21 /***
22 * The MessageQueue sorts messages on priority, and within the same priority on
23 * the moment the messages entered the queue.
24 *
25 * <br>
26 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
27 * Delft, the Netherlands. All rights reserved.
28 *
29 * See for project information <a href="http://www.simulation.tudelft.nl/">
30 * www.simulation.tudelft.nl </a>.
31 *
32 * The source code and binary code of this software is proprietary information
33 * of Delft University of Technology.
34 *
35 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
36 * Jacobs </a>, <a
37 * href="mailto:s.p.a.vanhouten@tbm.tudelft.nl">Stijn-Pieter van Houten
38 * </a>, <a
39 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
40 * Verbraeck </a>
41 * @version $$Revision: 1.3 $$ $$Date: 2005/04/08 11:29:13 $$
42 */
43 public class MessageQueue implements MessageQueueInterface
44 {
45 /*** the serial version uid */
46 private static final long serialVersionUID = 12L;
47
48 /*** The set of messages */
49 private SortedSet messages;
50
51 /*** The message counter to use for the messages in the queue */
52 private static long messageCounter = 0;
53
54 /***
55 * Make a new MessageQueue
56 *
57 * @param comparator (or chain) the comparator to use
58 */
59 public MessageQueue(final Comparator comparator)
60 {
61 super();
62 this.messages = new TreeSet(comparator);
63 }
64
65 /***
66 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface
67 * #add(nl.tudelft.simulation.messaging.Message)
68 */
69 public void add(final Message message)
70 {
71 message.setId(messageCounter++);
72 this.messages.add(message);
73 }
74
75 /***
76 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface
77 * #remove(nl.tudelft.simulation.messaging.Message)
78 */
79 public void remove(final Message message)
80 {
81 this.messages.remove(message);
82 }
83
84 /***
85 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface#first()
86 */
87 public Message first()
88 {
89 return (Message) this.messages.first();
90 }
91
92 /***
93 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface#removeFirst()
94 */
95 public Message removeFirst()
96 {
97 Message message = (Message) this.messages.first();
98 this.messages.remove(message);
99 return message;
100 }
101
102 /***
103 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface#size()
104 */
105 public int size()
106 {
107 return this.messages.size();
108 }
109
110 /***
111 * @see nl.tudelft.simulation.messaging.queues.MessageQueueInterface#isEmpty()
112 */
113 public boolean isEmpty()
114 {
115 return this.messages.isEmpty();
116 }
117 }