-
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
Add functionality to whitelist and blacklist commands #156
Changes from 3 commits
4f064d6
6fbf7f3
8aa931f
c9eccc7
8671ade
23ebabf
deb62b5
4a09380
7daa34b
a26e74c
c8eb4f8
281d50f
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,33 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.parser.FindCommandParser; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.Model; | ||
|
||
/** | ||
* Lists out all blacklisted clients | ||
*/ | ||
public class BlacklistListCommand extends BlacklistCommand { | ||
|
||
/** | ||
* Instantiates a {@code BlacklistListCommand} object. | ||
*/ | ||
public BlacklistListCommand() { | ||
super(Index.fromZeroBased(0)); // this is irrelevant but necessary | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
|
||
try { | ||
// make the find command do the hard work | ||
return (new FindCommandParser().parse("find cs/blacklisted")).execute(model); | ||
} catch (ParseException pe) { | ||
return null; // this will never happen | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.ArgumentPredicateToFail; | ||
import seedu.address.model.person.ClientStatus; | ||
|
||
/** | ||
* Lists out all whitelisted clients | ||
*/ | ||
public class WhitelistListCommand extends WhitelistCommand { | ||
|
||
/** | ||
* Instantiates a {@code WhitelistListCommand} object. | ||
* | ||
* @param argPredToFail an argument predicate that should fail | ||
*/ | ||
public WhitelistListCommand() { | ||
// this is irrelevant but necessary | ||
super(Index.fromZeroBased(0), new ClientStatus("old")); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
|
||
// here, the predicate dictates that the client status should be blacklisted; | ||
// hence if the predicate fails (ensured by the ArgumentPredicateToFail object), | ||
// the client is not blacklisted => they are whitelisted | ||
model.updateFilteredPersonList(ArgumentPredicateToFail.getNotBlacklistedPredicate()); | ||
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. Just for my understanding, how come there is a different execution for whitelistcommand and blacklistcommand? Couldn't it be easier if there was a predicate 'getBlacklistedPredicate' that kept things consistent and reduces Blacklist's dependence on other code? That being said, I think this still works 👍 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. Yep, I will be decoupling the |
||
|
||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Like an {@code ArgumentPredicate}, but instead tests to see if the Person fails the predicate | ||
*/ | ||
public class ArgumentPredicateToFail extends ArgumentPredicate { | ||
|
||
private static ArgumentPredicateToFail notBlacklistedPredicate; | ||
|
||
/** | ||
* Constructs an {@code ArgumentPredicateToFail} | ||
* | ||
* @param parameterMap A map with parameters inputted by user | ||
*/ | ||
public ArgumentPredicateToFail(Map<String, Object> parameterMap) { | ||
super(parameterMap); | ||
} | ||
|
||
public static ArgumentPredicateToFail getNotBlacklistedPredicate() { | ||
if (notBlacklistedPredicate == null) { | ||
Map<String, Object> parameterMap = new HashMap<>(); | ||
|
||
parameterMap.put(ClientStatus.CLIENT_STATUS_KEY, new ClientStatus("blacklisted")); | ||
|
||
Set<Tag> tagList = new HashSet<>(); | ||
parameterMap.put(Tag.TAG_KEY, tagList); | ||
|
||
notBlacklistedPredicate = new ArgumentPredicateToFail(parameterMap); | ||
} | ||
|
||
return notBlacklistedPredicate; | ||
} | ||
|
||
private void addIfPresent(String key, Predicate<Person> predicate, | ||
Person person, List<Boolean> validParameters) { | ||
assert key != null : "Key should not be null"; | ||
assert predicate != null : "Predicate should not be null"; | ||
assert person != null : "Person should not be null"; | ||
assert validParameters != null : "Valid parameters list should not be null"; | ||
if (getInputParameters().containsKey(key)) { | ||
validParameters.add(!predicate.test(person)); | ||
} | ||
} | ||
|
||
private void addIfTagPresent(Set<Tag> tags, Predicate<Person> predicate, | ||
Person person, List<Boolean> validParameters) { | ||
assert tags != null : "Tags should not be null"; | ||
assert predicate != null : "Predicate should not be null"; | ||
assert person != null : "Person should not be null"; | ||
assert validParameters != null : "Valid parameters list should not be null"; | ||
if (!tags.isEmpty()) { | ||
validParameters.add(!predicate.test(person)); | ||
} | ||
} | ||
|
||
private List<Boolean> getValidParameters(Person person) { | ||
List<Boolean> validParameters = new ArrayList<>(); | ||
addIfPresent(Name.NAME_KEY, p -> { | ||
String[] nameKeywords = getInputParameters().get("name").toString().split("\\s+"); | ||
NameContainsKeywordsPredicate namePredicate = | ||
new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)); | ||
return namePredicate.test(p); | ||
}, person, validParameters); | ||
|
||
addIfPresent(Phone.PHONE_KEY, p -> getInputParameters().get("phone").equals(p.getPhone()), | ||
person, validParameters); | ||
|
||
addIfPresent(Email.EMAIL_KEY, p -> getInputParameters().get("email").equals(p.getEmail()), | ||
person, validParameters); | ||
|
||
addIfPresent(Address.ADDRESS_KEY, p -> getInputParameters().get("address").equals(p.getAddress()), | ||
person, validParameters); | ||
|
||
addIfPresent(ProjectStatus.PROJECT_STATUS_KEY, p -> getInputParameters().get("project status") | ||
.equals(p.getProjectStatus()), person, validParameters); | ||
|
||
addIfPresent(PaymentStatus.PAYMENT_STATUS_KEY, p -> getInputParameters().get("payment status") | ||
.equals(p.getPaymentStatus()), person, validParameters); | ||
|
||
addIfPresent(ClientStatus.CLIENT_STATUS_KEY, p -> getInputParameters().get("client status") | ||
.equals(p.getClientStatus()), person, validParameters); | ||
|
||
addIfPresent(Deadline.DEADLINE_KEY, p -> getInputParameters().get("deadline") | ||
.equals(p.getDeadline()), person, validParameters); | ||
|
||
@SuppressWarnings("unchecked") | ||
Set<Tag> tags = (Set<Tag>) getInputParameters().get(Tag.TAG_KEY); | ||
addIfTagPresent(tags, p -> tags.stream() | ||
.anyMatch(p.getTags()::contains), person, validParameters); | ||
|
||
return validParameters; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
List<Boolean> validParameters = getValidParameters(person); | ||
return validParameters.stream().allMatch(Boolean::booleanValue); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ArgumentPredicateToFail)) { | ||
return false; | ||
} | ||
|
||
ArgumentPredicateToFail argumentPredicateToFail = (ArgumentPredicateToFail) other; | ||
return getInputParameters().equals(argumentPredicateToFail.getInputParameters()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("parameters to fail", getInputParameters()).toString(); | ||
} | ||
} |
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.
By any chance would it be good to have a good error message/log message anyways? In case find command changes or other code is modified? But for now, I agree, it won't happen.
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.
This portion of the find command is reliant on the
ArgumentPredicate
being changed, which is highly unlikely. But in the future I will be decoupling theblacklist
command from thefind
command entirely; this was just a quick addition that I did not want to initially spend too much time on, so I could focus on the test cases and thewhitelist
command.