forked from AY2425S1-CS2103T-T11-2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AY2425S1-CS2103T-T11-2#138 from Incogdino/branch-a…
…ddCommandHistory Branch add command history
- Loading branch information
Showing
4 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/main/java/seedu/address/storage/CommandHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package seedu.address.storage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javafx.scene.input.KeyCode; | ||
|
||
/** | ||
* Stores the history of commands entered by the user. | ||
*/ | ||
public class CommandHistory { | ||
private static CommandHistory instance = null; | ||
private static Logger logger = Logger.getLogger("CommandHistory"); | ||
|
||
private ArrayList<String> listOfCommands; | ||
private int index; | ||
|
||
|
||
/** | ||
* Constructor for CommandHistory. | ||
*/ | ||
private CommandHistory() { | ||
this.listOfCommands = new ArrayList<>(); | ||
this.index = 0; | ||
} | ||
|
||
/** | ||
* Returns the instance of the CommandHistory class. Instantiates a new instance if not instantiated. | ||
* | ||
* @return instance of the {@code CommandHistory} class | ||
*/ | ||
public static CommandHistory getInstance() { | ||
if (instance == null) { | ||
instance = new CommandHistory(); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
/** | ||
* Adds a new command to the {@code CommandHistory} instance. | ||
* | ||
* @param command command to add. | ||
*/ | ||
public void addCommand(String command) { | ||
if (command == null) { | ||
return; | ||
} | ||
|
||
assert(command != null && !command.isEmpty()); | ||
|
||
logger.log(Level.INFO, "Adding command into command history"); | ||
listOfCommands.add(command); | ||
index = listOfCommands.size(); | ||
} | ||
|
||
/** | ||
* Returns the requested command by the user based on user's key input. The valid {@KeyCode} inputs are the up | ||
* and down arrow keys. | ||
* | ||
* @param key {@KeyCode} input. | ||
* @return command corresponding to the input requested by the user. | ||
*/ | ||
public String getPastCommand(KeyCode key) { | ||
assert(key.isArrowKey() && (key == KeyCode.UP || key == KeyCode.DOWN)); | ||
|
||
// list of commands is empty | ||
if (listOfCommands.size() == 0) { | ||
logger.log(Level.INFO, "No history of commands, returning empty string"); | ||
return ""; | ||
} | ||
|
||
logger.log(Level.INFO, "Returning requested command"); | ||
switch (key) { | ||
case UP: | ||
return getPreviousCommand(); | ||
case DOWN: | ||
return getNextCommand(); | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the previous command in the command history. This corresponds to clicking the up arrow key. | ||
* | ||
* @return previous command in the command history. | ||
*/ | ||
private String getPreviousCommand() { | ||
// No commands left in command history | ||
if (index == 0) { | ||
logger.log(Level.INFO, "No more commands preceding this, returning current command."); | ||
return listOfCommands.get(index); | ||
} | ||
|
||
logger.log(Level.INFO, "Returning previous command"); | ||
String command = listOfCommands.get(index - 1); | ||
index--; | ||
|
||
return command; | ||
} | ||
|
||
/** | ||
* Returns the next command in the command history. This corresponds to clicking the down arrow key. | ||
* | ||
* @return next command in the command history. | ||
*/ | ||
private String getNextCommand() { | ||
// At last parsed command in command history | ||
if (index >= listOfCommands.size() - 1) { | ||
logger.log(Level.INFO, "Returning current command"); | ||
index = listOfCommands.size(); | ||
return ""; | ||
} | ||
|
||
logger.log(Level.INFO, "Returning next command"); | ||
String command = listOfCommands.get(index + 1); | ||
index++; | ||
return command; | ||
} | ||
|
||
/** | ||
* Clears the command history. | ||
*/ | ||
public void clearListOfCommands() { | ||
this.listOfCommands.clear(); | ||
this.index = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/seedu/address/storage/CommandHistoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package seedu.address.storage; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javafx.scene.input.KeyCode; | ||
|
||
public class CommandHistoryTest { | ||
private static final String VALID_COMMAND = "github n/John Doe"; | ||
private static final String INVALID_COMMAND = "iloveoop"; | ||
private CommandHistory commandHistory = CommandHistory.getInstance(); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
// Resets command history | ||
commandHistory.clearListOfCommands(); | ||
commandHistory = CommandHistory.getInstance(); | ||
} | ||
|
||
@Test | ||
void addCommand_nullStringCommand_success() { | ||
commandHistory.addCommand(null); | ||
} | ||
|
||
@Test | ||
public void getPastCommand_emptyListOfCommandsWithArrowKey_emptyStringReturned() { | ||
assertEquals(commandHistory.getPastCommand(KeyCode.UP), ""); | ||
commandHistory.clearListOfCommands(); //reset list of commands | ||
assertEquals(commandHistory.getPastCommand(KeyCode.DOWN), ""); | ||
} | ||
|
||
@Test | ||
public void getPastCommand_filledListOfCommandsWithUpArrowKey_commandReturned() { | ||
commandHistory.addCommand(VALID_COMMAND); | ||
commandHistory.addCommand(INVALID_COMMAND); | ||
|
||
String firstReturnedCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
assertEquals(firstReturnedCommand, INVALID_COMMAND); | ||
|
||
String secondReturnedCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
assertEquals(secondReturnedCommand, VALID_COMMAND); | ||
} | ||
|
||
/** | ||
* Tests the edge case where the earliest command is always returned after sufficiently large number of UP arrow | ||
* keys input. | ||
*/ | ||
@Test | ||
public void getPastCommand_earliestCommandEntered_commandReturned() { | ||
// Adds two commands into the command history | ||
commandHistory.addCommand(VALID_COMMAND); | ||
commandHistory.addCommand(INVALID_COMMAND); | ||
|
||
// Retrieves the first two commands | ||
String firstReturnedCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
String secondReturnedCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
|
||
// Attempting the third command -> expected to get the same command as the second command | ||
String thirdReturnedCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
assertEquals(secondReturnedCommand, thirdReturnedCommand); | ||
} | ||
|
||
@Test | ||
public void getPastCommand_navigateToCurrentCommand_emptyStringReturned() { | ||
commandHistory.addCommand(VALID_COMMAND); | ||
commandHistory.addCommand(INVALID_COMMAND); | ||
|
||
// Returns the last stored command in the command history. | ||
commandHistory.getPastCommand(KeyCode.UP); | ||
|
||
String currentCommand = commandHistory.getPastCommand(KeyCode.DOWN); | ||
assertEquals(currentCommand, ""); | ||
} | ||
|
||
@Test | ||
public void getPastCommand_getCommandWithDownArrowKey_emptyStringReturned() { | ||
commandHistory.addCommand(VALID_COMMAND); | ||
commandHistory.addCommand(INVALID_COMMAND); | ||
|
||
// Returns the 2 commands stored in the command history. | ||
commandHistory.getPastCommand(KeyCode.UP); | ||
commandHistory.getPastCommand(KeyCode.UP); | ||
|
||
String lastCommandStored = commandHistory.getPastCommand(KeyCode.DOWN); | ||
assertEquals(lastCommandStored, INVALID_COMMAND); | ||
} | ||
|
||
|
||
@Test | ||
public void clearListOfCommands_emptyStringReturned() { | ||
commandHistory.addCommand(VALID_COMMAND); | ||
commandHistory.addCommand(INVALID_COMMAND); | ||
|
||
commandHistory.clearListOfCommands(); | ||
|
||
String currentCommand = commandHistory.getPastCommand(KeyCode.UP); | ||
assertEquals(currentCommand, ""); | ||
} | ||
} |