KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > parser > CharQueue


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.template.parser;
19
20
21 /**
22  * Class CharQueue
23  *
24  *
25  * @author
26  * @version %I%, %G%
27  */

28 public class CharQueue {
29
30    /** Field buffer */
31    protected char[] buffer;
32
33    // buffer.length-1 for quick modulous
34

35    /** Field sizeLessOne */
36    protected int sizeLessOne;
37
38    // physical index of front token
39

40    /** Field offset */
41    protected int offset;
42
43    // number of characters in the queue
44

45    /** Field nbrEntries */
46    protected int nbrEntries;
47
48
49    /**
50     * Constructor CharQueue
51     *
52     *
53     * @param minSize
54     *
55     */

56    public CharQueue(int minSize) {
57       // Find first power of 2 >= to requested size
58
int size;
59       for (size = 2; size < minSize; size *= 2) ;
60       init(size);
61    }
62
63
64    /**
65     * Add token to end of the queue
66     * @param tok The token to add
67     */

68    public final void append(char tok) {
69       if (nbrEntries == buffer.length) {
70          expand();
71       }
72       buffer[(offset + nbrEntries) & sizeLessOne] = tok;
73       nbrEntries++;
74    }
75
76
77    /**
78     * Fetch a token from the queue by index
79     * @param idx The index of the token to fetch, where zero is the token at the front of the queue
80     *
81     * @return
82     */

83    public final char elementAt(int idx) {
84       return buffer[(offset + idx) & sizeLessOne];
85    }
86
87
88    /** Expand the token buffer by doubling its capacity */
89    private final void expand() {
90       char[] newBuffer = new char[buffer.length * 2];
91
92       // Copy the contents to the new buffer
93
// Note that this will store the first logical item in the
94
// first physical array element.
95
for (int i = 0; i < buffer.length; i++) {
96          newBuffer[i] = elementAt(i);
97       }
98
99       // Re-initialize with new contents, keep old nbrEntries
100
buffer = newBuffer;
101       sizeLessOne = buffer.length - 1;
102       offset = 0;
103    }
104
105
106    /**
107     * Initialize the queue.
108     * @param size The initial size of the queue
109     */

110    private final void init(int size) {
111       buffer = new char[size];
112       sizeLessOne = size - 1;
113       offset = 0;
114       nbrEntries = 0;
115    }
116
117
118    /** Remove char from front of queue */
119    public final void removeFirst() {
120       offset = (offset + 1) & sizeLessOne;
121       nbrEntries--;
122    }
123 }
Popular Tags