Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/AY2021S1-CS2103T-F12-1/tp
Browse files Browse the repository at this point in the history
…into branch-improve-task-list-view
  • Loading branch information
Ma-Yueran committed Oct 11, 2020
2 parents 3066cb9 + 40761f4 commit 4532b25
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_TAG_INPUT = "The Tag provided is invalid";
public static final String MESSAGE_INVALID_TAG_DISPLAYED_INDEX = "The Tag index provided is invalid";
public static final String MESSAGE_TAGS_LISTED_OVERVIEW = "%1$d tags listed!";

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/logic/commands/ShowCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TAG_INPUT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG_NAME;

import java.util.List;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagNameEqualsKeywordPredicate;

/**
* Shows a Tag's file path identified using it's tag name.
*/
public class ShowCommand extends Command {

public static final String COMMAND_WORD = "show";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows a tag's file address. "
+ "Parameters: "
+ PREFIX_TAG_NAME + "TAG_NAME "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_TAG_NAME + "cs2103 ";

public static final String MESSAGE_SUCCESS = "%s's file path: %s";

private final TagNameEqualsKeywordPredicate predicate;

/**
* Creates an ShowCommand to show the specified {@code Tag}'s file path
*/
public ShowCommand(TagNameEqualsKeywordPredicate predicate) {
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Tag> tags = model.findFilteredTagList(predicate);

if (tags.isEmpty()) {
throw new CommandException(MESSAGE_INVALID_TAG_INPUT);
}

Tag tag = tags.get(0);
return new CommandResult(String.format(MESSAGE_SUCCESS, tag.getTagName(), tag.getFileAddress()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ShowCommand // instanceof handles nulls
&& predicate.equals(((ShowCommand) other).predicate));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ShowCommand;
import seedu.address.logic.commands.TagCommand;
import seedu.address.logic.commands.UntagCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -52,6 +53,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case ShowCommand.COMMAND_WORD:
return new ShowCommandParser().parse(arguments);

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/seedu/address/logic/parser/ShowCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG_NAME;

import seedu.address.logic.commands.ShowCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.TagName;
import seedu.address.model.tag.TagNameEqualsKeywordPredicate;

/**
* Parses input arguments and creates a new ShowCommand object
*/
public class ShowCommandParser implements Parser<ShowCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ShowCommand
* and returns an ShowCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
@Override
public ShowCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TAG_NAME);

if (!argMultimap.getValue(PREFIX_TAG_NAME).isPresent()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ShowCommand.MESSAGE_USAGE));
}

TagName tagName = new TagName(argMultimap.getValue(PREFIX_TAG_NAME).orElse(""));

return new ShowCommand(new TagNameEqualsKeywordPredicate(tagName));
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model;

import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -84,4 +85,10 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredTagList(Predicate<Tag> predicate);

/**
* Finds the filter of the filtered tag list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
*/
List<Tag> findFilteredTagList(Predicate<Tag> predicate);
}
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
Expand Down Expand Up @@ -129,6 +131,15 @@ public void updateFilteredTagList(Predicate<Tag> predicate) {
filteredTags.setPredicate(predicate);
}

@Override
public List<Tag> findFilteredTagList(Predicate<Tag> predicate) {
requireNonNull(predicate);

return getFilteredTagList().stream()
.filter(predicate)
.collect(Collectors.toList());
}

@Override
public boolean equals(Object obj) {
// short circuit if same object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ public boolean equals(Object other) {
|| (other instanceof TagNameContainsKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((TagNameContainsKeywordsPredicate) other).keywords)); // state check
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.model.tag;

import java.util.function.Predicate;

