Skip to content

Commit 061ac9d

Browse files
committed
Patched search being static.
1 parent 77f2e0e commit 061ac9d

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
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
@@ -44,6 +44,7 @@ public SearchInteractor(DatabaseFetcher fetcher, DatabaseFetcherListener listene
4444
@Override
4545
public SearchPackagesResult search(SearchPackagesInput input) {
4646
var database = fetcher.fetchDatabase(!input.noCache(), listener);
47+
var factory = new SearcherFactory();
4748

4849
if (database == null) {
4950
return SearchPackagesResult.by(SearchPackagesResult.State.FAILED_TO_FETCH_DATABASE);
@@ -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
}

0 commit comments

Comments
 (0)