Skip to content

Commit 713dc7a

Browse files
authored
Merge pull request #99 from CSC207-2022F-UofT/search-notstatic
Search notstatic
2 parents 1f5c1d8 + c0d26b7 commit 713dc7a

File tree

6 files changed

+67
-34
lines changed

6 files changed

+67
-34
lines changed

src/main/java/org/hydev/mcpm/client/search/SearchInteractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class SearchInteractor implements SearchPackagesBoundary {
1313
private final DatabaseFetcher fetcher;
1414
private final DatabaseFetcherListener listener;
15+
private final SearcherFactory factory = new SearcherFactory();
1516

1617
/**
1718
* Creates a new database with the provided database fetcher.
@@ -56,6 +57,6 @@ public SearchPackagesResult search(SearchPackagesInput input) {
5657
var plugins = database.plugins();
5758

5859
return new SearchPackagesResult(SearchPackagesResult.State.SUCCESS,
59-
SearcherFactory.createSearcher(input).getSearchList(searchStr, plugins));
60+
factory.createSearcher(input).getSearchList(searchStr, plugins));
6061
}
6162
}

src/main/java/org/hydev/mcpm/client/search/SearcherByCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class SearcherByCommand implements Searcher {
1515

16-
private static Map<String, List<PluginModel>> commandMap = null;
16+
private Map<String, List<PluginModel>> commandMap = null;
1717

1818
/**
1919
* Returns a dictionary mapping the different commands to the matching plugins.
@@ -52,9 +52,9 @@ public Map<String, List<PluginModel>> constructSearchMaps(List<PluginModel> plug
5252
@Override
5353
public List<PluginModel> getSearchList(String inp, List<PluginModel> plugins) {
5454
// Instantiate if null
55-
if (SearcherByCommand.commandMap == null) {
56-
SearcherByCommand.commandMap = constructSearchMaps(plugins);
55+
if (commandMap == null) {
56+
commandMap = constructSearchMaps(plugins);
5757
}
58-
return SearcherByCommand.commandMap.getOrDefault(inp, List.of());
58+
return commandMap.getOrDefault(inp, List.of());
5959
}
6060
}

src/main/java/org/hydev/mcpm/client/search/SearcherByKeyword.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
public class SearcherByKeyword implements Searcher {
1717

18-
private static Map<String, List<PluginModel>> keywordMap = null;
18+
private Map<String, List<PluginModel>> keywordMap = null;
1919

2020
/**
2121
* Returns a dictionary mapping the different keywords to the matching plugins
@@ -59,13 +59,13 @@ public Map<String, List<PluginModel>> constructSearchMaps(List<PluginModel> plug
5959
public List<PluginModel> getSearchList(String inp, List<PluginModel> plugins) {
6060

6161
// Instantiate if null
62-
if (SearcherByKeyword.keywordMap == null) {
63-
SearcherByKeyword.keywordMap = constructSearchMaps(plugins);
62+
if (keywordMap == null) {
63+
keywordMap = constructSearchMaps(plugins);
6464
}
6565
String [] keywords = inp.split(" "); // Should be a string
66-
Set<PluginModel> res = new HashSet<>(SearcherByKeyword.keywordMap.getOrDefault(keywords[0], List.of()));
66+
Set<PluginModel> res = new HashSet<>(keywordMap.getOrDefault(keywords[0], List.of()));
6767
for (int i = 1; i < keywords.length; i++) {
68-
List<PluginModel> pl = SearcherByKeyword.keywordMap.getOrDefault(keywords[i], List.of());
68+
List<PluginModel> pl = keywordMap.getOrDefault(keywords[i], List.of());
6969
res.retainAll(pl);
7070
if (res.isEmpty())
7171
return List.of();

src/main/java/org/hydev/mcpm/client/search/SearcherByName.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class SearcherByName implements Searcher {
1515

16-
private static Map<String, List<PluginModel>> nameMap = null;
16+
private Map<String, List<PluginModel>> nameMap = null;
1717

1818
/**
1919
* Returns a dictionary mapping the different names to the matching plugins.
@@ -50,9 +50,9 @@ public Map<String, List<PluginModel>> constructSearchMaps(List<PluginModel> plug
5050
@Override
5151
public List<PluginModel> getSearchList(String inp, List<PluginModel> plugins) {
5252
// Instantiate if null
53-
if (SearcherByName.nameMap == null) {
54-
SearcherByName.nameMap = constructSearchMaps(plugins);
53+
if (nameMap == null) {
54+
nameMap = constructSearchMaps(plugins);
5555
}
56-
return SearcherByName.nameMap.getOrDefault(inp, List.of());
56+
return nameMap.getOrDefault(inp, List.of());
5757
}
5858
}

src/main/java/org/hydev/mcpm/client/search/SearcherFactory.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,33 @@
55
*
66
*/
77
public class SearcherFactory {
8+
SearcherByName nameSearcher;
9+
SearcherByKeyword keywordSearcher;
10+
SearcherByCommand commandSearcher;
811

912
/**
1013
* Returns the new searcher object based on the input type.
1114
*
1215
* @param input Contains the search type in particular. See SearchPackagesInput for details.
1316
*/
14-
public static Searcher createSearcher(SearchPackagesInput input) {
15-
return switch (input.type()) {
16-
case BY_NAME ->
17-
new SearcherByName();
18-
19-
case BY_COMMAND ->
20-
new SearcherByCommand();
21-
22-
case BY_KEYWORD ->
23-
new SearcherByKeyword();
24-
25-
};
17+
public Searcher createSearcher(SearchPackagesInput input) {
18+
switch (input.type()) {
19+
case BY_NAME -> {
20+
if (nameSearcher == null)
21+
nameSearcher = new SearcherByName();
22+
return nameSearcher;
23+
}
24+
case BY_COMMAND -> {
25+
if (commandSearcher == null)
26+
commandSearcher = new SearcherByCommand();
27+
return commandSearcher;
28+
}
29+
case BY_KEYWORD -> {
30+
if (keywordSearcher == null)
31+
keywordSearcher = new SearcherByKeyword();
32+
return keywordSearcher;
33+
}
34+
}
35+
return nameSearcher;
2636
}
2737
}

src/test/java/org/hydev/mcpm/client/database/SearchInteractorTest.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
/**
1818
* Test suite for the SearchInteractor class.
19-
*
20-
* @author Jerry Zhu (<a href="https://github.com/jerryzhu509">...</a>)
2119
*/
2220
public class SearchInteractorTest {
2321
private static SearchInteractor database;
@@ -50,6 +48,10 @@ private String formatStr(SearchPackagesResult result, String delim) {
5048
.collect(Collectors.joining(delim));
5149
}
5250

51+
/**
52+
* Tests search by name when there is a match. Using lower-case to ensure that the search is not
53+
* case-sensitive.
54+
*/
5355
@Test
5456
void testSearchByNameSuccessMatch() {
5557
var result = database.search(
@@ -58,9 +60,12 @@ void testSearchByNameSuccessMatch() {
5860
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
5961

6062
var text = formatStr(result, ", ");
61-
assertEquals(text, "Multiverse-Core");
63+
assertEquals("Multiverse-Core", text);
6264
}
6365

66+
/**
67+
* Tests search by name when there is no match. Expect an empty string.
68+
*/
6469
@Test
6570
void testSearchByNameSuccessNoMatch() {
6671
var result = database.search(
@@ -69,9 +74,12 @@ void testSearchByNameSuccessNoMatch() {
6974
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
7075

7176
var text = formatStr(result, ", ");
72-
assertEquals(text, "");
77+
assertEquals("", text);
7378
}
7479

80+
/**
81+
* Tests search by keyword when there is a match.
82+
*/
7583
@Test
7684
void testSearchByKeywordSuccessMatch() {
7785
var result = database.search(
@@ -80,9 +88,12 @@ void testSearchByKeywordSuccessMatch() {
8088
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
8189

8290
var text = formatStr(result, ", ");
83-
assertEquals(text, "Holographic Displays, WorldGuard");
91+
assertEquals("Holographic Displays, WorldGuard", text);
8492
}
8593

94+
/**
95+
* Tests search by keyword when there is no match. Expect an empty string.
96+
*/
8697
@Test
8798
void testSearchByKeywordSuccessNoMatch() {
8899
var result = database.search(
@@ -91,9 +102,13 @@ void testSearchByKeywordSuccessNoMatch() {
91102
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
92103

93104
var text = formatStr(result, ", ");
94-
assertEquals(text, "");
105+
assertEquals("", text);
95106
}
96107

108+
109+
/**
110+
* Tests search by command when there is a match.
111+
*/
97112
@Test
98113
@SuppressWarnings("SpellCheckingInspection")
99114
void testSearchByCommandSuccessMatch() {
@@ -103,9 +118,13 @@ void testSearchByCommandSuccessMatch() {
103118
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
104119

105120
var text = formatStr(result, ", ");
106-
assertEquals(text, "WorldGuard, Holographic Displays");
121+
assertEquals("WorldGuard, Holographic Displays", text);
107122
}
108123

124+
125+
/**
126+
* Tests search by command when there is no match.
127+
*/
109128
@Test
110129
void testSearchByCommandSuccessNoMatch() {
111130
var result = database.search(
@@ -114,9 +133,12 @@ void testSearchByCommandSuccessNoMatch() {
114133
assertEquals(result.state(), SearchPackagesResult.State.SUCCESS);
115134

116135
var text = formatStr(result, ", ");
117-
assertEquals(text, "");
136+
assertEquals("", text);
118137
}
119138

139+
/**
140+
* Tests invalid searches due to empty inputs.
141+
*/
120142
@Test
121143
void testInvalidSearch() {
122144
var result1 = database.search(

0 commit comments

Comments
 (0)