FileDocCategorySizeDatePackage
CharQueue.javaAPI DocGlassfish v2 API2716Wed Aug 30 15:34:04 BST 2006persistence.antlr

CharQueue

public class CharQueue extends Object
A circular buffer object used by CharBuffer

Fields Summary
protected char[]
buffer
Physical circular buffer of tokens
private int
sizeLessOne
buffer.length-1 for quick modulos
private int
offset
physical index of front token
protected int
nbrEntries
number of tokens in the queue
Constructors Summary
public CharQueue(int minSize)

        // Find first power of 2 >= to requested size
        int size;
        if ( minSize<0 ) {
            init(16); // pick some value for them
            return;
        }
        // check for overflow
        if ( minSize>=(Integer.MAX_VALUE/2) ) {
            init(Integer.MAX_VALUE); // wow that's big.
            return;
        }
        for (size = 2; size < minSize; size *= 2) {
        }
        init(size);
    
Methods Summary
public final voidappend(char tok)
Add token to end of the queue

param
tok The token to add

        if (nbrEntries == buffer.length) {
            expand();
        }
        buffer[(offset + nbrEntries) & sizeLessOne] = tok;
        nbrEntries++;
    
public final charelementAt(int idx)
Fetch a token from the queue by index

param
idx The index of the token to fetch, where zero is the token at the front of the queue

        return buffer[(offset + idx) & sizeLessOne];
    
private final voidexpand()
Expand the token buffer by doubling its capacity

        char[] newBuffer = new char[buffer.length * 2];
        // Copy the contents to the new buffer
        // Note that this will store the first logical item in the
        // first physical array element.
        for (int i = 0; i < buffer.length; i++) {
            newBuffer[i] = elementAt(i);
        }
        // Re-initialize with new contents, keep old nbrEntries
        buffer = newBuffer;
        sizeLessOne = buffer.length - 1;
        offset = 0;
    
public voidinit(int size)
Initialize the queue.

param
size The initial size of the queue

        // Allocate buffer
        buffer = new char[size];
        // Other initialization
        sizeLessOne = size - 1;
        offset = 0;
        nbrEntries = 0;
    
public final voidremoveFirst()
Remove char from front of queue

        offset = (offset + 1) & sizeLessOne;
        nbrEntries--;
    
public final voidreset()
Clear the queue. Leaving the previous buffer alone.

        offset = 0;
        nbrEntries = 0;