001 /*
002 * Copyright 1999-2001,2004 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.apache.commons.workflow.core;
018
019
020 import java.util.EmptyStackException;
021 import org.apache.commons.workflow.Context;
022 import org.apache.commons.workflow.StepException;
023 import org.apache.commons.workflow.base.BaseStep;
024
025
026 /**
027 * <p>Swap the positions of the top two values on the evaluation stack.</p>
028 *
029 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
030 * @author Craig R. McClanahan
031 */
032
033 public class SwapStep extends BaseStep {
034
035
036 // ----------------------------------------------------------= Constructors
037
038
039 /**
040 * Construct a default instance of this Step.
041 */
042 public SwapStep() {
043
044 super();
045
046 }
047
048
049 /**
050 * Construct an instance of this Step with the specified identifier.
051 *
052 * @param id Step identifier
053 */
054 public SwapStep(String id) {
055
056 super();
057 setId(id);
058
059 }
060
061
062 // --------------------------------------------------------- Public Methods
063
064
065 /**
066 * Perform the executable actions related to this Step, in the context of
067 * the specified Context.
068 *
069 * @param context The Context that is tracking our execution state
070 *
071 * @exception StepException if a processing error has occurred
072 */
073 public void execute(Context context) throws StepException {
074
075 // Retrieve the top value from the evaluation stack
076 Object oldTop = null;
077 try {
078 oldTop = context.pop();
079 } catch (EmptyStackException e) {
080 throw new StepException("Evaluation stack is empty", e, this);
081 }
082
083 // Retrieve the next value from the evaluation stack
084 Object oldBottom = null;
085 try {
086 oldBottom = context.pop();
087 } catch (EmptyStackException e) {
088 throw new StepException("Evaluation stack is empty", e, this);
089 }
090
091 // Push the items back onto the evaluation stack in reverse order
092 context.push(oldTop);
093 context.push(oldBottom);
094
095 }
096
097
098 }