1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-12 06:18:01 -05:00

Refactor SearchResult classes

This commit is contained in:
Reinhard Pointner 2016-02-09 09:37:34 +00:00
parent e2a3149d19
commit 7a4f0eb9e2
8 changed files with 45 additions and 153 deletions

View File

@ -2,52 +2,25 @@ package net.filebot.web;
public class AnidbSearchResult extends SearchResult { public class AnidbSearchResult extends SearchResult {
protected int aid;
protected AnidbSearchResult() { protected AnidbSearchResult() {
// used by serializer // used by serializer
} }
public AnidbSearchResult(int aid, String primaryTitle, String[] aliasNames) { public AnidbSearchResult(int aid, String primaryTitle, String[] aliasNames) {
super(primaryTitle, aliasNames); super(aid, primaryTitle, aliasNames);
this.aid = aid;
}
public int getId() {
return aid;
} }
public int getAnimeId() { public int getAnimeId() {
return aid; return id;
}
@Override
public String getName() {
return name;
} }
public String getPrimaryTitle() { public String getPrimaryTitle() {
return name; return name;
} }
@Override
public int hashCode() {
return aid;
}
@Override
public boolean equals(Object object) {
if (object instanceof AnidbSearchResult) {
AnidbSearchResult other = (AnidbSearchResult) object;
return this.aid == other.aid;
}
return false;
}
@Override @Override
public AnidbSearchResult clone() { public AnidbSearchResult clone() {
return new AnidbSearchResult(aid, name, aliasNames); return new AnidbSearchResult(id, name, aliasNames);
} }
} }

View File

