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.
* 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 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(); } /** * 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 addAction * 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 Set of all the actions present in the entry. */ public Set 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 String in the form "MaskHistoryEntry 'name' Actions: actions", where name is the name of the entry * and actions is a list of [action, value] pairs. */ @Override public String toString() { StringBuilder str = new StringBuilder("MaskHistoryEntry '" + name + "', Actions: "); for ( Iterator 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(); } }