Use collectingAndThen for merging SearchResult objects

This commit is contained in:
Reinhard Pointner 2016-02-09 11:38:40 +00:00
parent 38b17a467a
commit c55882e7d1
2 changed files with 15 additions and 12 deletions

View File

@ -2,12 +2,14 @@ package net.filebot;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.media.MediaDetection.*; import static net.filebot.media.MediaDetection.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
@ -138,6 +140,13 @@ public final class WebServices {
return localIndex; return localIndex;
} }
private SearchResult merge(SearchResult prime, List<SearchResult> group) {
int id = prime.getId();
String name = prime.getName();
String[] aliasNames = StreamEx.of(group).flatMap(it -> stream(it.getAliasNames())).remove(name::equals).distinct().toArray(String[]::new);
return new TheTVDBSearchResult(name, aliasNames, id);
}
@Override @Override
public List<SearchResult> fetchSearchResult(final String query, final Locale locale) throws Exception { public List<SearchResult> fetchSearchResult(final String query, final Locale locale) throws Exception {
// run local search and API search in parallel // run local search and API search in parallel
@ -145,15 +154,9 @@ public final class WebServices {
Future<List<SearchResult>> localSearch = requestThreadPool.submit(() -> getLocalIndex().search(query)); Future<List<SearchResult>> localSearch = requestThreadPool.submit(() -> getLocalIndex().search(query));
// combine alias names into a single search results, and keep API search name as primary name // combine alias names into a single search results, and keep API search name as primary name
SearchResult[] result = StreamEx.of(apiSearch.get()).append(localSearch.get()).groupingBy(SearchResult::getId).values().stream().map(group -> { Collection<SearchResult> result = StreamEx.of(apiSearch.get()).append(localSearch.get()).groupingBy(SearchResult::getId, collectingAndThen(toList(), group -> merge(group.get(0), group))).values();
int id = group.get(0).getId();
String name = group.get(0).getName();
String[] aliasNames = StreamEx.of(group).flatMap(it -> stream(it.getAliasNames())).remove(name::equals).distinct().toArray(String[]::new);
return new TheTVDBSearchResult(name, aliasNames, id); return sortBySimilarity(result, singleton(query), getSeriesMatchMetric());
}).toArray(TheTVDBSearchResult[]::new);
return sortBySimilarity(asList(result), singleton(query), getSeriesMatchMetric());
} }
} }

View File

@ -2,6 +2,7 @@ package net.filebot.media;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.regex.Pattern.*; import static java.util.regex.Pattern.*;
import static java.util.stream.Collectors.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.similarity.CommonSequenceMatcher.*; import static net.filebot.similarity.CommonSequenceMatcher.*;
@ -41,7 +42,6 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import net.filebot.WebServices; import net.filebot.WebServices;
import net.filebot.archive.Archive; import net.filebot.archive.Archive;
@ -715,10 +715,10 @@ public class MediaDetection {
SimilarityComparator<SearchResult, String> comparator = new SimilarityComparator<SearchResult, String>(metric, terms, mapper); SimilarityComparator<SearchResult, String> comparator = new SimilarityComparator<SearchResult, String>(metric, terms, mapper);
// DEBUG // DEBUG
// System.out.format("sortBySimilarity %s => %s%n", terms, options.stream().sorted(comparator).distinct().collect(Collectors.toList())); // System.out.format("sortBySimilarity %s => %s%n", terms, options.stream().sorted(comparator).distinct().collect(toList()));
// sort by ranking and remove duplicate entries // sort by ranking and remove duplicate entries
return options.stream().sorted(comparator).distinct().collect(Collectors.toList()); return options.stream().sorted(comparator).distinct().collect(toList());
} }
public static List<Movie> sortMoviesBySimilarity(Collection<Movie> options, Collection<String> terms) throws IOException { public static List<Movie> sortMoviesBySimilarity(Collection<Movie> options, Collection<String> terms) throws IOException {
@ -744,7 +744,7 @@ public class MediaDetection {
} }
public static List<Integer> parseMovieYear(String name) { public static List<Integer> parseMovieYear(String name) {
return matchIntegers(name).stream().filter(year -> 1950 < year && year < 2050).collect(Collectors.toList()); return matchIntegers(name).stream().filter(year -> 1950 < year && year < 2050).collect(toList());
} }
public static String reduceMovieName(String name, boolean strict) throws IOException { public static String reduceMovieName(String name, boolean strict) throws IOException {