@ -1,53 +0,0 @@
package net.filebot.web;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
public class HyperLink extends SearchResult {
protected URL url;
protected HyperLink() {
// used by serializer
}
public HyperLink(String name, URL url) {
super(name, new String[0]);
this.url = url;
}
public URL getURL() {
return url;
}
public URI getURI() {
try {
return url.toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean equals(Object object) {
if (object instanceof HyperLink) {
HyperLink other = (HyperLink) object;
return name.equals(name) && url.toString().equals(other.url.toString());
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { name, url.toString() });
}
@Override
public HyperLink clone() {
return new HyperLink(name, url);
}
}

View File

@ -20,16 +20,12 @@ public class Movie extends SearchResult {
// used by serializer // used by serializer
} }
public Movie(Movie obj) {
this(obj.name, obj.aliasNames, obj.year, obj.imdbId, obj.tmdbId, obj.getLanguage());
}
public Movie(String name, int year, int imdbId, int tmdbId) { public Movie(String name, int year, int imdbId, int tmdbId) {
this(name, new String[0], year, imdbId, tmdbId, null); this(name, null, year, imdbId, tmdbId, null);
} }
public Movie(String name, String[] aliasNames, int year, int imdbId, int tmdbId, Locale locale) { public Movie(String name, String[] aliasNames, int year, int imdbId, int tmdbId, Locale locale) {
super(name, aliasNames); super(tmdbId > 0 ? tmdbId : imdbId > 0 ? imdbId : -1, name, aliasNames);
this.year = year; this.year = year;
this.imdbId = imdbId; this.imdbId = imdbId;
this.tmdbId = tmdbId; this.tmdbId = tmdbId;
@ -99,12 +95,7 @@ public class Movie extends SearchResult {
@Override @Override
public Movie clone() { public Movie clone() {
return new Movie(this); return new Movie(name, aliasNames, year, imdbId, tmdbId, getLanguage());
}
@Override
public int hashCode() {
return tmdbId > 0 ? tmdbId : imdbId > 0 ? imdbId : year;
} }
@Override @Override

View File

@ -14,7 +14,7 @@ public class MoviePart extends Movie {
} }
public MoviePart(Movie movie, int partIndex, int partCount) { public MoviePart(Movie movie, int partIndex, int partCount) {
super(movie); super(movie.getName(), movie.getAliasNames(), movie.getYear(), movie.getImdbId(), movie.getTmdbId(), movie.getLanguage());
this.partIndex = partIndex; this.partIndex = partIndex;
this.partCount = partCount; this.partCount = partCount;
} }
@ -44,7 +44,7 @@ public class MoviePart extends Movie {
@Override @Override
public String toString() { public String toString() {
return String.format("%s (%d) [%d]", name, year, partIndex); return String.format("%s (%d) [CD%d]", name, year, partIndex);
} }
} }

View File

@ -8,6 +8,7 @@ import java.util.List;
public abstract class SearchResult implements Serializable { public abstract class SearchResult implements Serializable {
protected int id;
protected String name; protected String name;
protected String[] aliasNames; protected String[] aliasNames;
@ -15,11 +16,22 @@ public abstract class SearchResult implements Serializable {
// used by serializer // used by serializer
} }
public SearchResult(String name, String[] aliasNames) { public SearchResult(int id, String name) {
this.id = id;
this.name = name;
this.aliasNames = EMPTY_STRING_ARRAY;
}
public SearchResult(int id, String name, String[] aliasNames) {
this.id = id;
this.name = name; this.name = name;
this.aliasNames = (aliasNames == null || aliasNames.length == 0) ? EMPTY_STRING_ARRAY : aliasNames.clone(); this.aliasNames = (aliasNames == null || aliasNames.length == 0) ? EMPTY_STRING_ARRAY : aliasNames.clone();
} }
public int getId() {
return id;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -50,13 +62,26 @@ public abstract class SearchResult implements Serializable {
} }
@Override @Override
public abstract SearchResult clone(); public int hashCode() {
return id;
}
@Override
public boolean equals(Object other) {
if (getClass().isInstance(other)) {
return getId() == ((SearchResult) other).getId();
}
return false;
}
@Override @Override
public String toString() { public String toString() {
return name; return name;
} }
private static final String[] EMPTY_STRING_ARRAY = new String[0]; @Override
public abstract SearchResult clone();
protected static final String[] EMPTY_STRING_ARRAY = new String[0];
} }

View File

@ -2,34 +2,12 @@ package net.filebot.web;
public class TVMazeSearchResult extends SearchResult { public class TVMazeSearchResult extends SearchResult {
protected int id;
protected TVMazeSearchResult() { protected TVMazeSearchResult() {
// used by serializer // used by serializer
} }
public TVMazeSearchResult(int id, String name) { public TVMazeSearchResult(int id, String name) {
super(name, new String[0]); super(id, name);
this.id = id;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object object) {
if (object instanceof TVMazeSearchResult) {
TVMazeSearchResult other = (TVMazeSearchResult) object;
return this.id == other.id;
}
return false;
} }
@Override @Override

View File

@ -410,12 +410,12 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
public List<BannerDescriptor> getBannerList(TheTVDBSearchResult series) throws Exception { public List<BannerDescriptor> getBannerList(TheTVDBSearchResult series) throws Exception {
// check cache first // check cache first
BannerDescriptor[] cachedList = getCache().getData("banners", series.seriesId, null, BannerDescriptor[].class); BannerDescriptor[] cachedList = getCache().getData("banners", series.getId(), null, BannerDescriptor[].class);
if (cachedList != null) { if (cachedList != null) {
return asList(cachedList); return asList(cachedList);
} }
Document dom = getXmlResource(MirrorType.XML, "/api/" + apikey + "/series/" + series.seriesId + "/banners.xml"); Document dom = getXmlResource(MirrorType.XML, "/api/" + apikey + "/series/" + series.getId() + "/banners.xml");
List<Node> nodes = selectNodes("//Banner", dom); List<Node> nodes = selectNodes("//Banner", dom);
List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>(); List<BannerDescriptor> banners = new ArrayList<BannerDescriptor>();
@ -442,7 +442,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
} }
} }
getCache().putData("banners", series.seriesId, null, banners.toArray(new BannerDescriptor[0])); getCache().putData("banners", series.getId(), null, banners.toArray(new BannerDescriptor[0]));
return banners; return banners;
} }

View File

@ -2,47 +2,25 @@ package net.filebot.web;
public class TheTVDBSearchResult extends SearchResult { public class TheTVDBSearchResult extends SearchResult {
protected int seriesId;
protected TheTVDBSearchResult() { protected TheTVDBSearchResult() {
// used by serializer // used by serializer
} }
public TheTVDBSearchResult(String seriesName, int seriesId) { public TheTVDBSearchResult(String seriesName, int seriesId) {
this(seriesName, new String[0], seriesId); super(seriesId, seriesName);
} }
public TheTVDBSearchResult(String seriesName, String[] aliasNames, int seriesId) { public TheTVDBSearchResult(String seriesName, String[] aliasNames, int seriesId) {
super(seriesName, aliasNames); super(seriesId, seriesName, aliasNames);
this.seriesId = seriesId;
}
public int getId() {
return seriesId;
} }
public int getSeriesId() { public int getSeriesId() {
return seriesId; return id;
}
@Override
public int hashCode() {
return seriesId;
}
@Override
public boolean equals(Object object) {
if (object instanceof TheTVDBSearchResult) {
TheTVDBSearchResult other = (TheTVDBSearchResult) object;
return this.seriesId == other.seriesId;
}
return false;
} }
@Override @Override
public TheTVDBSearchResult clone() { public TheTVDBSearchResult clone() {
return new TheTVDBSearchResult(name, aliasNames, seriesId); return new TheTVDBSearchResult(name, aliasNames, id);
} }
} }