001: //$Id: ActionStack.java 256 2006-10-23 21:56:10Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data.processing;
038:
039: import org.apache.log4j.Logger;
040:
041: /**
042: * Stack containing all {@link junitx.ddtunit.data.processing.ActionBase}s
043: * generated during parsing xml structure.
044: *
045: * @author jg
046: */
047: public class ActionStack implements ILinkChangeListener {
048: private Logger log = Logger.getLogger(ActionStack.class);
049:
050: private static final String LF = System
051: .getProperty("line.separator");
052:
053: protected IAction first;
054:
055: protected IAction last;
056:
057: public ActionStack() {
058: // no special initialization
059: }
060:
061: /**
062: * Push new element on stack
063: *
064: * @param action to add
065: */
066: public void push(IAction action) {
067: if (action == null) {
068: throw new IllegalArgumentException(
069: "Provided action should not be null");
070: }
071: if (this .last != null) {
072: action.setPrevious(this .last);
073: this .last.setNext(action);
074: action.setNext(null);
075: this .last = action;
076: } else {
077: this .first = action;
078: this .last = action;
079: action.setPrevious(null);
080: action.setNext(null);
081: }
082: action.registerLinkChangeListener(this );
083: }
084:
085: /**
086: * Retrieve last element on stack and delete it on stack.
087: *
088: * @return last element on stack
089: */
090: public IAction pop() {
091: IAction action = this .last;
092: this .last = action.getPrevious();
093: if (this .last != null) {
094: this .last.setNext(null);
095: }
096: if (action == this .first) {
097: this .first = null;
098: }
099: action.setPrevious(null);
100: action.setNext(null);
101: return action;
102: }
103:
104: /**
105: * Retrieve last element on stack without deleting it from stack
106: *
107: * @return last element of stack
108: */
109: public IAction peek() {
110: return this .last;
111: }
112:
113: /**
114: * check if record stack is empty.
115: *
116: * @return true if no elements on stack
117: */
118: public boolean isEmpty() {
119: boolean check = false;
120: if (this .last == null && this .first == null) {
121: check = true;
122: }
123: return check;
124: }
125:
126: String infoOf() {
127: StringBuffer info = new StringBuffer("ActionStack Info:");
128: if (this .first == null) {
129: info.append(" is Empty");
130: } else {
131: IAction action = this .first;
132: StringBuffer indent = new StringBuffer(" ");
133: while (action != null) {
134: info.append(LF);
135: info.append(indent).append(action.getClass().getName());
136: action = action.getNext();
137: indent.append(" ");
138: }
139: }
140: return info.toString();
141: }
142:
143: public IAction process() {
144: IAction lastAction = peek();
145: log.debug("Processing " + lastAction + " ...");
146: return lastAction.process();
147: }
148: }
|