Source code: mobile/protocol/FrameQueue.java
1 package mobile.protocol;
2
3
4 /**
5 * <p>
6 * The FrameQueue class implements a queue of Frame objects. It provides the
7 * queue interface, which is not available in the MIDP.
8 * </p>
9 * @author Pawel Koziol
10 */
11
12 public class FrameQueue{
13
14 /**<p>
15 Checks if the queue is empty
16 </p>
17 <p>
18 @return true if the queue is empty, false otherwise
19 </p>
20 */
21
22 synchronized public boolean empty(){
23 return (size == 0);
24 }
25 /**<p>
26 Adds a new Frame at the end of the queue
27 </p>
28 <p>
29 @param newFrame the frame to be added
30 </p>
31 */
32 synchronized public void add(Frame newFrame){
33 size++;
34 if(head == null){
35 head = newFrame;
36 newFrame.setNext(newFrame);
37 newFrame.setPrev(newFrame);
38 }
39 else{
40 newFrame.setPrev(head.getPrev());
41 head.getPrev().setNext(newFrame);
42 newFrame.setNext(head);
43 head.setPrev(newFrame);
44 }
45 }
46
47 /**<p>
48 Gets a Frame from the head of the queue but does not remove it from
49 the queue
50 </p>
51 <p>
52 @return the frame from the head of the queue or null when the queue is empty
53 </p>
54 */
55 synchronized public Frame get(){
56 return head;
57 }
58
59 /**
60 <p>
61 Gets a Frame from the head of the queue and removes it from the queue
62 </p>
63 @return @return the frame from the head of the queue or null when the queue is empty
64 */
65 synchronized public Frame remove(){
66 if(head == null) return null;
67 else{
68 Frame ret;
69 ret = head;
70 if(size == 1) head = null;
71 else{
72 head.getPrev().setNext(head.getNext());
73 head.getNext().setPrev(head.getPrev());
74 head = head.getNext();
75 }
76 size--;
77 return ret;
78 }
79 }
80
81 /**
82 <p>
83 The head of the cyclic linked list containing the queue
84 <p>
85 */
86 private Frame head = null;
87 /**
88 <p>
89 Number of frames contained in the queue
90 <p>
91 */
92
93 private int size = 0;
94
95 /**
96 * Prints the ids of messages contained in this queue. Used for debugging.
97 */
98 public void print(){
99 if(head == null) System.out.println("empty");
100 else{
101 Frame first = head, tmp = head.getNext();
102 System.out.println("\\ \n" + head.id);
103 // while(tmp.id != head.id) {
104 // System.out.println(tmp.id);
105 // tmp = tmp.getNext();
106 // }
107 while(tmp != head) {
108 System.out.println(tmp.id);
109 tmp = tmp.getNext();
110 }
111 System.out.println("// \n");
112
113 }
114 }
115 }
116
117
118
119
120