diff options
author | Martin Fietz <Martin.Fietz@gmail.com> | 2017-04-09 19:37:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-09 19:37:38 +0200 |
commit | 7e9bf8ef64bb14dd67bc7e7b56097b5e3ba356ac (patch) | |
tree | 4bff7743eaa46457eb9085431511632eb34c491e | |
parent | 352b6747cf8a90e7a17685135e3339f8b011b09a (diff) | |
parent | c0da3df8929b8a5822724d216a655bb1038118de (diff) | |
download | AntennaPod-7e9bf8ef64bb14dd67bc7e7b56097b5e3ba356ac.zip |
Merge pull request #2265 from orelogo/develop
Improve search by reducing duplication prevension runtime and sorting results lexicographically
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java | 24 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/util/comparator/SearchResultValueComparator.java | 15 |
2 files changed, 20 insertions, 19 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java index 9d136273c..48e574069 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java @@ -4,7 +4,9 @@ import android.content.Context; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; @@ -51,15 +53,17 @@ public class FeedSearcher { task.run(); } try { + Set<Long> set = new HashSet<>(); + for (int i = 0; i < tasks.size(); i++) { FutureTask<List<FeedItem>> task = tasks.get(i); List<FeedItem> items = task.get(); for (FeedItem item : items) { - if (result.isEmpty() || !isDuplicate(result, item)) { + if (!set.contains(item.getId())) { // to prevent duplicate results result.add(new SearchResult(item, values[i], subtitles[i])); + set.add(item.getId()); } } - } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); @@ -67,20 +71,4 @@ public class FeedSearcher { Collections.sort(result, new SearchResultValueComparator()); return result; } - - /** - * Determines if the feed item is already in the search result list. - * - * @param result list of search results - * @param item feed item to validate - * @return true if the feed item is already in the results - */ - private static boolean isDuplicate(List<SearchResult> result, FeedItem item) { - for (SearchResult resultItem : result) { - if (resultItem.getComponent().getId() == item.getId()) { - return true; - } - } - return false; - } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/comparator/SearchResultValueComparator.java b/core/src/main/java/de/danoeh/antennapod/core/util/comparator/SearchResultValueComparator.java index b16e0949d..d23901a45 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/comparator/SearchResultValueComparator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/comparator/SearchResultValueComparator.java @@ -1,14 +1,27 @@ package de.danoeh.antennapod.core.util.comparator; +import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.SearchResult; import java.util.Comparator; public class SearchResultValueComparator implements Comparator<SearchResult> { + /** + * Compare items based, first, on where they were found (ie. title, chapters, or show notes). + * If they were found in the same section, then compare based on the title, in lexicographic + * order. This is still not ideal since, for example, "#12 Example A" would be considered + * before "#8 Example B" due to the fact that "8" has a larger unicode value than "1" + */ @Override public int compare(SearchResult lhs, SearchResult rhs) { - return rhs.getValue() - lhs.getValue(); + int value = rhs.getValue() - lhs.getValue(); + if (value == 0 && lhs.getComponent() instanceof FeedItem && rhs.getComponent() instanceof FeedItem) { + String lhsTitle = ((FeedItem) lhs.getComponent()).getTitle(); + String rhsTitle = ((FeedItem) rhs.getComponent()).getTitle(); + return lhsTitle.compareTo(rhsTitle); + } + return value; } } |