package edu.princeton.cs.algs4.growingtree.framework; /* * @(#)WaitingActionList.java * * Last Modified: 9/15/01 */ import java.awt.geom.AffineTransform; import java.util.Collection; import java.util.LinkedList; /** * Linked list implementation of the List interface containing String actions and accompanying Object elements. Implements all * optional list operations, but only permits a String with Object insertion. In addition to implementing the List interface, * the WaitingActionList class extends the uniformly named methods within LinkedList to * get, remove and insert an element at the * beginning and end of the list. These operations allow this list to be * used as a stack, queue, or double-ended queue (deque).

* * The primary purpose of the class is for the development of nextAction, * which calls the waitingAction method for the headelement passed and the * next action and accompanying Object element. * * @author Corey Sanders * @version 1.4 9/15/01 */ public class WaitingActionList

{ /** * LinkedList holding the String actions for the WaitingActions. */ private LinkedList actions; /** * LinkedList holding the Object elements for the WaitingActions. */ private LinkedList> elements; /** * Constructs an empty list. */ public WaitingActionList() { actions = new LinkedList(); elements = new LinkedList>(); } /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param actions the collection whose string actions are to be placed into this list as the String Actions. * @param elements the collection whose elements are to be places into the list as the Object elements. */ public WaitingActionList(Collection actions, Collection> elements) { this.actions = new LinkedList(actions); this.elements = new LinkedList>(elements); } /** * Is the list of actions empty? (Since boths lists have the same number * of elements, just check actions list.) */ public boolean isEmpty() { return actions.isEmpty(); } /** * Appends the given String at the end of this list. * * @param action the String representing an action, to be inserted at the beginning of this list. * * @return true (as per the general contract of * Collection.add). */ public boolean add(String action) { return this.add(action, new ActionElementType

(new Integer(0))); } /** * Appends the given String and Object at the end of this list. * * @param action the String representing an action, to be inserted at the beginning of this list. * @param element the Object to be inserted in reference to the action. * * @return true (as per the general contract of * Collection.add). */ public boolean add(String action, ActionElementType

element) { elements.add(element); return actions.add(action); } /** * Inserts the specified String and Object at the specified position in this list. * Shifts the String and Object currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index the integer index representing the location to add the action and element. * @param action the String representing an action, to be inserted at the beginning of this list. * @param element the Object to be inserted in reference to the action. * * @throws IndexOutOfBoundsException if the specified index is out of * range (index < 0 || index > size()). */ public void add(int index, String action, ActionElementType

element) throws IndexOutOfBoundsException { elements.add(index, element); actions.add(index, action); } /** * Inserts the given String and Object at the beggining of this list. * * @param action the String representing an action, to be inserted at the beginning of this list. * @param element the Object to be inserted in reference to the action. */ public void addFirst(String action, ActionElementType

element) { actions.addFirst(action); elements.addFirst(element); } /** * Appends the given String and Object at the end of this list. * * @param action the String representing an action, to be inserted at the end of this list. * @param element the Object to be inserted in reference to the action. */ public void addLast(String action, ActionElementType

element) { actions.addLast(action); elements.addLast(element); } /** * Gets the given String of the action at the beggining of this list. * * @return action the String representing the first action. */ public String getFirstAction() { return actions.getFirst(); } /** * Calls the next action of the ObjectHead headelement, using the waitingAction command. * The call to waitingAction always passes the element, even if the element is unnecssary to * that operations. * * @param headTree the TreeHead element of the Tree to which the action occurs. */ public void nextAction(TreeHead

headTree) { String action = actions.removeFirst(); ActionElementType

element = elements.removeFirst(); headTree.waitingAction(action, element); } }