/*
 * @(#)Memento.java	1.0 11-Apr-07
 *
 * IIT Bombay, 
 * Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.
 */
package classes.main;

import interfaces.IShape;

import java.util.ArrayList;

/**
 * Stores the internal state of the objects in the command container class
 *
 * @author  Prathab, Mukesh
 * @version 1.0, 03-Apr-07
 */
public class Memento {
	
	int objectCount;
	ArrayList lastCreatedShapeList = new ArrayList();
	
	/**
	 * Set the current buffer state 
	 * @param lastCreatedShape last created <tt>IShape</tt> object
	 */
	public void setState(IShape lastCreatedShape) {
		if(lastCreatedShape != null) lastCreatedShapeList.add(lastCreatedShape.clone());
		else lastCreatedShapeList.add(lastCreatedShape);
		objectCount++;
	}
	
	/**
	 * Returns the previous state (undo operation)
	 * @return stored <tt>IShape</tt> object
	 */
	public IShape getState() {
		if(objectCount == 0) return null;
		objectCount--;
		Object obj=lastCreatedShapeList.get(objectCount);
		if(obj != null) return (IShape) obj;
		else return null;
	}
}
