forked from nus-cs2103-AY1920S1/addressbook-level3
/
ContainsKeywordsPredicate.java
75 lines (64 loc) · 2.24 KB
/
ContainsKeywordsPredicate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package io.xpire.model.item;
import static io.xpire.commons.util.CollectionUtil.requireAllNonNull;
import static io.xpire.commons.util.CollectionUtil.stringifyCollection;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.function.Predicate;
import io.xpire.commons.util.StringUtil;
//@@author JermyTan
/**
* Tests if an {@code Item}'s {@code Name} or {@code Tag} matches any of the keywords given.
*/
public class ContainsKeywordsPredicate implements Predicate<Item> {
private final List<String> keywords;
public ContainsKeywordsPredicate(List<String> keywords) {
requireAllNonNull(keywords);
Collections.sort(keywords);
this.keywords = keywords;
}
/**
* Tests if an item contains any of the keywords in its name or tag(s).
*
* @param item Item.
* @return {@code true} if item contains any of the keywords else {@code false}.
*/
@Override
public boolean test(Item item) {
requireNonNull(item);
boolean nameContainsKeywords;
boolean tagsContainsKeywords;
for (String keyword: this.keywords) {
nameContainsKeywords = StringUtil.containsPhraseIgnoreCase(item.getName().toString(), keyword);
tagsContainsKeywords = keyword.startsWith("#")
&& keyword.length() > 1
&& new HashSet<>(stringifyCollection(
item.getTags(), tag -> tag.substring(1))
)
.contains(keyword.substring(1));
if (nameContainsKeywords || tagsContainsKeywords) {
return true;
}
}
return false;
}
public List<String> getKeywords() {
return this.keywords;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof ContainsKeywordsPredicate)) {
return false;
} else {
ContainsKeywordsPredicate other = (ContainsKeywordsPredicate) obj;
return this.keywords.equals(other.keywords);
}
}
@Override
public int hashCode() {
return this.keywords.hashCode();
}
}