Merge pull request #2265 from orelogo/develop

Improve search by reducing duplication prevension runtime and sorting results lexicographically
This commit is contained in:
Martin Fietz 2017-04-09 19:37:38 +02:00 committed by GitHub
commit 7e9bf8ef64
2 changed files with 20 additions and 19 deletions

View File

@ -4,7 +4,9 @@ import android.content.Context;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
@ -51,15 +53,17 @@ public class FeedSearcher {
task.run(); task.run();
} }
try { try {
Set<Long> set = new HashSet<>();
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
FutureTask<List<FeedItem>> task = tasks.get(i); FutureTask<List<FeedItem>> task = tasks.get(i);
List<FeedItem> items = task.get(); List<FeedItem> items = task.get();
for (FeedItem item : items) { 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])); result.add(new SearchResult(item, values[i], subtitles[i]));
set.add(item.getId());
} }
} }
} }
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
@ -67,20 +71,4 @@ public class FeedSearcher {
Collections.sort(result, new SearchResultValueComparator()); Collections.sort(result, new SearchResultValueComparator());
return result; 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;
}
} }

View File

@ -1,14 +1,27 @@
package de.danoeh.antennapod.core.util.comparator; package de.danoeh.antennapod.core.util.comparator;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.SearchResult; import de.danoeh.antennapod.core.feed.SearchResult;
import java.util.Comparator; import java.util.Comparator;
public class SearchResultValueComparator implements Comparator<SearchResult> { 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 @Override
public int compare(SearchResult lhs, SearchResult rhs) { 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;
} }
} }