<commit_before>package edu.psu.sweng500.team8.play;

import java.util.ArrayDeque;
import java.util.Deque;

import edu.psu.sweng500.team8.coreDataStructures.CellGrid;

public class ActionManager {

	/* If limit the size, we need a custom implementation */
	private Deque<SudokuAction> undoStack = new ArrayDeque<SudokuAction>();
	private Deque<SudokuAction> redoStack = new ArrayDeque<SudokuAction>();

	public void addUndoAction(SudokuAction sudokuAction) {
		
		if (!undoStack.isEmpty()) {
			if (undoStack.peek().equals(sudokuAction)) {
				/* duplicate call due to cell change listener isseu */
				/* do not push */
			} else {
				this.undoStack.push(sudokuAction);	
			}
		} else {
			this.undoStack.push(sudokuAction);	
		}
	}

	/**
	 * Undo last action such as entering a number, deleting a number or redo
	 * 
	 */
	public void doUndo(CellGrid currentCellGridFromBoard) {
		/*
		 * revert last action on the sudokuActionStack, and put that action into
		 * sudokuActionStackForUndo
		 */
		
		if (!undoStack.isEmpty()) {
			
			SudokuAction lastAction = undoStack.pop(); 
			CellGrid previousCellGrid = lastAction.getCellGrid(); 
			
			SudokuAction undoAction = new SudokuAction(new CellGrid(currentCellGridFromBoard));
			redoStack.push(undoAction);
			
			currentCellGridFromBoard.copyValues(previousCellGrid);
		}
	}

	/**
	 * redo last action reverted back back undo
	 */
	public void doRedo(CellGrid currentCellGridFromBoard) {
		/* redo last action reverted back by undo */
		
		if (!redoStack.isEmpty()) {
			SudokuAction lastActionUndone = redoStack.pop();
			CellGrid previousCellGrid = lastActionUndone.getCellGrid();
			
			SudokuAction redoAction = new SudokuAction(new CellGrid(currentCellGridFromBoard));
			undoStack.push(redoAction);

			currentCellGridFromBoard.copyValues(previousCellGrid);
		}
	}
	
	//TODO: Unit test
	public boolean hasUndoActions() {
		return !this.undoStack.isEmpty();
	}
	
	//TODO: Unit test
	public boolean hasRedoActions() {
		return !this.redoStack.isEmpty();
	}
}<commit_msg>actionManager's addUndoAction is refactored and its comment is updated<commit_after>package edu.psu.sweng500.team8.play;

import java.util.ArrayDeque;
import java.util.Deque;

import edu.psu.sweng500.team8.coreDataStructures.CellGrid;

public class ActionManager {

	/* If limit the size, we need a custom implementation */
	private Deque<SudokuAction> undoStack = new ArrayDeque<SudokuAction>();
	private Deque<SudokuAction> redoStack = new ArrayDeque<SudokuAction>();

	public void addUndoAction(SudokuAction sudokuAction) {

		/* push sudokuAction only when undoStack is empty OR the top action isn't the same as incoming action */
		if (undoStack.isEmpty() || !undoStack.peek().equals(sudokuAction)) {
			this.undoStack.push(sudokuAction);
		} 
	}
	
	private void addRedoAction(SudokuAction sudokuAction) {

		/* push sudokuAction only when redoStack is empty OR the top action isn't the same as incoming action */
		if (redoStack.isEmpty() || !redoStack.peek().equals(sudokuAction)) {
			this.redoStack.push(sudokuAction);
		}
	}

	/**
	 * Undo last action such as entering a number, deleting a number or redo
	 * 
	 */
	public void doUndo(CellGrid currentCellGridFromBoard) {
		/*
		 * revert last action on the sudokuActionStack, and put that action into
		 * sudokuActionStackForUndo
		 */
		
		if (!undoStack.isEmpty()) {
			
			SudokuAction lastAction = undoStack.pop(); 
			CellGrid previousCellGrid = lastAction.getCellGrid(); 
			
			SudokuAction undoAction = new SudokuAction(new CellGrid(currentCellGridFromBoard));
			addRedoAction(undoAction);
			
			currentCellGridFromBoard.copyValues(previousCellGrid);
		}
	}

	/**
	 * redo last action reverted back back undo
	 */
	public void doRedo(CellGrid currentCellGridFromBoard) {
		/* redo last action reverted back by undo */
		
		if (!redoStack.isEmpty()) {
			SudokuAction lastActionUndone = redoStack.pop();
			CellGrid previousCellGrid = lastActionUndone.getCellGrid();
			
			SudokuAction redoAction = new SudokuAction(new CellGrid(currentCellGridFromBoard));
			addUndoAction(redoAction);

			currentCellGridFromBoard.copyValues(previousCellGrid);
		}
	}
	
	//TODO: Unit test
	public boolean hasUndoActions() {
		return !this.undoStack.isEmpty();
	}
	
	//TODO: Unit test
	public boolean hasRedoActions() {
		return !this.redoStack.isEmpty();
	}
}
{'fraction_non_alphanumeric': 0.1001150747986191, 'fraction_numerical': 0.003682393555811277, 'mean_word_length': 4.229843561973526, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3827798', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1353, 'n_words': 327}