001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.format.util;
023:
024: import java.util.Collections;
025: import java.util.Iterator;
026:
027: /**
028: * The goal of this class is to ensure that any event with open/close well formdness semantics are respected.
029: * <p/>
030: * This class maintain a stack of keys.
031: * <p/>
032: * To be operational this class must be subclassed to provide semantics that the user wants to give to the key
033: * identities.
034: *
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8784 $
037: */
038: public abstract class Stack {
039:
040: /** The stack pointer which always points the first available element. */
041: protected int ptr = 0;
042:
043: /** The stack holder. */
044: protected Key[] stack;
045:
046: /** The reusable iterator. */
047: private KeyIterator iterator = new KeyIterator();
048:
049: /** Create a new stack with a specified depth. */
050: public Stack(int initalCapacity) {
051: this .stack = new Key[initalCapacity];
052: for (int i = 0; i < stack.length; i++) {
053: this .stack[i] = createKey();
054: }
055: }
056:
057: /** Reset the stack state. */
058: public final void reset() {
059: ptr = 0;
060: }
061:
062: /** Push a key on the stack. */
063: public final Key push() {
064: if (ptr == stack.length) {
065: enlarge();
066: }
067: return stack[ptr++];
068: }
069:
070: /** Peek a key on the stack. */
071: public Key peek(int level) {
072: level = ptr - 1 - level;
073: return level >= 0 ? stack[level] : null;
074: }
075:
076: /** Pop keys until it finds the good one. */
077: public final Iterator pop(Key candidate) {
078: if (ptr > 0) {
079: iterator.from = ptr - 1;
080: for (Key key = stack[--ptr]; !equals(candidate, key)
081: && ptr >= 1; key = stack[--ptr]) {
082: ;
083: }
084: iterator.to = ptr - 1;
085: return iterator;
086: } else {
087: // This is a warn because it is not possible to pop with empty stack
088: return Collections.EMPTY_LIST.iterator();
089: }
090: }
091:
092: /** The implementation must provide a reusable key. */
093: protected abstract Key createKey();
094:
095: /** The implementation must test keys equality. */
096: protected abstract boolean equals(Key key1, Key key2);
097:
098: /** Enlarge the key stack. For now it simply add 3 keys to the actual length */
099: protected void enlarge() {
100: Key[] tmp = new Key[stack.length + 3];
101: System.arraycopy(stack, 0, tmp, 0, stack.length);
102: for (int i = stack.length; i < tmp.length; i++) {
103: tmp[i] = createKey();
104: }
105: stack = tmp;
106: }
107:
108: /** Iterates over the poped keys. */
109: public class KeyIterator implements Iterator {
110: private int from;
111: private int to;
112:
113: public boolean hasNext() {
114: return from > to;
115: }
116:
117: public Object next() {
118: return stack[from--];
119: }
120:
121: public void remove() {
122: throw new UnsupportedOperationException();
123: }
124: }
125:
126: /** A key for the stack. */
127: public interface Key {
128: }
129:
130: public String toString() {
131: StringBuffer buffer = new StringBuffer();
132: buffer.append("[stack[");
133: for (int i = 0; i < ptr; i++) {
134: Stack.Key key = stack[i];
135: buffer.append(i == 0 ? "" : ",").append(key);
136: }
137: buffer.append("]]");
138: return buffer.toString();
139: }
140: }
|