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.
Update filter by tag to handle multiple tags
- Loading branch information
Showing
8 changed files
with
188 additions
and
82 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
43 changes: 0 additions & 43 deletions
43
src/main/java/staffconnect/model/person/PersonHasTagPredicate.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/main/java/staffconnect/model/person/PersonHasTagsPredicate.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,54 @@ | ||
package staffconnect.model.person; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import staffconnect.commons.util.ToStringBuilder; | ||
import staffconnect.model.tag.Tag; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Tag} matches any of the tag names given. | ||
*/ | ||
public class PersonHasTagsPredicate implements Predicate<Person> { | ||
private final Set<Tag> tags; // TODO: change to multiple ss in later iterations | ||
|
||
public PersonHasTagsPredicate(Set<Tag> tags) { | ||
this.tags = tags; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
// get list of person tags | ||
List<String> personTags = person.getTags().stream().map(t -> t.tagName.toLowerCase()) | ||
.collect(Collectors.toList()); | ||
// get stream of tags to filter from | ||
Stream<String> tagsToFilter = tags.stream().map(t -> t.tagName.toLowerCase()) | ||
.collect(Collectors.toList()).stream(); | ||
// check if the person DOES NOT contain any of the tags to filter from, if true | ||
// then predicate is not satisfied | ||
return !tagsToFilter.anyMatch(tag -> !personTags.contains(tag)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof PersonHasTagsPredicate)) { | ||
return false; | ||
} | ||
|
||
PersonHasTagsPredicate otherPersonHasTagPredicate = (PersonHasTagsPredicate) other; | ||
return tags.equals(otherPersonHasTagPredicate.tags); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("tag name", tags).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
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.