forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into branch-fixEditCompanyNameBug
# Conflicts: # src/test/java/seedu/address/model/person/CompanyNameTest.java
- Loading branch information
Showing
12 changed files
with
426 additions
and
21 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
60 changes: 60 additions & 0 deletions
60
src/main/java/seedu/address/logic/commands/FilterTagCommand.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,60 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
|
||
/** | ||
* Filters and lists all persons in address book whose tags contains any of the argument keywords. | ||
*/ | ||
public class FilterTagCommand extends FilterCommand { | ||
|
||
public static final String COMMAND_WORD = FilterCommand.COMMAND_WORD + " " + PREFIX_TAG; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Filters all contacts whose tags contain any of " | ||
+ "the specified tags and displays them as a list with index numbers.\n" | ||
+ "Parameters: TAG [MORE_TAGS]...\n" | ||
+ "Example: " + COMMAND_WORD + " owes money"; | ||
|
||
private final TagContainsKeywordsPredicate predicate; | ||
|
||
public FilterTagCommand(TagContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
|
||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredPersonList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FilterTagCommand)) { | ||
return false; | ||
} | ||
|
||
FilterTagCommand otherFindCommand = (FilterTagCommand) other; | ||
return predicate.equals(otherFindCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("tag", predicate) | ||
.toString(); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/logic/parser/FilterTagCommandParser.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,45 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.logic.commands.FilterTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
import seedu.address.model.tag.Tag; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FilterTagCommandParser implements Parser<FilterTagCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FilterTagCommand | ||
* and returns a FilterTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FilterTagCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterTagCommand.MESSAGE_USAGE)); | ||
} | ||
String[] tagKeywords = trimmedArgs.split("\\s+"); | ||
return new FilterTagCommand(new TagContainsKeywordsPredicate(createTags(tagKeywords))); | ||
} | ||
|
||
/** | ||
* Parses {@code tagKeywords} into a {@code List<Tag>}. | ||
*/ | ||
public static List<Tag> createTags(String... tagKeywords) throws ParseException { | ||
List<Tag> tags = new ArrayList<>(); | ||
for (String keyword : tagKeywords) { | ||
tags.add(ParserUtil.parseTag(keyword)); | ||
} | ||
return tags; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/address/model/person/TagContainsKeywordsPredicate.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,46 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Tag} matches any of the keywords given. | ||
*/ | ||
public class TagContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<Tag> tags; | ||
|
||
public TagContainsKeywordsPredicate(List<Tag> tags) { | ||
this.tags = tags; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
Set<Tag> personTags = person.getTags(); | ||
return tags.stream() | ||
.anyMatch(personTags::contains); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof TagContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
TagContainsKeywordsPredicate otherTagContainsKeywordsPredicate = (TagContainsKeywordsPredicate) other; | ||
return tags.equals(otherTagContainsKeywordsPredicate.tags); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("tags", tags).toString(); | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
src/test/java/seedu/address/logic/commands/FilterTagCommandTest.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,94 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.logic.parser.FilterTagCommandParser.createTags; | ||
import static seedu.address.testutil.TypicalPersons.ALICE; | ||
import static seedu.address.testutil.TypicalPersons.BENSON; | ||
import static seedu.address.testutil.TypicalPersons.DANIEL; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
|
||
/** | ||
* Contains integration tests (interaction with the Model) for {@code FilterTagCommand}. | ||
*/ | ||
public class FilterTagCommandTest { | ||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() throws ParseException { | ||
TagContainsKeywordsPredicate firstPredicate = | ||
new TagContainsKeywordsPredicate(createTags("first")); | ||
TagContainsKeywordsPredicate secondPredicate = | ||
new TagContainsKeywordsPredicate(createTags("second")); | ||
|
||
FilterTagCommand findTagFirstCommand = new FilterTagCommand(firstPredicate); | ||
FilterTagCommand findTagSecondCommand = new FilterTagCommand(secondPredicate); | ||
|
||
// same object -> returns true | ||
assertTrue(findTagFirstCommand.equals(findTagFirstCommand)); | ||
|
||
// same values -> returns true | ||
FilterTagCommand findTagFirstCommandCopy = new FilterTagCommand(firstPredicate); | ||
assertTrue(findTagFirstCommand.equals(findTagFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(findTagFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(findTagFirstCommand.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(findTagFirstCommand.equals(findTagSecondCommand)); | ||
} | ||
|
||
@Test | ||
public void execute_zeroKeywords_noPersonFound() throws ParseException { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
TagContainsKeywordsPredicate predicate = preparePredicate(" "); | ||
FilterTagCommand command = new FilterTagCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredPersonList()); | ||
} | ||
|
||
@Test | ||
public void execute_multipleKeywords_multiplePersonsFound() throws ParseException { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); | ||
TagContainsKeywordsPredicate predicate = preparePredicate("friends friend"); | ||
FilterTagCommand command = new FilterTagCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(ALICE, BENSON, DANIEL), model.getFilteredPersonList()); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() throws ParseException { | ||
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(createTags("friends")); | ||
FilterTagCommand filterTagCommand = new FilterTagCommand(predicate); | ||
String expected = FilterTagCommand.class.getCanonicalName() + "{tag=" + predicate + "}"; | ||
assertEquals(expected, filterTagCommand.toString()); | ||
} | ||
|
||
/** | ||
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. | ||
*/ | ||
private TagContainsKeywordsPredicate preparePredicate(String userInput) throws ParseException { | ||
return new TagContainsKeywordsPredicate(createTags(userInput.split("\\s+"))); | ||
} | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
src/test/java/seedu/address/logic/parser/FilterTagCommandParserTest.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,35 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
import static seedu.address.logic.parser.FilterTagCommandParser.createTags; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.FilterTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
|
||
public class FilterTagCommandParserTest { | ||
|
||
private FilterTagCommandParser parser = new FilterTagCommandParser(); | ||
|
||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
FilterTagCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
@Test | ||
public void parse_validArgs_returnsFilterTagCommand() throws ParseException { | ||
// no leading and trailing whitespaces | ||
FilterTagCommand expectedFilterTagCommand = | ||
new FilterTagCommand(new TagContainsKeywordsPredicate(createTags("Alice", "Bob"))); | ||
assertParseSuccess(parser, "Alice Bob", expectedFilterTagCommand); | ||
|
||
// multiple whitespaces between keywords | ||
assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFilterTagCommand); | ||
} | ||
} |
Oops, something went wrong.