001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.util;
019:
020: public class ExecutionStack implements Stack {
021: private StackElement element;
022: private int size = 0;
023:
024: public boolean isEmpty() {
025: return size == 0;
026: }
027:
028: public void add(Object o) {
029: size++;
030: StackElement el = element;
031: if (el != null) {
032: while (el.next != null) {
033: el = el.next;
034: }
035: el.next = new StackElement(null, o);
036: } else {
037: element = new StackElement(null, o);
038: }
039: }
040:
041: public void push(Object o) {
042: size++;
043: element = new StackElement(element, o);
044: }
045:
046: public Object pushAndPeek(Object o) {
047: size++;
048: element = new StackElement(element, o);
049: return o;
050: }
051:
052: public void push(Object obj1, Object obj2) {
053: size += 2;
054: element = new StackElement(new StackElement(element, obj1),
055: obj2);
056: }
057:
058: public void push(Object obj1, Object obj2, Object obj3) {
059: size += 3;
060: element = new StackElement(new StackElement(new StackElement(
061: element, obj1), obj2), obj3);
062: }
063:
064: public Object peek() {
065: if (size == 0)
066: return null;
067: else
068: return element.value;
069: }
070:
071: public Object pop() {
072: if (size-- == 0)
073: return null;
074: try {
075: return element.value;
076: } finally {
077: element = element.next;
078: }
079: }
080:
081: public void discard() {
082: if (size != 0) {
083: size--;
084: element = element.next;
085: }
086: }
087:
088: public int size() {
089: return size;
090: }
091:
092: public void clear() {
093: size = 0;
094: element = null;
095: }
096:
097: public void showStack() {
098: StackElement el = element;
099: do {
100: System.out.println("->" + el.value);
101: } while ((el = el.next) != null);
102: }
103: }
|