FreeWRL / FreeX3D 4.3.0
MessageQueue.java
1// copyright (c) 1997,1998 stephen f. white
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2, or (at your option)
6// any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; see the file COPYING. If not, write to
15// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16package sai.eai;
17
18class MessageQueue {
19 private Message head;
20 private Message tail;
21
22 public MessageQueue() {
23 head = tail = null;
24 }
25
26 public synchronized void enqueue(Message msg) {
27 msg.next = head;
28 msg.prev = null;
29 if (head == null) {
30 tail = msg;
31 } else {
32 head.prev = msg;
33 }
34 head = msg;
35 }
36
37 public synchronized void enqueueUnique(Message msg) {
38 for (Message m = head; m != null; m = m.next) {
39 if (m.id == msg.id && m.field == msg.field) {
40 m.value = msg.value;
41 return;
42 }
43 }
44 enqueue(msg);
45 }
46
47 public synchronized Message dequeue() {
48 if (tail == null) return null;
49 Message msg = tail;
50 tail = tail.prev;
51 if (tail == null) {
52 head = null;
53 } else {
54 tail.next = null;
55 }
56 msg.prev = msg.next = null;
57 return msg;
58 }
59
60 public boolean isEmpty() {
61 return head == null;
62 }
63}