/**
* Tests that a {@code Tag}'s {@code TagName} equals to the tag name given.
*/
public class TagNameEqualsKeywordPredicate implements Predicate<Tag> {
private final TagName tagNameToFind;

public TagNameEqualsKeywordPredicate(TagName tagNameToFind) {
this.tagNameToFind = tagNameToFind;
}

@Override
public boolean test(Tag tag) {
return tagNameToFind.equals(tag.getTagName());
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof TagNameEqualsKeywordPredicate // instanceof handles nulls
&& tagNameToFind.equals(((TagNameEqualsKeywordPredicate) other).tagNameToFind)); // state check
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"_comment": "fileAddressBook save file which contains the same Person values as in TypicalTags#getTypicalfileAddressBook()",
"tags" : [ {
"tagName" : "Alice Pauline",
"fileAddress" : "c:\\a\\b\\alice.txt"
"tagName" : "cs2103",
"fileAddress" : "c:\\a\\b\\cs2103.txt"
}, {
"tagName" : "Benson Meier",
"fileAddress" : "c:\\a\\b\\benson.txt"
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/seedu/address/logic/commands/ShowCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TAG;
import static seedu.address.testutil.TypicalTags.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.Messages;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagName;
import seedu.address.model.tag.TagNameEqualsKeywordPredicate;

public class ShowCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
TagName firstPredicateTagName = new TagName("first");
TagName secondPredicateTagName = new TagName("second");

TagNameEqualsKeywordPredicate firstPredicate = new TagNameEqualsKeywordPredicate(
firstPredicateTagName);
TagNameEqualsKeywordPredicate secondPredicate = new TagNameEqualsKeywordPredicate(
secondPredicateTagName);

ShowCommand showFirstCommand = new ShowCommand(firstPredicate);
ShowCommand showSecondCommand = new ShowCommand(secondPredicate);

// same object -> returns true
assertTrue(showFirstCommand.equals(showFirstCommand));

// same values -> returns true
ShowCommand showFirstCommandCopy = new ShowCommand(firstPredicate);
assertTrue(showFirstCommand.equals(showFirstCommandCopy));

// different types -> returns false
assertFalse(showFirstCommand.equals(1));

// null -> returns false
assertFalse(showFirstCommand.equals(null));

// different person -> returns false
assertFalse(showFirstCommand.equals(showSecondCommand));
}

@Test
public void execute_invalidTagName_throwsCommandException() {
TagNameEqualsKeywordPredicate predicate = new TagNameEqualsKeywordPredicate(new TagName("Invalid Tag Name"));
ShowCommand showCommand = new ShowCommand(predicate);

assertCommandFailure(showCommand, model, Messages.MESSAGE_INVALID_TAG_INPUT);
}

@Test
public void execute_validTagName_throwsCommandException() {
Tag tagToBeShown = model.getFilteredTagList().get(INDEX_FIRST_TAG.getZeroBased());
TagNameEqualsKeywordPredicate predicate = new TagNameEqualsKeywordPredicate(tagToBeShown.getTagName());
ShowCommand showCommand = new ShowCommand(predicate);

String expectedMessage = String.format(showCommand.MESSAGE_SUCCESS,
tagToBeShown.getTagName(), tagToBeShown.getFileAddress());

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());

assertCommandSuccess(showCommand, model, expectedMessage, expectedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.junit.jupiter.api.Disabled;
Expand Down Expand Up @@ -150,6 +151,11 @@ public ObservableList<Tag> getFilteredTagList() {
public void updateFilteredTagList(Predicate<Tag> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public List<Tag> findFilteredTagList(Predicate<Tag> predicate) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.ShowCommand;
import seedu.address.model.tag.TagName;
import seedu.address.model.tag.TagNameEqualsKeywordPredicate;

public class ShowCommandParserTest {
private ShowCommandParser parser = new ShowCommandParser();

@Test
public void parse_invalidArgs_throwsParseException() {
// No prefix
assertParseFailure(parser, "noprefix",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ShowCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_throwsParseException() {
ShowCommand expectedFindCommand =
new ShowCommand(new TagNameEqualsKeywordPredicate(new TagName(VALID_NAME_AMY)));

assertParseSuccess(parser, NAME_DESC_AMY, expectedFindCommand);
}
}
18 changes: 9 additions & 9 deletions src/test/java/seedu/address/model/AddressBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalTags.ALICE;
import static seedu.address.testutil.TypicalTags.CS2103;
import static seedu.address.testutil.TypicalTags.getTypicalAddressBook;

import java.util.Arrays;
Expand Down Expand Up @@ -44,8 +44,8 @@ public void resetData_withValidReadOnlyAddressBook_replacesData() {
@Test
public void resetData_withDuplicateTags_throwsDuplicateTagException() {
// Two persons with the same identity fields
Tag editedAlice = new TagBuilder(ALICE).build();
List<Tag> newPersons = Arrays.asList(ALICE, editedAlice);
Tag editedCS2103 = new TagBuilder(CS2103).build();
List<Tag> newPersons = Arrays.asList(CS2103, editedCS2103);
AddressBookStub newData = new AddressBookStub(newPersons);

assertThrows(DuplicateTagException.class, () -> addressBook.resetData(newData));
Expand All @@ -58,20 +58,20 @@ public void hasTag_nullTag_throwsNullPointerException() {

@Test
public void hasTag_tagNotInAddressBook_returnsFalse() {
assertFalse(addressBook.hasTag(ALICE));
assertFalse(addressBook.hasTag(CS2103));
}

@Test
public void hasTag_tagInAddressBook_returnsTrue() {
addressBook.addTag(ALICE);
assertTrue(addressBook.hasTag(ALICE));
addressBook.addTag(CS2103);
assertTrue(addressBook.hasTag(CS2103));
}

@Test
public void hasTag_tagWithSameIdentityFieldsInAddressBook_returnsTrue() {
addressBook.addTag(ALICE);
Tag editedAlice = new TagBuilder(ALICE).build();
assertTrue(addressBook.hasTag(editedAlice));
addressBook.addTag(CS2103);
Tag editedCS2103 = new TagBuilder(CS2103).build();
assertTrue(addressBook.hasTag(editedCS2103));
}

@Test
Expand Down
Loading

0 comments on commit 4532b25

Please sign in to comment.