From 9667d3d55cbb440883511a7d17284f6880271a25 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 23 Nov 2016 04:34:56 +0800 Subject: [PATCH] Refactor common reload-episode-data tasks --- .../net/filebot/format/MediaBindingBean.java | 21 +++------ source/net/filebot/web/EpisodeUtilities.java | 46 ++++++++++++++----- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 09765a27..71d659ec 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -684,14 +684,9 @@ public class MediaBindingBean { return new DynamicBindings(SortOrder::names, k -> { if (infoObject instanceof Episode) { SortOrder order = SortOrder.forName(k); - SeriesInfo info = getSeriesInfo(); - - List episodeList = getEpisodeListProvider(info.getDatabase()).getEpisodeList(info.getId(), order, new Locale(info.getLanguage())); - Episode episode = createEpisode(episodeList.stream().filter(e -> getEpisodes().contains(e))); - + Episode episode = fetchEpisode(getEpisode(), order, null); return createBindingObject(null, episode, null); } - return undefined(k); }); } @@ -702,15 +697,13 @@ public class MediaBindingBean { Language language = Language.findLanguage(k); if (language != null && infoObject instanceof Movie) { - MovieInfo m = getMovieInfo(language.getLocale(), true); - return createPropertyBindings(m); // TODO use createBindingObject -> BREAKING CHANGE + MovieInfo movie = getMovieInfo(language.getLocale(), true); + return createPropertyBindings(movie); // TODO use createBindingObject -> BREAKING CHANGE } if (language != null && infoObject instanceof Episode) { - SeriesInfo i = getSeriesInfo(); - List es = getEpisodeListProvider(i.getDatabase()).getEpisodeList(i.getId(), SortOrder.forName(i.getOrder()), language.getLocale()); - Episode e = es.stream().filter(it -> getEpisode().getNumbers().equals(it.getNumbers())).findFirst().get(); - return createPropertyBindings(e); // TODO use createBindingObject -> BREAKING CHANGE + Episode episode = fetchEpisode(getEpisode(), null, language.getLocale()); + return createPropertyBindings(episode); // TODO use createBindingObject -> BREAKING CHANGE } return undefined(k); @@ -738,9 +731,7 @@ public class MediaBindingBean { @Define("episodelist") public List getEpisodeList() throws Exception { - SeriesInfo i = getSeriesInfo(); - - return getEpisodeListProvider(i.getDatabase()).getEpisodeList(i.getId(), SortOrder.forName(i.getOrder()), new Locale(i.getLanguage())); + return fetchEpisodeList(getEpisode()); } @Define("sy") diff --git a/source/net/filebot/web/EpisodeUtilities.java b/source/net/filebot/web/EpisodeUtilities.java index de003b51..71c250a1 100644 --- a/source/net/filebot/web/EpisodeUtilities.java +++ b/source/net/filebot/web/EpisodeUtilities.java @@ -2,29 +2,24 @@ package net.filebot.web; import static java.util.Collections.*; import static java.util.stream.Collectors.*; +import static net.filebot.WebServices.*; import java.text.Collator; import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.Locale; -import java.util.NoSuchElementException; import java.util.Set; import java.util.TreeSet; -import java.util.stream.Stream; - -import net.filebot.WebServices; public final class EpisodeUtilities { - public static Episode createEpisode(Stream episode) { - List es = episode.sorted(EPISODE_NUMBERS_COMPARATOR).collect(toList()); - - if (es.isEmpty()) { - throw new NoSuchElementException("No such Episode"); + public static Episode createEpisode(List episode) { + if (episode.isEmpty()) { + throw new IllegalArgumentException("No such Episode"); } - return es.size() == 1 ? es.get(0) : new MultiEpisode(es); + return episode.size() == 1 ? episode.get(0) : new MultiEpisode(episode); } public static List getMultiEpisodeList(Episode e) { @@ -32,7 +27,7 @@ public final class EpisodeUtilities { } public static boolean isAnime(Episode e) { - return WebServices.AniDB.getIdentifier().equals(e.getSeriesInfo().getDatabase()); + return AniDB.getIdentifier().equals(e.getSeriesInfo().getDatabase()); } public static boolean isRegular(Episode e) { @@ -43,6 +38,33 @@ public final class EpisodeUtilities { return e.getAbsolute() != null && e.getSeriesInfo().getOrder() != null && SortOrder.Absolute == SortOrder.valueOf(e.getSeriesInfo().getOrder()); } + public static List fetchEpisodeList(Episode episode) throws Exception { + return fetchEpisodeList(episode, null, null); + } + + public static List fetchEpisodeList(Episode episode, SortOrder preferredSortOrder, Locale preferredLocale) throws Exception { + SeriesInfo info = episode.getSeriesInfo(); + + SortOrder order = preferredSortOrder; + if (order == null) { + order = SortOrder.valueOf(info.getOrder()); // default to original order + } + + Locale locale = preferredLocale; + if (locale == null) { + locale = new Locale(info.getLanguage()); // default to original locale + } + + return getEpisodeListProvider(info.getDatabase()).getEpisodeList(info.getId(), order, locale); + } + + public static Episode fetchEpisode(Episode episode, SortOrder preferredSortOrder, Locale preferredLocale) throws Exception { + List episodeList = fetchEpisodeList(episode, preferredSortOrder, preferredLocale); + List includes = getMultiEpisodeList(episode); + + return createEpisode(episodeList.stream().filter(includes::contains).sorted(EPISODE_NUMBERS_COMPARATOR).collect(toList())); + } + public static Episode getEpisodeByAbsoluteNumber(Episode e, EpisodeListProvider service, SortOrder order) throws Exception { // e.g. match AniDB episode to TheTVDB episode Set seriesNames = getLenientSeriesNameSet(e); @@ -70,7 +92,7 @@ public final class EpisodeUtilities { break; } - return airdateEpisode.size() == 1 ? airdateEpisode.get(0) : new MultiEpisode(airdateEpisode); + return createEpisode(airdateEpisode); } // return episode object as is by default