Skip to content

Commit a3fa53d

Browse files
committed
ModuleSearcher: refactor matching to be defensive
In case the title is null, let's not throw an exception.
1 parent 1aa16fa commit a3fa53d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/java/org/scijava/search/module/ModuleSearcher.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,17 @@ public List<SearchResult> search(final String text, final boolean fuzzy) {
8383

8484
// First, add modules where title starts with the text.
8585
modules.stream() //
86-
.filter(info -> title(info).toLowerCase().startsWith(textLower)) //
86+
.filter(info -> startsWith(info, textLower) ) //
8787
.forEach(matches::add);
8888

8989
// Next, add modules where title has text inside somewhere.
9090
modules.stream() //
91-
.filter(info -> matches(title(info).toLowerCase(), textLower)) //
91+
.filter(info -> hasSubstring(info, textLower)) //
9292
.forEach(matches::add);
9393

9494
// Finally, add modules where menu path has text inside somewhere.
9595
modules.stream() //
96-
.filter(info -> matches(info.getMenuPath().toString().toLowerCase(),
97-
textLower)) //
96+
.filter(info -> hasSubstring(info, textLower)) //
9897
.forEach(matches::add);
9998

10099
// Wrap each matching ModuleInfo in a ModuleSearchResult.
@@ -157,9 +156,16 @@ private boolean isGoodModule(final ModuleInfo info) {
157156
title(info) != null;
158157
}
159158

160-
private boolean matches(final String actual, final String desired) {
161-
// TODO: Implement fuzzy matching option, and maybe case sensitive option.
162-
// Probably put it in the SearchService itself, and make an API toggle.
163-
return actual.matches(".*" + desired + ".*");
159+
private boolean startsWith(final ModuleInfo info, final String desiredLower) {
160+
final String title = title(info);
161+
return title != null && title.toLowerCase().startsWith(desiredLower);
162+
}
163+
164+
private boolean hasSubstring(final ModuleInfo info,
165+
final String desiredLower)
166+
{
167+
final String title = title(info);
168+
return title != null && //
169+
title.toLowerCase().matches(".*" + desiredLower + ".*");
164170
}
165171
}

0 commit comments

Comments
 (0)