diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 23c477ab..d00c38af 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -39,6 +39,7 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Function; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -481,45 +482,27 @@ public class MediaDetection { private static final ArrayList> seriesIndex = new ArrayList>(); public static List> getSeriesIndex() throws IOException { - synchronized (seriesIndex) { - if (seriesIndex.isEmpty()) { - seriesIndex.ensureCapacity(100000); - try { - for (SearchResult it : releaseInfo.getTheTVDBIndex()) { - seriesIndex.addAll(HighPerformanceMatcher.prepare(it)); - } - } catch (Exception e) { - // can't load movie index, just try again next time - debug.severe("Failed to load series index: " + e); - - // rely on online search - return emptyList(); - } + return getIndex(() -> { + try { + return releaseInfo.getTheTVDBIndex(); + } catch (Exception e) { + debug.severe("Failed to load series index: " + e.getMessage()); + return new SearchResult[0]; } - return seriesIndex; - } + }, HighPerformanceMatcher::prepare, seriesIndex); } private static final ArrayList> animeIndex = new ArrayList>(); - public static List> getAnimeIndex() throws IOException { - synchronized (animeIndex) { - if (animeIndex.isEmpty()) { - animeIndex.ensureCapacity(50000); - try { - for (SearchResult it : releaseInfo.getAnidbIndex()) { - animeIndex.addAll(HighPerformanceMatcher.prepare(it)); - } - } catch (Exception e) { - // can't load movie index, just try again next time - debug.severe("Failed to load anime index: " + e); - - // rely on online search - return emptyList(); - } + public static List> getAnimeIndex() { + return getIndex(() -> { + try { + return releaseInfo.getAnidbIndex(); + } catch (Exception e) { + debug.severe("Failed to load anime index: " + e.getMessage()); + return new SearchResult[0]; } - return animeIndex; - } + }, HighPerformanceMatcher::prepare, animeIndex); } public static List matchSeriesByName(Collection files, int maxStartIndex, List> index) throws Exception { @@ -840,26 +823,28 @@ public class MediaDetection { private static final ArrayList> movieIndex = new ArrayList>(); - public static List> getMovieIndex() { - synchronized (movieIndex) { - if (movieIndex.isEmpty()) { - movieIndex.ensureCapacity(100000); - try { - for (Movie it : releaseInfo.getMovieList()) { - movieIndex.addAll(HighPerformanceMatcher.prepare(it)); - } - } catch (Exception e) { - // can't load movie index, just try again next time - debug.severe("Failed to load movie index: " + e); - - // if we can't use internal index we can only rely on online search - return emptyList(); - } + private static List> getIndex(Supplier function, Function>> mapper, ArrayList> sink) { + synchronized (sink) { + if (sink.isEmpty()) { + T[] index = function.get(); + sink.ensureCapacity(index.length * 4); // alias names + stream(index).map(mapper).forEach(sink::addAll); } - return movieIndex; + return sink; } } + public static List> getMovieIndex() { + return getIndex(() -> { + try { + return releaseInfo.getMovieList(); + } catch (Exception e) { + debug.severe("Failed to load movie index: " + e.getMessage()); + return new Movie[0]; + } + }, HighPerformanceMatcher::prepare, movieIndex); + } + public static List matchMovieName(Collection files, boolean strict, int maxStartIndex) { // cross-reference file / folder name with movie list final HighPerformanceMatcher nameMatcher = new HighPerformanceMatcher(maxStartIndex);