From 480c848bda41561e9470b540a3643f568e15e06c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 18 Mar 2009 20:09:45 +0000 Subject: [PATCH] * less code duplication, renamed interfaces *Client to *Provider --- .../panel/episodelist/EpisodeListPanel.java | 44 ++++++++--------- .../rename/AutoFetchEpisodeListMatcher.java | 12 ++--- .../filebot/ui/panel/rename/RenamePanel.java | 12 ++--- .../ui/panel/subtitle/SubtitlePanel.java | 34 ++++++------- .../sourceforge/filebot/web/AnidbClient.java | 2 +- ...stClient.java => EpisodeListProvider.java} | 2 +- .../filebot/web/EpisodeListUtilities.java | 49 +++++++++++++++++++ .../sourceforge/filebot/web/IMDbClient.java | 36 +++----------- .../web/OpenSubtitlesSubtitleClient.java | 2 +- .../web/SeasonOutOfBoundsException.java | 12 ++--- .../filebot/web/SubsceneSubtitleClient.java | 2 +- ...titleClient.java => SubtitleProvider.java} | 2 +- .../filebot/web/SubtitleSourceClient.java | 2 +- .../filebot/web/TVDotComClient.java | 2 +- .../sourceforge/filebot/web/TVRageClient.java | 35 +++---------- .../filebot/web/TheTVDBClient.java | 34 +++---------- 16 files changed, 137 insertions(+), 145 deletions(-) rename source/net/sourceforge/filebot/web/{EpisodeListClient.java => EpisodeListProvider.java} (93%) create mode 100644 source/net/sourceforge/filebot/web/EpisodeListUtilities.java rename source/net/sourceforge/filebot/web/{SubtitleClient.java => SubtitleProvider.java} (92%) diff --git a/source/net/sourceforge/filebot/ui/panel/episodelist/EpisodeListPanel.java b/source/net/sourceforge/filebot/ui/panel/episodelist/EpisodeListPanel.java index ea9f45fd..3dba0050 100644 --- a/source/net/sourceforge/filebot/ui/panel/episodelist/EpisodeListPanel.java +++ b/source/net/sourceforge/filebot/ui/panel/episodelist/EpisodeListPanel.java @@ -29,7 +29,7 @@ import net.sourceforge.filebot.ui.transfer.FileExportHandler; import net.sourceforge.filebot.ui.transfer.SaveAction; import net.sourceforge.filebot.web.AnidbClient; import net.sourceforge.filebot.web.Episode; -import net.sourceforge.filebot.web.EpisodeListClient; +import net.sourceforge.filebot.web.EpisodeListProvider; import net.sourceforge.filebot.web.IMDbClient; import net.sourceforge.filebot.web.SearchResult; import net.sourceforge.filebot.web.TVDotComClient; @@ -41,7 +41,7 @@ import net.sourceforge.tuned.ui.SimpleLabelProvider; import net.sourceforge.tuned.ui.TunedUtilities; -public class EpisodeListPanel extends AbstractSearchPanel { +public class EpisodeListPanel extends AbstractSearchPanel { private SeasonSpinnerModel seasonSpinnerModel = new SeasonSpinnerModel(); @@ -69,8 +69,8 @@ public class EpisodeListPanel extends AbstractSearchPanel createSearchEngines() { - List engines = new ArrayList(4); + protected List createSearchEngines() { + List engines = new ArrayList(4); engines.add(new TVRageClient()); engines.add(new AnidbClient()); @@ -83,8 +83,8 @@ public class EpisodeListPanel extends AbstractSearchPanel createSearchEngineLabelProvider() { - return SimpleLabelProvider.forClass(EpisodeListClient.class); + protected LabelProvider createSearchEngineLabelProvider() { + return SimpleLabelProvider.forClass(EpisodeListProvider.class); } @@ -96,20 +96,20 @@ public class EpisodeListPanel extends AbstractSearchPanel search() throws Exception { - return request.getClient().search(request.getSearchText()); + return request.getProvider().search(request.getSearchText()); } @Override public Collection fetch() throws Exception { if (request.getSeason() != ALL_SEASONS) - return request.getClient().getEpisodeList(getSearchResult(), request.getSeason()); + return request.getProvider().getEpisodeList(getSearchResult(), request.getSeason()); else - return request.getClient().getEpisodeList(getSearchResult()); + return request.getProvider().getEpisodeList(getSearchResult()); } @Override public URI getLink() { if (request.getSeason() != ALL_SEASONS) { - return request.getClient().getEpisodeListLink(getSearchResult(), request.getSeason()); + return request.getProvider().getEpisodeListLink(getSearchResult(), request.getSeason()); } - return request.getClient().getEpisodeListLink(getSearchResult()); + return request.getProvider().getEpisodeListLink(getSearchResult()); } @@ -263,7 +263,7 @@ public class EpisodeListPanel extends AbstractSearchPanel> private final List files; - private final EpisodeListClient client; + private final EpisodeListProvider provider; private final Collection metrics; - public AutoFetchEpisodeListMatcher(EpisodeListClient client, List files, Collection metrics) { - this.client = client; + public AutoFetchEpisodeListMatcher(EpisodeListProvider provider, List files, Collection metrics) { + this.provider = provider; this.files = new LinkedList(files); this.metrics = new ArrayList(metrics); } @@ -79,13 +79,13 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> @Override public Collection call() throws Exception { - Collection results = client.search(seriesName); + Collection results = provider.search(seriesName); if (results.size() > 0) { SearchResult selectedSearchResult = selectSearchResult(seriesName, results); if (selectedSearchResult != null) { - return client.getEpisodeList(selectedSearchResult); + return provider.getEpisodeList(selectedSearchResult); } } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java b/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java index f0389c57..c03153b6 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java @@ -41,7 +41,7 @@ import net.sourceforge.filebot.ui.EpisodeFormatDialog; import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.web.AnidbClient; import net.sourceforge.filebot.web.Episode; -import net.sourceforge.filebot.web.EpisodeListClient; +import net.sourceforge.filebot.web.EpisodeListProvider; import net.sourceforge.filebot.web.IMDbClient; import net.sourceforge.filebot.web.SearchResult; import net.sourceforge.filebot.web.TVDotComClient; @@ -201,13 +201,13 @@ public class RenamePanel extends JComponent { protected class AutoFetchEpisodeListAction extends AbstractAction { - private final EpisodeListClient client; + private final EpisodeListProvider provider; - public AutoFetchEpisodeListAction(EpisodeListClient client) { - super(client.getName(), client.getIcon()); + public AutoFetchEpisodeListAction(EpisodeListProvider provider) { + super(provider.getName(), provider.getIcon()); - this.client = client; + this.provider = provider; // disable action while episode list matcher is working namesList.addPropertyChangeListener(LOADING_PROPERTY, new PropertyChangeListener() { @@ -229,7 +229,7 @@ public class RenamePanel extends JComponent { // clear names list model.names().clear(); - AutoFetchEpisodeListMatcher worker = new AutoFetchEpisodeListMatcher(client, model.files(), matchAction.getMetrics()) { + AutoFetchEpisodeListMatcher worker = new AutoFetchEpisodeListMatcher(provider, model.files(), matchAction.getMetrics()) { @Override protected void done() { diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java index feca02c6..f410d089 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java @@ -19,14 +19,14 @@ import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.web.OpenSubtitlesSubtitleClient; import net.sourceforge.filebot.web.SearchResult; import net.sourceforge.filebot.web.SubsceneSubtitleClient; -import net.sourceforge.filebot.web.SubtitleClient; import net.sourceforge.filebot.web.SubtitleDescriptor; +import net.sourceforge.filebot.web.SubtitleProvider; import net.sourceforge.filebot.web.SubtitleSourceClient; import net.sourceforge.tuned.ui.LabelProvider; import net.sourceforge.tuned.ui.SimpleLabelProvider; -public class SubtitlePanel extends AbstractSearchPanel { +public class SubtitlePanel extends AbstractSearchPanel { public SubtitlePanel() { historyPanel.setColumnHeader(0, "Show / Movie"); @@ -35,8 +35,8 @@ public class SubtitlePanel extends AbstractSearchPanel createSearchEngines() { - List engines = new ArrayList(2); + protected List createSearchEngines() { + List engines = new ArrayList(2); engines.add(new OpenSubtitlesSubtitleClient(String.format("%s v%s", getApplicationName(), getApplicationVersion()))); engines.add(new SubsceneSubtitleClient()); @@ -47,8 +47,8 @@ public class SubtitlePanel extends AbstractSearchPanel createSearchEngineLabelProvider() { - return SimpleLabelProvider.forClass(SubtitleClient.class); + protected LabelProvider createSearchEngineLabelProvider() { + return SimpleLabelProvider.forClass(SubtitleProvider.class); } @@ -60,31 +60,31 @@ public class SubtitlePanel extends AbstractSearchPanel search() throws Exception { - return request.getClient().search(request.getSearchText()); + return request.getProvider().search(request.getSearchText()); } @@ -112,7 +112,7 @@ public class SubtitlePanel extends AbstractSearchPanel fetch() throws Exception { List packages = new ArrayList(20); - for (SubtitleDescriptor subtitle : request.getClient().getSubtitleList(getSearchResult(), request.getLanguage())) { + for (SubtitleDescriptor subtitle : request.getProvider().getSubtitleList(getSearchResult(), request.getLanguage())) { packages.add(new SubtitlePackage(subtitle)); } @@ -122,7 +122,7 @@ public class SubtitlePanel extends AbstractSearchPanel search(String query) throws Exception; diff --git a/source/net/sourceforge/filebot/web/EpisodeListUtilities.java b/source/net/sourceforge/filebot/web/EpisodeListUtilities.java new file mode 100644 index 00000000..a2962ca1 --- /dev/null +++ b/source/net/sourceforge/filebot/web/EpisodeListUtilities.java @@ -0,0 +1,49 @@ + +package net.sourceforge.filebot.web; + + +import java.util.ArrayList; +import java.util.List; + + +public final class EpisodeListUtilities { + + public static List filterBySeason(Iterable episodes, int season) { + + List results = new ArrayList(25); + + // filter given season from all seasons + for (Episode episode : episodes) { + try { + if (season == Integer.parseInt(episode.getSeasonNumber())) { + results.add(episode); + } + } catch (NumberFormatException e) { + // ignore illegal episodes + } + } + + return results; + } + + + public static int getLastSeason(Iterable episodes) { + int lastSeason = 0; + + // filter given season from all seasons + for (Episode episode : episodes) { + try { + lastSeason = Math.max(lastSeason, Integer.parseInt(episode.getSeasonNumber())); + } catch (NumberFormatException e) { + // ignore illegal episodes + } + } + + return lastSeason; + } + + + private EpisodeListUtilities() { + throw new UnsupportedOperationException(); + } +} diff --git a/source/net/sourceforge/filebot/web/IMDbClient.java b/source/net/sourceforge/filebot/web/IMDbClient.java index 4e0467ab..7b2f43b8 100644 --- a/source/net/sourceforge/filebot/web/IMDbClient.java +++ b/source/net/sourceforge/filebot/web/IMDbClient.java @@ -2,6 +2,8 @@ package net.sourceforge.filebot.web; +import static net.sourceforge.filebot.web.EpisodeListUtilities.filterBySeason; +import static net.sourceforge.filebot.web.EpisodeListUtilities.getLastSeason; import static net.sourceforge.filebot.web.WebRequest.getHtmlDocument; import static net.sourceforge.tuned.XPathUtilities.getAttribute; import static net.sourceforge.tuned.XPathUtilities.selectNodes; @@ -15,8 +17,6 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Scanner; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.Icon; @@ -27,7 +27,7 @@ import org.w3c.dom.Node; import org.xml.sax.SAXException; -public class IMDbClient implements EpisodeListClient { +public class IMDbClient implements EpisodeListProvider { private static final String host = "www.imdb.com"; @@ -102,34 +102,14 @@ public class IMDbClient implements EpisodeListClient { @Override public List getEpisodeList(SearchResult searchResult, int season) throws Exception { + List all = getEpisodeList(searchResult); + List eps = filterBySeason(all, season); - List episodes = new ArrayList(25); - - // remember max. season, so we can throw a proper exception, in case an illegal season number was requested - int maxSeason = 0; - - // filter given season from all seasons - for (Episode episode : getEpisodeList(searchResult)) { - try { - int seasonNumber = Integer.parseInt(episode.getSeasonNumber()); - - if (season == seasonNumber) { - episodes.add(episode); - } - - if (seasonNumber > maxSeason) { - maxSeason = seasonNumber; - } - } catch (NumberFormatException e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal season number", e); - } + if (eps.isEmpty()) { + throw new SeasonOutOfBoundsException(searchResult.getName(), season, getLastSeason(all)); } - if (episodes.isEmpty()) { - throw new SeasonOutOfBoundsException(searchResult.getName(), season, maxSeason); - } - - return episodes; + return eps; } diff --git a/source/net/sourceforge/filebot/web/OpenSubtitlesSubtitleClient.java b/source/net/sourceforge/filebot/web/OpenSubtitlesSubtitleClient.java index 8a22642b..c0a048c2 100644 --- a/source/net/sourceforge/filebot/web/OpenSubtitlesSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/OpenSubtitlesSubtitleClient.java @@ -18,7 +18,7 @@ import net.sourceforge.tuned.Timer; /** * SubtitleClient for OpenSubtitles. */ -public class OpenSubtitlesSubtitleClient implements SubtitleClient { +public class OpenSubtitlesSubtitleClient implements SubtitleProvider { private final OpenSubtitlesClient client; diff --git a/source/net/sourceforge/filebot/web/SeasonOutOfBoundsException.java b/source/net/sourceforge/filebot/web/SeasonOutOfBoundsException.java index 9d82b5be..429ddb3d 100644 --- a/source/net/sourceforge/filebot/web/SeasonOutOfBoundsException.java +++ b/source/net/sourceforge/filebot/web/SeasonOutOfBoundsException.java @@ -6,19 +6,19 @@ public class SeasonOutOfBoundsException extends IndexOutOfBoundsException { private final String seriesName; private final int season; - private final int maxSeason; + private final int lastSeason; - public SeasonOutOfBoundsException(String seriesName, int season, int maxSeason) { + public SeasonOutOfBoundsException(String seriesName, int season, int lastSeason) { this.seriesName = seriesName; this.season = season; - this.maxSeason = maxSeason; + this.lastSeason = lastSeason; } @Override public String getMessage() { - return String.format("%s has only %d season%s.", seriesName, maxSeason, maxSeason != 1 ? "s" : ""); + return String.format("%s has only %d season%s.", seriesName, lastSeason, lastSeason != 1 ? "s" : ""); } @@ -32,8 +32,8 @@ public class SeasonOutOfBoundsException extends IndexOutOfBoundsException { } - public int getMaxSeason() { - return maxSeason; + public int getLastSeason() { + return lastSeason; } } diff --git a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java index 23864322..d1663249 100644 --- a/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SubsceneSubtitleClient.java @@ -38,7 +38,7 @@ import org.w3c.dom.Node; import org.xml.sax.SAXException; -public class SubsceneSubtitleClient implements SubtitleClient { +public class SubsceneSubtitleClient implements SubtitleProvider { private static final String host = "subscene.com"; diff --git a/source/net/sourceforge/filebot/web/SubtitleClient.java b/source/net/sourceforge/filebot/web/SubtitleProvider.java similarity index 92% rename from source/net/sourceforge/filebot/web/SubtitleClient.java rename to source/net/sourceforge/filebot/web/SubtitleProvider.java index 52ada293..8bfaf579 100644 --- a/source/net/sourceforge/filebot/web/SubtitleClient.java +++ b/source/net/sourceforge/filebot/web/SubtitleProvider.java @@ -9,7 +9,7 @@ import java.util.Locale; import javax.swing.Icon; -public interface SubtitleClient { +public interface SubtitleProvider { public List search(String query) throws Exception; diff --git a/source/net/sourceforge/filebot/web/SubtitleSourceClient.java b/source/net/sourceforge/filebot/web/SubtitleSourceClient.java index de89a8b9..ebfe79e1 100644 --- a/source/net/sourceforge/filebot/web/SubtitleSourceClient.java +++ b/source/net/sourceforge/filebot/web/SubtitleSourceClient.java @@ -25,7 +25,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; -public class SubtitleSourceClient implements SubtitleClient { +public class SubtitleSourceClient implements SubtitleProvider { protected static final String HOST = "www.subtitlesource.org"; diff --git a/source/net/sourceforge/filebot/web/TVDotComClient.java b/source/net/sourceforge/filebot/web/TVDotComClient.java index 07606d18..1cc00d18 100644 --- a/source/net/sourceforge/filebot/web/TVDotComClient.java +++ b/source/net/sourceforge/filebot/web/TVDotComClient.java @@ -30,7 +30,7 @@ import org.w3c.dom.Node; import org.xml.sax.SAXException; -public class TVDotComClient implements EpisodeListClient { +public class TVDotComClient implements EpisodeListProvider { private static final String host = "www.tv.com"; diff --git a/source/net/sourceforge/filebot/web/TVRageClient.java b/source/net/sourceforge/filebot/web/TVRageClient.java index c7631eba..201b5cfe 100644 --- a/source/net/sourceforge/filebot/web/TVRageClient.java +++ b/source/net/sourceforge/filebot/web/TVRageClient.java @@ -2,6 +2,8 @@ package net.sourceforge.filebot.web; +import static net.sourceforge.filebot.web.EpisodeListUtilities.filterBySeason; +import static net.sourceforge.filebot.web.EpisodeListUtilities.getLastSeason; import static net.sourceforge.filebot.web.WebRequest.getDocument; import static net.sourceforge.tuned.XPathUtilities.getTextContent; import static net.sourceforge.tuned.XPathUtilities.selectNodes; @@ -13,8 +15,6 @@ import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.Icon; import javax.xml.parsers.ParserConfigurationException; @@ -29,7 +29,7 @@ import org.w3c.dom.Node; import org.xml.sax.SAXException; -public class TVRageClient implements EpisodeListClient { +public class TVRageClient implements EpisodeListProvider { private static final String host = "www.tvrage.com"; @@ -115,33 +115,14 @@ public class TVRageClient implements EpisodeListClient { @Override public List getEpisodeList(SearchResult searchResult, int season) throws IOException, SAXException, ParserConfigurationException { + List all = getEpisodeList(searchResult); + List eps = filterBySeason(all, season); - List episodes = new ArrayList(25); - - // remember max. season, so we can throw a proper exception, in case an illegal season number was requested - int maxSeason = 0; - - // filter given season from all seasons - for (Episode episode : getEpisodeList(searchResult)) { - try { - int seasonNumber = Integer.parseInt(episode.getSeasonNumber()); - - if (season == seasonNumber) { - episodes.add(episode); - } - - if (seasonNumber > maxSeason) { - maxSeason = seasonNumber; - } - } catch (NumberFormatException e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal season number", e); - } + if (eps.isEmpty()) { + throw new SeasonOutOfBoundsException(searchResult.getName(), season, getLastSeason(all)); } - if (episodes.isEmpty()) - throw new SeasonOutOfBoundsException(searchResult.getName(), season, maxSeason); - - return episodes; + return eps; } diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index 9974b6c3..f816692b 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -2,6 +2,8 @@ package net.sourceforge.filebot.web; +import static net.sourceforge.filebot.web.EpisodeListUtilities.filterBySeason; +import static net.sourceforge.filebot.web.EpisodeListUtilities.getLastSeason; import static net.sourceforge.filebot.web.WebRequest.getDocument; import static net.sourceforge.tuned.XPathUtilities.getTextContent; import static net.sourceforge.tuned.XPathUtilities.selectNodes; @@ -38,7 +40,7 @@ import org.w3c.dom.Node; import org.xml.sax.SAXException; -public class TheTVDBClient implements EpisodeListClient { +public class TheTVDBClient implements EpisodeListProvider { private static final String host = "www.thetvdb.com"; @@ -110,34 +112,14 @@ public class TheTVDBClient implements EpisodeListClient { @Override public List getEpisodeList(SearchResult searchResult, int season) throws Exception { + List all = getEpisodeList(searchResult); + List eps = filterBySeason(all, season); - List episodes = new ArrayList(25); - - // remember max. season, so we can throw a proper exception, in case an illegal season number was requested - int maxSeason = 0; - - // filter given season from all seasons - for (Episode episode : getEpisodeList(searchResult)) { - try { - int seasonNumber = Integer.parseInt(episode.getSeasonNumber()); - - if (season == seasonNumber) { - episodes.add(episode); - } - - if (seasonNumber > maxSeason) { - maxSeason = seasonNumber; - } - } catch (NumberFormatException e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, "Illegal season number", e); - } + if (eps.isEmpty()) { + throw new SeasonOutOfBoundsException(searchResult.getName(), season, getLastSeason(all)); } - if (episodes.isEmpty()) { - throw new SeasonOutOfBoundsException(searchResult.getName(), season, maxSeason); - } - - return episodes; + return eps; }