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 #166 from AY2324S2-CS2103T-W12-3/19-fuzzy-search
Implement substring search feature
- Loading branch information
Showing
11 changed files
with
320 additions
and
294 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
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
56 changes: 0 additions & 56 deletions
56
src/main/java/seedu/address/model/person/PersonMatchesKeywordsPredicate.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/main/java/seedu/address/model/person/PersonMatchesQueryPredicate.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,67 @@ | ||
package seedu.address.model.person; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name}, {@code Tags} or {@code Assets} matches the search query. | ||
* The algorithm removes all whitespaces and checks if the query is a substring of a field. | ||
* The substring matching is case-insensitive. | ||
*/ | ||
public class PersonMatchesQueryPredicate implements Predicate<Person> { | ||
|
||
private static final String STRIP_WHITESPACE_REGEX = "\\s+"; | ||
private static final String EMPTY_STRING = ""; | ||
|
||
private final String query; | ||
|
||
/** | ||
* Processes the query string and constructs the object. | ||
* @param query the query string. | ||
* @throws NullPointerException if the query string is null. | ||
* @throws AssertionError if the query string is empty. | ||
*/ | ||
public PersonMatchesQueryPredicate(String query) { | ||
requireNonNull(query); | ||
assert !query.isEmpty(); | ||
this.query = processString(query); | ||
} | ||
|
||
private static String processString(String str) { | ||
return str.toLowerCase().replaceAll(STRIP_WHITESPACE_REGEX, EMPTY_STRING); | ||
} | ||
|
||
//@@author rizkidelta | ||
@Override | ||
public boolean test(Person person) { | ||
return doesStringMatchQuery(person.getName().toString(), query) | ||
|| person.getTags().stream().anyMatch(tag -> doesStringMatchQuery(tag.get(), query) | ||
|| person.getAssets().stream().anyMatch(asset -> doesStringMatchQuery(asset.get(), query))); | ||
} | ||
|
||
//@@author rizkidelta | ||
private static boolean doesStringMatchQuery(String text, String query) { | ||
requireNonNull(text); | ||
|
||
// only need to process text, as query is already processed in the constructor | ||
text = processString(text); | ||
return text.contains(query); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof PersonMatchesQueryPredicate)) { | ||
return false; | ||
} | ||
|
||
PersonMatchesQueryPredicate otherPersonMatchesQueryPredicate = (PersonMatchesQueryPredicate) other; | ||
return query.equals(otherPersonMatchesQueryPredicate.query); | ||
} | ||
|
||
} |
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.