📄 actionlist.java
字号:
package piy;
import java.util.*;
import java.io.*;
/**
* Represents a list of Actions which are executed sequentially.
* @author David Vivash
* @version 1.0, 19/11/00
*/
public class ActionList implements Serializable
{
private ArrayList actions = new ArrayList(4); //initial size set to 4
private HashMap returns = new HashMap(4);
/**
* Add an action to the end of the list
* @param action the action to add
*/
public void add(PIYAction action) { actions.add(action); }
/**
* Insert an action into a specific position. ie. 0 = start of the list, size() = end of list.
* @param index a value between 0 and size() inclusive
* @param action the action to insert
* @throws IndexOutOfBoundsException if size() < index < 0
*/
public void add(int index, PIYAction action) throws IndexOutOfBoundsException
{ actions.add(index, action); }
/**
* Removes the action at the specified index from the list. Subsequent actions are shifted left (ie.
* their indices ar decremented.
* @param index a value between 0 and size()-1 represting the index of the action to be removed
* @throws IndexOutOfBoundsException if size() <= index < 0
*/
public void remove(int index) throws IndexOutOfBoundsException {
returns.remove(get(index));
actions.remove(index);
}
/**
* Removes the specefied action from this list, providing the action is defined in this list.
* @param action the action to remove
* @return the position in the list that the action was at before it was removed, or -1 if the
* action wasn't in the list
*/
public int removeAction(PIYAction action) {
int index = getIndex(action);
if (index > -1) remove(index);
return index;
}
/**
* Retrieves the number of actions stored in the actionlist.
* @return the length of this actionlist (number of actions)
*/
public int size() { return actions.size(); }
/**
* Get the action at position pos.
* @param pos the index of the item in the list to retrieve (numbering starts at zero)
* @return the action at position pos (could be null)
* @throws IndexOutOfBoundsException if size() <= pos > 0
*/
public PIYAction get(int pos) throws IndexOutOfBoundsException
{ return (PIYAction)actions.get(pos); }
/**
* Gets the index of the specified action, ie. where in the list the specified action is located.
* If the action is not found in the list, -1 is returned.
* @param action the action to find
* @return the position of the action in the list, or -1 if the action is not in the list
*/
public int getIndex(PIYAction action) {
for (int i=0; i<size(); i++)
if (actions.get(i) == action) return i;
return -1;
}
/**
* Gets where the specified action sends its return value to.
* @param action the action returning a value
* @return the Property placeholder where the action value will be stored
*/
public Property getReturn(PIYAction action) {
return (Property)returns.get(action);
}
/**
* Set the place where a specified action will send its result value.
* @param action the action returning the value
* @param property the place the value will be stored
*/
public void setReturn(PIYAction action, Property property) {
returns.put(action, property);
}
/**
* executes the actions in the action list sequentially.
*/
public void execute()
{
Iterator i = actions.iterator();
PIYAction action = null;
Object returnValue = null;
Property returnPlace = null;
while (i.hasNext()) {
action = (PIYAction)i.next();
if (action != null) {
returnValue = action.execute();
if ( (returnPlace = (Property)returns.get(action)) != null)
returnPlace.setValue(returnValue);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -