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 pull request #75 from ashleyy2444/branch-addFilterPL
Add Filter Programming Language Feature
- Loading branch information
Showing
16 changed files
with
374 additions
and
19 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/seedu/address/logic/commands/FilterProgrammingLanguageCommand.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,59 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROGRAMMING_LANGUAGE; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.language.ProgrammingLanguageContainsKeywordsPredicate; | ||
|
||
/** | ||
* Filters and lists all persons in address book whose programming language contains any of the argument keywords. | ||
*/ | ||
public class FilterProgrammingLanguageCommand extends FilterCommand { | ||
|
||
public static final String COMMAND_WORD = FilterCommand.COMMAND_WORD + " " + PREFIX_PROGRAMMING_LANGUAGE; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Filters all contacts whose programming language " | ||
+ "contain any of the specified tags and displays them as a list with index numbers.\n" | ||
+ "Parameters: PROGRAMMING_LANGUAGE [MORE_PROGRAMMING_LANGUAGES]...\n" | ||
+ "Example: " + COMMAND_WORD + " Java"; | ||
|
||
private final ProgrammingLanguageContainsKeywordsPredicate predicate; | ||
|
||
public FilterProgrammingLanguageCommand(ProgrammingLanguageContainsKeywordsPredicate 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 FilterProgrammingLanguageCommand)) { | ||
return false; | ||
} | ||
|
||
FilterProgrammingLanguageCommand otherFindCommand = (FilterProgrammingLanguageCommand) other; | ||
return predicate.equals(otherFindCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("programming_language", 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
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
43 changes: 43 additions & 0 deletions
43
src/main/java/seedu/address/logic/parser/FilterProgrammingLanguageCommandParser.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,43 @@ | ||
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.FilterProgrammingLanguageCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.language.ProgrammingLanguage; | ||
import seedu.address.model.language.ProgrammingLanguageContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FilterProgrammingLanguageCommandParser implements Parser<FilterProgrammingLanguageCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the FilterProgrammingLanguageCommand | ||
* and returns a FilterProgrammingLanguageCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FilterProgrammingLanguageCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterProgrammingLanguageCommand.MESSAGE_USAGE)); | ||
} | ||
String[] languageKeywords = trimmedArgs.split("\\s+"); | ||
return new FilterProgrammingLanguageCommand( | ||
new ProgrammingLanguageContainsKeywordsPredicate(createLanguages(languageKeywords))); | ||
} | ||
|
||
/** | ||
* Parses {@code languageKeywords} into a {@code List<ProgrammingLanguage>}. | ||
*/ | ||
public static List<ProgrammingLanguage> createLanguages(String... languageKeywords) throws ParseException { | ||
List<ProgrammingLanguage> languages = new ArrayList<>(); | ||
for (String keyword : languageKeywords) { | ||
languages.add(ParserUtil.parseProgrammingLanguage(keyword.toLowerCase())); // Convert to lowercase | ||
} | ||
return languages; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/main/java/seedu/address/model/language/ProgrammingLanguageContainsKeywordsPredicate.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,66 @@ | ||
package seedu.address.model.language; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Tag} matches any of the keywords given. | ||
*/ | ||
public class ProgrammingLanguageContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<ProgrammingLanguage> originalLanguages; | ||
private final List<String> lowercaseLanguageNames; | ||
/** | ||
* Constructs a {@code ProgrammingLanguageContainsKeywordsPredicate} with the given list of programming languages. | ||
* Creates two lists: one to store the original ProgrammingLanguage objects and another to store their lowercase | ||
* string representations. | ||
* | ||
* @param languages The list of programming languages to be used in the predicate. | ||
* @throws NullPointerException if {@code languages} is null. | ||
*/ | ||
public ProgrammingLanguageContainsKeywordsPredicate(List<ProgrammingLanguage> languages) { | ||
requireNonNull(languages); | ||
this.originalLanguages = new ArrayList<>(languages); | ||
this.lowercaseLanguageNames = languages.stream() | ||
.map(ProgrammingLanguage::getLanguageName) | ||
.map(String::toLowerCase) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
Set<ProgrammingLanguage> personProgrammingLanguages = person.getProgrammingLanguages(); | ||
return personProgrammingLanguages.stream() | ||
.map(ProgrammingLanguage::getLanguageName) | ||
.map(String::toLowerCase) | ||
.anyMatch(lowercaseLanguageNames::contains); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ProgrammingLanguageContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
ProgrammingLanguageContainsKeywordsPredicate otherProgrammingLanguageContainsKeywordsPredicate = | ||
(ProgrammingLanguageContainsKeywordsPredicate) other; | ||
return originalLanguages.equals(otherProgrammingLanguageContainsKeywordsPredicate.originalLanguages); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("programming_language", originalLanguages).toString(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../person/TagContainsKeywordsPredicate.java → ...del/tag/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
74 changes: 74 additions & 0 deletions
74
src/test/java/seedu/address/logic/commands/FilterProgrammingLanguageCommandTest.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,74 @@ | ||
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.FilterProgrammingLanguageCommandParser.createLanguages; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
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.language.ProgrammingLanguageContainsKeywordsPredicate; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) for {@code FilterProgrammingLanguageCommand}. | ||
*/ | ||
public class FilterProgrammingLanguageCommandTest { | ||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() throws ParseException { | ||
ProgrammingLanguageContainsKeywordsPredicate firstPredicate = | ||
new ProgrammingLanguageContainsKeywordsPredicate(createLanguages("Java")); | ||
ProgrammingLanguageContainsKeywordsPredicate secondPredicate = | ||
new ProgrammingLanguageContainsKeywordsPredicate(createLanguages("Python")); | ||
|
||
FilterProgrammingLanguageCommand filterProgrammingLanguageFirstCommand = | ||
new FilterProgrammingLanguageCommand(firstPredicate); | ||
FilterProgrammingLanguageCommand filterProgrammingLanguageSecondCommand = | ||
new FilterProgrammingLanguageCommand(secondPredicate); | ||
|
||
// same object -> returns true | ||
assertTrue(filterProgrammingLanguageFirstCommand.equals(filterProgrammingLanguageFirstCommand)); | ||
|
||
// same values -> returns true | ||
FilterProgrammingLanguageCommand filterProgrammingLanguageFirstCommandCopy = | ||
new FilterProgrammingLanguageCommand(firstPredicate); | ||
assertTrue(filterProgrammingLanguageFirstCommand.equals(filterProgrammingLanguageFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(filterProgrammingLanguageFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(filterProgrammingLanguageFirstCommand.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(filterProgrammingLanguageFirstCommand.equals(filterProgrammingLanguageSecondCommand)); | ||
} | ||
|
||
@Test | ||
public void execute_zeroKeywords_noPersonFound() throws ParseException { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
ProgrammingLanguageContainsKeywordsPredicate predicate = | ||
preparePredicate(" "); | ||
FilterProgrammingLanguageCommand command = new FilterProgrammingLanguageCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredPersonList()); | ||
} | ||
/** | ||
* Parses {@code userInput} into a {@code ProgrammingLanguageContainsKeywordsPredicate}. | ||
*/ | ||
private ProgrammingLanguageContainsKeywordsPredicate preparePredicate(String userInput) throws ParseException { | ||
return new ProgrammingLanguageContainsKeywordsPredicate(createLanguages(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
Oops, something went wrong.