MaskHistoryEntry.java 2.86 KB
package osp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Represents an entry in the history with a list of actions that have to be undone with the entry.<br/>
 * Each action should appear only once per entry, even if there is no limitation.
 * @see MaskHistory
 */
public class MaskHistoryEntry
{
  /**
   * The name of the entry.
   */
  private final String name;
  
  /**
   * The list of pairs [action, value] corresponding to the entry.
   */
  private HashMap<String, Object> actions;
  
  /**
   * Constructs an entry with a given name and an empty list of actions.
   * @param entryName The name of the entry.
   */
  public MaskHistoryEntry(String entryName)
  {
    name = entryName;
    actions = new HashMap<String, Object>();
  }
  
  /**
   * Get the name of the entry.
   * @return The name of the entry.
   */
  public String getName()
  {
    return name;
  }
  
  /**
   * Put a new action in this entry. This should not be used directly.
   * MaskHistory's <a href="./MaskHistory.html#addAction(java.lang.String, java.lang.Object)">addAction</a>
   * method should be called instead.
   * @param action The action to add to the entry.
   * @param value The value to be restored by the action.
   */
  void putAction(String action, Object value)
  {
    actions.put(action, value);
  }
  
  /**
   * Get the list of actions in this entry.
   * @return A <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Set.html">Set</a> of all the actions present in the entry.
   */
  public Set<String> getActions()
  {
    return actions.keySet();
  }

  /**
   * Get the value corresponding to an action.
   * @param action The action to get the value for.
   * @return The value that corresponds to the given action.
   */
  public Object getValue(String action)
  {
    return actions.get(action);
  }

  /**
   * Delete a given action in the entry.
   * @param action The action to delete.
   */
  public void deleteAction(String action)
  {
    actions.remove(action);
  }
  
  /**
   * Returns a <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html">String</a> in the form "MaskHistoryEntry '<i>name</i>' Actions: <i>actions</i>", where <i>name</i> is the name of the entry
   * and <i>actions</i> is a list of [<i>action</i>, <i>value</i>] pairs.
   */
  @Override
public String toString()
  { 
    StringBuilder str = new StringBuilder("MaskHistoryEntry '" + name + "', Actions: ");
    for ( Iterator<String> itr = actions.keySet().iterator(); itr.hasNext(); )
    {
      String action = itr.next();
      str.append("[");
      str.append(action);
      str.append(", ");
      str.append( actions.get(action) );
      str.append( "]" );
    }
    return str.toString();
  }

  /**
   * Tests if the entry is empty.
   * @return true if the list of actions is empty.
   */
  public boolean isEmpty()
  {
    return actions.isEmpty();
  }
}