From c55882e7d1c236372c4aa068636dba4a98be2360 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 9 Feb 2016 11:38:40 +0000 Subject: [PATCH] Use collectingAndThen for merging SearchResult objects --- source/net/filebot/WebServices.java | 19 +++++++++++-------- source/net/filebot/media/MediaDetection.java | 8 ++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/source/net/filebot/WebServices.java b/source/net/filebot/WebServices.java index 5d06f15a..d71c7cd4 100644 --- a/source/net/filebot/WebServices.java +++ b/source/net/filebot/WebServices.java @@ -2,12 +2,14 @@ package net.filebot; import static java.util.Arrays.*; import static java.util.Collections.*; +import static java.util.stream.Collectors.*; import static net.filebot.Settings.*; import static net.filebot.media.MediaDetection.*; import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.io.IOException; +import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.Set; @@ -138,6 +140,13 @@ public final class WebServices { return localIndex; } + private SearchResult merge(SearchResult prime, List 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 public List fetchSearchResult(final String query, final Locale locale) throws Exception { // run local search and API search in parallel @@ -145,15 +154,9 @@ public final class WebServices { Future> localSearch = requestThreadPool.submit(() -> getLocalIndex().search(query)); // 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 -> { - 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); + Collection result = StreamEx.of(apiSearch.get()).append(localSearch.get()).groupingBy(SearchResult::getId, collectingAndThen(toList(), group -> merge(group.get(0), group))).values(); - return new TheTVDBSearchResult(name, aliasNames, id); - }).toArray(TheTVDBSearchResult[]::new); - - return sortBySimilarity(asList(result), singleton(query), getSeriesMatchMetric()); + return sortBySimilarity(result, singleton(query), getSeriesMatchMetric()); } } diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 503b8f8d..3ad77f7b 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -2,6 +2,7 @@ package net.filebot.media; import static java.util.Collections.*; import static java.util.regex.Pattern.*; +import static java.util.stream.Collectors.*; import static net.filebot.MediaTypes.*; import static net.filebot.Settings.*; import static net.filebot.similarity.CommonSequenceMatcher.*; @@ -41,7 +42,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; import net.filebot.WebServices; import net.filebot.archive.Archive; @@ -715,10 +715,10 @@ public class MediaDetection { SimilarityComparator comparator = new SimilarityComparator(metric, terms, mapper); // 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 - return options.stream().sorted(comparator).distinct().collect(Collectors.toList()); + return options.stream().sorted(comparator).distinct().collect(toList()); } public static List sortMoviesBySimilarity(Collection options, Collection terms) throws IOException { @@ -744,7 +744,7 @@ public class MediaDetection { } public static List 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 {