-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Branch kk filter by status #66
Changes from all commits
78318d3
fb94b9a
c1ca026
8a2c5e7
7d1e94e
21d6076
d5cf06a
9f84352
1afa057
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package seedu.address.logic.commands; | ||
|
||
/** | ||
* Sets predicates that filter article book | ||
*/ | ||
public class SetCommand { | ||
public static final String COMMAND_WORD = "set"; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package seedu.address.logic.commands.articlecommands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ARTICLES; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.article.Article; | ||
import seedu.address.model.article.ArticleMatchesStatusPredicate; | ||
import seedu.address.model.article.exceptions.InvalidStatusException; | ||
|
||
/** | ||
* Use to set filter for Articles | ||
*/ | ||
public class SetArticleCommand extends ArticleCommand { | ||
public static final String COMMAND_WORD = "set"; | ||
|
||
public static final String COMMAND_PREFIX = "-a"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Filter Updated"; | ||
private Predicate<Article> finalPredicate; | ||
|
||
/** | ||
* Constructs a SetArticleCommand object. | ||
* @param status The status to be filtered by. | ||
*/ | ||
public SetArticleCommand(String status) { | ||
try { | ||
finalPredicate = new ArticleMatchesStatusPredicate(status); | ||
} catch (InvalidStatusException e) { | ||
finalPredicate = PREDICATE_SHOW_ALL_ARTICLES; | ||
} | ||
} | ||
Check warning on line 35 in src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java
|
||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.getFilter().updateFilter(finalPredicate); | ||
model.updateFilteredArticleList(finalPredicate); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
Check warning on line 42 in src/main/java/seedu/address/logic/commands/articlecommands/SetArticleCommand.java
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.articlecommands.SetArticleCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a SetArticleCommand object | ||
*/ | ||
public class SetArticleCommandParser implements Parser<SetArticleCommand> { | ||
|
||
@Override | ||
public SetArticleCommand parse(String userInput) throws ParseException { | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(userInput, PREFIX_STATUS); | ||
if (!arePrefixesPresent(argMultimap, PREFIX_STATUS)) { | ||
throw new ParseException("The set command does not follow the correct format"); | ||
} | ||
String status = argMultimap.getValue(PREFIX_STATUS).get(); | ||
return new SetArticleCommand(status); | ||
} | ||
|
||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package seedu.address.model; | ||
|
||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ARTICLES; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.model.article.Article; | ||
|
||
/** | ||
* Use to filter through ArticleBook | ||
*/ | ||
public class ArticleFilter implements Filter { | ||
private Predicate<Article> finalPredicate; | ||
public ArticleFilter() { | ||
finalPredicate = PREDICATE_SHOW_ALL_ARTICLES; | ||
} | ||
public void updateFilter(Predicate<Article> newPredicate) { | ||
finalPredicate = newPredicate; | ||
} | ||
public Predicate<Article> getFinalPredicate() { | ||
return finalPredicate; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package seedu.address.model; | ||
|
||
/** | ||
* Used to sieve through lists. | ||
*/ | ||
public interface Filter { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package seedu.address.model.article; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.model.article.exceptions.InvalidStatusException; | ||
|
||
/** | ||
* Ensures that an {@code Article}'s {@code Status} matches the given status | ||
*/ | ||
public class ArticleMatchesStatusPredicate implements Predicate<Article> { | ||
private Article.Status status; | ||
|
||
/** | ||
* Constructs an ArticleMatchesStatusPredicate | ||
* @param s The string representation of the status | ||
* @throws InvalidStatusException thrown when status entered is invalid. | ||
*/ | ||
public ArticleMatchesStatusPredicate(String s) throws InvalidStatusException { | ||
if (s.equals("PUBLISHED")) { | ||
status = Article.Status.PUBLISHED; | ||
} else if (s.equals("DRAFT")) { | ||
status = Article.Status.DRAFT; | ||
} else if (s.equals("ARCHIVED")) { | ||
status = Article.Status.ARCHIVED; | ||
} else { | ||
throw new InvalidStatusException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean test(Article article) { | ||
return this.status.equals(article.getStatus()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof TitleContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
ArticleMatchesStatusPredicate otherArticleMatchesStatusPredicate = (ArticleMatchesStatusPredicate) other; | ||
return this.status.equals(otherArticleMatchesStatusPredicate.getStatus()); | ||
} | ||
public Article.Status getStatus() { | ||
return this.status; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package seedu.address.model.article.exceptions; | ||
|
||
/** | ||
* Signals that status entered is not a valid one. | ||
*/ | ||
public class InvalidStatusException extends RuntimeException { | ||
public InvalidStatusException() { | ||
super("The status is invalid"); | ||
} | ||
Check warning on line 9 in src/main/java/seedu/address/model/article/exceptions/InvalidStatusException.java
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not "FilterCommand"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to emphasize that using the command sets the filter for all future searches.