From 0a88c2d55fe62ea30512c19c8d747300a6c21607 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 17 Nov 2020 23:29:27 +0100 Subject: [PATCH] Fix various NPE and other potential errors --- .../pandacube/util/search/SearchEngine.java | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main/java/fr/pandacube/util/search/SearchEngine.java b/src/main/java/fr/pandacube/util/search/SearchEngine.java index 287d9fc..47e2279 100644 --- a/src/main/java/fr/pandacube/util/search/SearchEngine.java +++ b/src/main/java/fr/pandacube/util/search/SearchEngine.java @@ -58,6 +58,17 @@ public class SearchEngine { return; } + Set suggestsKw; + try { + suggestsKw = result.getSuggestionKeywords(); + Preconditions.checkNotNull(suggestsKw, "SearchResult instance must provide a non null set of suggestions keywords"); + suggestsKw = new HashSet<>(suggestsKw); + suggestsKw.removeIf(e -> e == null); + } catch (Exception e) { + Log.severe(e); + return; + } + resultSet.add(result); for (String skw : searchKw) { @@ -66,17 +77,7 @@ public class SearchEngine { resultsSearchKeywordsMap.put(result, searchKw); - Set suggestsKw; - try { - suggestsKw = result.getSuggestionKeywords(); - Preconditions.checkNotNull(suggestsKw, "SearchResult instance must provide a non null set of suggestions keywords"); - suggestsKw.removeIf(e -> e == null); - } catch (Exception e) { - Log.severe(e); - return; - } - - resultsSuggestionsKeywordsMap.put(result, new HashSet<>(suggestsKw)); + resultsSuggestionsKeywordsMap.put(result, suggestsKw); for (String skw : suggestsKw) { suggestionsKeywordsResultMap.computeIfAbsent(skw, s -> new HashSet<>()).add(result); @@ -92,19 +93,27 @@ public class SearchEngine { resultSet.remove(result); Set searchKw = resultsSearchKeywordsMap.remove(result); - for (String skw : searchKw) { - Set set = searchKeywordsResultMap.get(skw); - set.remove(result); - if (set.isEmpty()) - searchKeywordsResultMap.remove(skw); + if (searchKw != null) { + for (String skw : searchKw) { + Set set = searchKeywordsResultMap.get(skw); + if (set == null) + continue; + set.remove(result); + if (set.isEmpty()) + searchKeywordsResultMap.remove(skw); + } } Set suggestsKw = resultsSearchKeywordsMap.remove(result); - for (String skw : suggestsKw) { - Set set = suggestionsKeywordsResultMap.get(skw); - set.remove(result); - if (set.isEmpty()) - suggestionsKeywordsResultMap.remove(skw); + if (suggestsKw != null) { + for (String skw : suggestsKw) { + Set set = suggestionsKeywordsResultMap.get(skw); + if (set == null) + continue; + set.remove(result); + if (set.isEmpty()) + suggestionsKeywordsResultMap.remove(skw); + } } resultsSuggestionsKeywordsMap.remove(result);