1 //$Id: ActionStack.java 351 2008-08-14 20:20:56Z jg_hamburg $ 2 /******************************************************************************** 3 * DDTUnit, a Datadriven Approach to Unit- and Moduletesting 4 * Copyright (c) 2004, Joerg and Kai Gellien 5 * All rights reserved. 6 * 7 * The Software is provided under the terms of the Common Public License 1.0 8 * as provided with the distribution of DDTUnit in the file cpl-v10.html. 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * + Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * + Redistributions in binary form must reproduce the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer in the documentation and/or other materials provided 19 * with the distribution. 20 * 21 * + Neither the name of the authors or DDTUnit, nor the 22 * names of its contributors may be used to endorse or promote 23 * products derived from this software without specific prior 24 * written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 ********************************************************************************/ 38 package junitx.ddtunit.data.processing; 39 40 import org.slf4j.Logger; 41 import org.slf4j.LoggerFactory; 42 43 /** 44 * Stack containing all {@link junitx.ddtunit.data.processing.ActionBase}s 45 * generated during parsing xml structure. 46 * 47 * @author jg 48 */ 49 public class ActionStack implements ILinkChangeListener { 50 private Logger log = LoggerFactory.getLogger(ActionStack.class); 51 52 private static final String LF = System.getProperty("line.separator"); 53 54 protected IAction first; 55 56 protected IAction last; 57 58 public ActionStack() { 59 // no special initialization 60 } 61 62 /** 63 * Push new element on stack 64 * 65 * @param action to add 66 */ 67 public void push(IAction action) { 68 if (action == null) { 69 throw new IllegalArgumentException( 70 "Provided action should not be null"); 71 } 72 if (this.last != null) { 73 action.setPrevious(this.last); 74 this.last.setNext(action); 75 action.setNext(null); 76 this.last = action; 77 } else { 78 this.first = action; 79 this.last = action; 80 action.setPrevious(null); 81 action.setNext(null); 82 } 83 action.registerLinkChangeListener(this); 84 } 85 86 /** 87 * Retrieve last element on stack and delete it on stack. 88 * 89 * @return last element on stack 90 */ 91 public IAction pop() { 92 IAction action = this.last; 93 this.last = action.getPrevious(); 94 if (this.last != null) { 95 this.last.setNext(null); 96 } 97 if (action == this.first) { 98 this.first = null; 99 } 100 action.setPrevious(null); 101 action.setNext(null); 102 return action; 103 } 104 105 /** 106 * Retrieve last element on stack without deleting it from stack 107 * 108 * @return last element of stack 109 */ 110 public IAction peek() { 111 return this.last; 112 } 113 114 /** 115 * check if record stack is empty. 116 * 117 * @return true if no elements on stack 118 */ 119 public boolean isEmpty() { 120 boolean check = false; 121 if (this.last == null && this.first == null) { 122 check = true; 123 } 124 return check; 125 } 126 127 String infoOf() { 128 StringBuffer info = new StringBuffer("ActionStack Info:"); 129 if (this.first == null) { 130 info.append(" is Empty"); 131 } else { 132 IAction action = this.first; 133 StringBuffer indent = new StringBuffer(" "); 134 while (action != null) { 135 info.append(LF); 136 info.append(indent).append(action.getClass().getName()); 137 action = action.getNext(); 138 indent.append(" "); 139 } 140 } 141 return info.toString(); 142 } 143 144 public IAction process() { 145 IAction lastAction = peek(); 146 log.debug("Processing " + lastAction + " ..."); 147 return lastAction.process(); 148 } 149 }