Skip to content
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

Reformat find by name command #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.ContainsKeywordsPredicate;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Keyword matching is case-insensitive.
*/
public abstract class AbstractFindCommand extends Command {

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails "
+ "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final ContainsKeywordsPredicate predicate;

public AbstractFindCommand(ContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

Check warning on line 27 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L25-L27

Added lines #L25 - L27 were not covered by tests

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(this.predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));

Check warning on line 34 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L31-L34

Added lines #L31 - L34 were not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 40 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L40

Added line #L40 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof AbstractFindCommand)) {
return false;

Check warning on line 45 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L45

Added line #L45 was not covered by tests
}

AbstractFindCommand otherFindCommand = (AbstractFindCommand) other;
return this.predicate.equals(otherFindCommand.predicate);

Check warning on line 49 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L48-L49

Added lines #L48 - L49 were not covered by tests
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", this.predicate)
.toString();

Check warning on line 56 in src/main/java/seedu/address/logic/commands/AbstractFindCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/AbstractFindCommand.java#L54-L56

Added lines #L54 - L56 were not covered by tests
}
}
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
* Keyword matching is case-insensitive.
*/
public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names "
+ "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

Expand All @@ -29,7 +29,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.updateFilteredPersonList(this.predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -46,13 +46,13 @@ public boolean equals(Object other) {
}

FindCommand otherFindCommand = (FindCommand) other;
return predicate.equals(otherFindCommand.predicate);
return this.predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.add("predicate", this.predicate)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.model.person;

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class ContactContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public ContactContainsKeywordsPredicate(List<String> keywords) {
super(keywords);
}

Check warning on line 15 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L14-L15

Added lines #L14 - L15 were not covered by tests

@Override
public boolean test(Person person) {
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword));

Check warning on line 20 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 26 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L26

Added line #L26 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof ContactContainsKeywordsPredicate)) {
return false;

Check warning on line 31 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L31

Added line #L31 was not covered by tests
}

ContactContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContactContainsKeywordsPredicate) other;
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());

Check warning on line 35 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L34-L35

Added lines #L34 - L35 were not covered by tests
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();

Check warning on line 40 in src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.java#L40

Added line #L40 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public abstract class ContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public ContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

protected List<String> getKeywords() {
return this.keywords;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 25 in src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java#L25

Added line #L25 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof ContainsKeywordsPredicate)) {
return false;

Check warning on line 30 in src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java#L30

Added line #L30 was not covered by tests
}

ContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);

Check warning on line 34 in src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java#L33-L34

Added lines #L33 - L34 were not covered by tests
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();

Check warning on line 39 in src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.java#L39

Added line #L39 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.model.person;

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class EmailContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public EmailContainsKeywordsPredicate(List<String> keywords) {
super(keywords);
}

Check warning on line 15 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L14-L15

Added lines #L14 - L15 were not covered by tests

@Override
public boolean test(Person person) {
return super.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword));

Check warning on line 20 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 26 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L26

Added line #L26 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof EmailContainsKeywordsPredicate)) {
return false;

Check warning on line 31 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L31

Added line #L31 was not covered by tests
}

EmailContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (EmailContainsKeywordsPredicate) other;
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());

Check warning on line 35 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L34-L35

Added lines #L34 - L35 were not covered by tests
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();

Check warning on line 40 in src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.java#L40

Added line #L40 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class NameContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;
public class NameContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public NameContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
super(keywords);
}

@Override
public boolean test(Person person) {
return keywords.stream()
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword));
}

Expand All @@ -34,11 +32,11 @@ public boolean equals(Object other) {
}

NameContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (NameContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();
}
}