01: /*
02: * (C) Copyright 2002-2003, Andy Clark. All rights reserved.
03: *
04: * This file is distributed under an Apache style license. Please
05: * refer to the LICENSE file for specific details.
06: */
07:
08: package org.cyberneko.pull.util;
09:
10: import org.cyberneko.pull.XMLEvent;
11:
12: /**
13: * A general purpose queue for pull parser event objects.
14: *
15: * @author Andy Clark
16: *
17: * @version $Id$
18: */
19: public class EventQueue {
20:
21: //
22: // Data
23: //
24:
25: /** The size of the queue. */
26: protected int fSize;
27:
28: /** The head of the queue. */
29: protected XMLEvent fHead;
30:
31: /** The tail of the queue. */
32: protected XMLEvent fTail;
33:
34: //
35: // Public methods
36: //
37:
38: /** Returns the size of the queue. */
39: public int size() {
40: return fSize;
41: } // size():int
42:
43: /** Clears the queue. */
44: public void clear() {
45: fSize = 0;
46: fHead = null;
47: fTail = null;
48: } // clear()
49:
50: /** Returns true if the queue is empty. */
51: public boolean isEmpty() {
52: return fHead == null;
53: } // isEmpty():boolean
54:
55: /** Adds an event to the queue. */
56: public void enqueue(XMLEvent event) {
57: if (fHead == null) {
58: fHead = event;
59: } else {
60: fTail.next = event;
61: }
62: fTail = event;
63: event.next = null;
64: fSize++;
65: } // enqueue(XMLEvent)
66:
67: /**
68: * Removes and returns an event from the queue, or null if the queue
69: * is empty.
70: */
71: public XMLEvent dequeue() {
72: XMLEvent event = fHead;
73: if (fHead != null) {
74: fSize--;
75: fHead = fHead.next;
76: event.next = null;
77: }
78: return event;
79: } // dequeue():XMLEvent
80:
81: //
82: // Object methods
83: //
84:
85: /** Returns a String representation of this object. */
86: public String toString() {
87: StringBuffer str = new StringBuffer();
88: XMLEvent event = fHead;
89: while (event != null) {
90: str.append(event);
91: event = event.next;
92: if (event != null) {
93: str.append(", ");
94: }
95: }
96: return str.toString();
97: } // toString():String
98:
99: } // class EventQueue
|