Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small search handler improvement #143

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor filterPredicate to allow shortcutting in case of early match
this is a minor speed improvement as it will not dive into groups or settings if a higher level already matches the searchText
sclassen committed Oct 21, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit be0e590ed70cc19b2edd5f97726504891cd64754
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dlsc.preferencesfx.util;

import static com.dlsc.preferencesfx.util.Strings.containsIgnoreCase;
import static com.dlsc.preferencesfx.util.Strings.isNullOrEmpty;

import com.dlsc.preferencesfx.model.Category;
import com.dlsc.preferencesfx.model.Group;
@@ -60,27 +61,35 @@ public class SearchHandler {
* If result is true, it will be shown, if the result is false, it will be hidden.
*/
private Predicate<Category> filterPredicate = category -> {
// look in category description for matches
String searchText = model.getSearchText();
boolean categoryMatch = containsIgnoreCase(category.getDescription(), searchText);
boolean settingMatch = false;
boolean groupMatch = false;
if (category.getGroups() != null) {
// look in settings too
settingMatch = category.getGroups().stream()
.map(Group::getSettings) // get settings from groups
.flatMap(Collection::stream) // flatten all lists of settings to settings
.filter(Setting::hasDescription)
.filter(setting -> !Strings.isNullOrEmpty(setting.getDescription()))
.anyMatch(setting -> containsIgnoreCase(setting.getDescription(), searchText));
// look in groups too
groupMatch = category.getGroups().stream()
.filter(group -> !Strings.isNullOrEmpty(group.getDescription()))
.anyMatch(group -> containsIgnoreCase(group.getDescription(), searchText));
}
return categoryMatch || settingMatch || groupMatch;
final String searchText = model.getSearchText();
sclassen marked this conversation as resolved.
Show resolved Hide resolved
return testCategory(category, searchText)
|| testGroups(category, searchText)
|| testSettings(category, searchText);
};

private boolean testCategory(Category category, String searchText) {
return containsIgnoreCase(category.getDescription(), searchText);
}

private boolean testGroups(Category category, String searchText) {
sclassen marked this conversation as resolved.
Show resolved Hide resolved
final List<Group> groups = category.getGroups();
return groups != null && groups.stream()
.map(Group::getDescription)
.filter(description -> !isNullOrEmpty(description))
.anyMatch(description -> containsIgnoreCase(description, searchText));
}

private boolean testSettings(Category category, String searchText) {
sclassen marked this conversation as resolved.
Show resolved Hide resolved
final List<Group> groups = category.getGroups();
return groups != null && groups.stream()
.map(Group::getSettings) // get settings from groups
.flatMap(Collection::stream) // flatten all lists of settings to settings
.filter(Setting::hasDescription)
.map(Setting::getDescription)
.filter(description -> !isNullOrEmpty(description))
.anyMatch(description -> containsIgnoreCase(description, searchText));
}

/**
* Initializes the SearchHandler by initially creating all necessary lists
* for filtering and setting up the bindings.