* less code duplication, renamed interfaces *Client to *Provider

This commit is contained in:
Reinhard Pointner 2009-03-18 20:09:45 +00:00
parent df143e0305
commit 480c848bda
16 changed files with 137 additions and 145 deletions

View File

@ -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<EpisodeListClient, Episode> {
public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, Episode> {
private SeasonSpinnerModel seasonSpinnerModel = new SeasonSpinnerModel();
@ -69,8 +69,8 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
@Override
protected List<EpisodeListClient> createSearchEngines() {
List<EpisodeListClient> engines = new ArrayList<EpisodeListClient>(4);
protected List<EpisodeListProvider> createSearchEngines() {
List<EpisodeListProvider> engines = new ArrayList<EpisodeListProvider>(4);
engines.add(new TVRageClient());
engines.add(new AnidbClient());
@ -83,8 +83,8 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
@Override
protected LabelProvider<EpisodeListClient> createSearchEngineLabelProvider() {
return SimpleLabelProvider.forClass(EpisodeListClient.class);
protected LabelProvider<EpisodeListProvider> createSearchEngineLabelProvider() {
return SimpleLabelProvider.forClass(EpisodeListProvider.class);
}
@ -96,20 +96,20 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
@Override
protected EpisodeListRequestProcessor createRequestProcessor() {
EpisodeListClient client = searchTextField.getSelectButton().getSelectedValue();
EpisodeListProvider provider = searchTextField.getSelectButton().getSelectedValue();
String text = searchTextField.getText().trim();
int season = seasonSpinnerModel.getSeason();
return new EpisodeListRequestProcessor(new EpisodeListRequest(client, text, season));
return new EpisodeListRequestProcessor(new EpisodeListRequest(provider, text, season));
};
private final PropertyChangeListener selectButtonListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
EpisodeListClient client = searchTextField.getSelectButton().getSelectedValue();
EpisodeListProvider provider = searchTextField.getSelectButton().getSelectedValue();
// lock season spinner on "All Seasons" if client doesn't support fetching of single seasons
if (!client.hasSingleSeasonSupport()) {
// lock season spinner on "All Seasons" if provider doesn't support fetching of single seasons
if (!provider.hasSingleSeasonSupport()) {
seasonSpinnerModel.lock(ALL_SEASONS);
} else {
seasonSpinnerModel.unlock();
@ -175,19 +175,19 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
protected static class EpisodeListRequest extends Request {
private final EpisodeListClient client;
private final EpisodeListProvider provider;
private final int season;
public EpisodeListRequest(EpisodeListClient client, String searchText, int season) {
public EpisodeListRequest(EpisodeListProvider provider, String searchText, int season) {
super(searchText);
this.client = client;
this.provider = provider;
this.season = season;
}
public EpisodeListClient getClient() {
return client;
public EpisodeListProvider getProvider() {
return provider;
}
@ -207,26 +207,26 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
@Override
public Collection<SearchResult> search() throws Exception {
return request.getClient().search(request.getSearchText());
return request.getProvider().search(request.getSearchText());
}
@Override
public Collection<Episode> 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<EpisodeListClient, Epi
@Override
public Icon getIcon() {
return request.getClient().getIcon();
return request.getProvider().getIcon();
}

View File

@ -27,7 +27,7 @@ import net.sourceforge.filebot.similarity.Matcher;
import net.sourceforge.filebot.similarity.SeriesNameMatcher;
import net.sourceforge.filebot.similarity.SimilarityMetric;
import net.sourceforge.filebot.web.Episode;
import net.sourceforge.filebot.web.EpisodeListClient;
import net.sourceforge.filebot.web.EpisodeListProvider;
import net.sourceforge.filebot.web.SearchResult;
import net.sourceforge.tuned.FileUtilities;
@ -36,13 +36,13 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
private final List<File> files;
private final EpisodeListClient client;
private final EpisodeListProvider provider;
private final Collection<SimilarityMetric> metrics;
public AutoFetchEpisodeListMatcher(EpisodeListClient client, List<File> files, Collection<SimilarityMetric> metrics) {
this.client = client;
public AutoFetchEpisodeListMatcher(EpisodeListProvider provider, List<File> files, Collection<SimilarityMetric> metrics) {
this.provider = provider;
this.files = new LinkedList<File>(files);
this.metrics = new ArrayList<SimilarityMetric>(metrics);
}
@ -79,13 +79,13 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
@Override
public Collection<Episode> call() throws Exception {
Collection<SearchResult> results = client.search(seriesName);
Collection<SearchResult> results = provider.search(seriesName);
if (results.size() > 0) {
SearchResult selectedSearchResult = selectSearchResult(seriesName, results);
if (selectedSearchResult != null) {
return client.getEpisodeList(selectedSearchResult);
return provider.getEpisodeList(selectedSearchResult);
}
}

View File

@ -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() {

View File

@ -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<SubtitleClient, SubtitlePackage> {
public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, SubtitlePackage> {
public SubtitlePanel() {
historyPanel.setColumnHeader(0, "Show / Movie");
@ -35,8 +35,8 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
@Override
protected List<SubtitleClient> createSearchEngines() {
List<SubtitleClient> engines = new ArrayList<SubtitleClient>(2);
protected List<SubtitleProvider> createSearchEngines() {
List<SubtitleProvider> engines = new ArrayList<SubtitleProvider>(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<SubtitleClient, SubtitleP
@Override
protected LabelProvider<SubtitleClient> createSearchEngineLabelProvider() {
return SimpleLabelProvider.forClass(SubtitleClient.class);
protected LabelProvider<SubtitleProvider> createSearchEngineLabelProvider() {
return SimpleLabelProvider.forClass(SubtitleProvider.class);
}
@ -60,31 +60,31 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
@Override
protected SubtitleRequestProcessor createRequestProcessor() {
SubtitleClient client = searchTextField.getSelectButton().getSelectedValue();
SubtitleProvider provider = searchTextField.getSelectButton().getSelectedValue();
String text = searchTextField.getText().trim();
//TODO language selection combobox
Locale language = Locale.ENGLISH;
return new SubtitleRequestProcessor(new SubtitleRequest(client, text, language));
return new SubtitleRequestProcessor(new SubtitleRequest(provider, text, language));
}
protected static class SubtitleRequest extends Request {
private final SubtitleClient client;
private final SubtitleProvider provider;
private final Locale language;
public SubtitleRequest(SubtitleClient client, String searchText, Locale language) {
public SubtitleRequest(SubtitleProvider provider, String searchText, Locale language) {
super(searchText);
this.client = client;
this.provider = provider;
this.language = language;
}
public SubtitleClient getClient() {
return client;
public SubtitleProvider getProvider() {
return provider;
}
@ -104,7 +104,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
@Override
public Collection<SearchResult> search() throws Exception {
return request.getClient().search(request.getSearchText());
return request.getProvider().search(request.getSearchText());
}
@ -112,7 +112,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
public Collection<SubtitlePackage> fetch() throws Exception {
List<SubtitlePackage> packages = new ArrayList<SubtitlePackage>(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<SubtitleClient, SubtitleP
@Override
public URI getLink() {
return request.getClient().getSubtitleListLink(getSearchResult(), request.getLanguage());
return request.getProvider().getSubtitleListLink(getSearchResult(), request.getLanguage());
}
@ -146,7 +146,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
@Override
public Icon getIcon() {
return request.client.getIcon();
return request.provider.getIcon();
}

View File

@ -29,7 +29,7 @@ import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class AnidbClient implements EpisodeListClient {
public class AnidbClient implements EpisodeListProvider {
private static final String host = "anidb.net";

View File

@ -8,7 +8,7 @@ import java.util.List;
import javax.swing.Icon;
public interface EpisodeListClient {
public interface EpisodeListProvider {
public List<SearchResult> search(String query) throws Exception;

View File

@ -0,0 +1,49 @@
package net.sourceforge.filebot.web;
import java.util.ArrayList;
import java.util.List;
public final class EpisodeListUtilities {
public static List<Episode> filterBySeason(Iterable<Episode> episodes, int season) {
List<Episode> results = new ArrayList<Episode>(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<Episode> 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();
}
}

View File

@ -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<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception {
List<Episode> all = getEpisodeList(searchResult);
List<Episode> eps = filterBySeason(all, season);
List<Episode> episodes = new ArrayList<Episode>(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;
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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";

View File

@ -9,7 +9,7 @@ import java.util.Locale;
import javax.swing.Icon;
public interface SubtitleClient {
public interface SubtitleProvider {
public List<SearchResult> search(String query) throws Exception;

View File

@ -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";

View File

@ -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";

View File

@ -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<Episode> getEpisodeList(SearchResult searchResult, int season) throws IOException, SAXException, ParserConfigurationException {
List<Episode> all = getEpisodeList(searchResult);
List<Episode> eps = filterBySeason(all, season);
List<Episode> episodes = new ArrayList<Episode>(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;
}

View File

@ -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<Episode> getEpisodeList(SearchResult searchResult, int season) throws Exception {
List<Episode> all = getEpisodeList(searchResult);
List<Episode> eps = filterBySeason(all, season);
List<Episode> episodes = new ArrayList<Episode>(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;
}