From be6f96662ae2765100c8d53c1eced6fd26cc7f9f Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 1 Nov 2016 21:22:36 +0800 Subject: [PATCH] Unify Crew/People into new interface with default methods --- source/net/filebot/web/Crew.java | 44 +++++++++++++++++++++ source/net/filebot/web/EpisodeInfo.java | 17 +------- source/net/filebot/web/Person.java | 9 +++-- source/net/filebot/web/SearchResult.java | 2 +- source/net/filebot/web/TMDbClient.java | 27 ++++--------- source/net/filebot/web/TheTVDBClient.java | 2 +- test/net/filebot/web/TMDbClientTest.java | 2 +- test/net/filebot/web/TheTVDBClientTest.java | 2 +- 8 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 source/net/filebot/web/Crew.java diff --git a/source/net/filebot/web/Crew.java b/source/net/filebot/web/Crew.java new file mode 100644 index 00000000..147a3330 --- /dev/null +++ b/source/net/filebot/web/Crew.java @@ -0,0 +1,44 @@ +package net.filebot.web; + +import static java.util.stream.Collectors.*; + +import java.util.List; +import java.util.function.Predicate; + +public interface Crew { + + List getCrew(); + + default List getCast() { + return getCrew().stream().filter(Person::isActor).collect(toList()); + } + + default List getActors() { + return getCrewNames(Person::isActor); + } + + default List getDirectors() { + return getCrewNames(Person::isDirector); + } + + default List getWriters() { + return getCrewNames(Person::isWriter); + } + + default String getDirector() { + return getCrewName(Person::isDirector); + } + + default String getWriter() { + return getCrewName(Person::isWriter); + } + + default String getCrewName(Predicate filter) { + return getCrew().stream().filter(filter).map(Person::getName).findFirst().orElse(null); + } + + default List getCrewNames(Predicate filter) { + return getCrew().stream().filter(filter).map(Person::getName).collect(toList()); + } + +} diff --git a/source/net/filebot/web/EpisodeInfo.java b/source/net/filebot/web/EpisodeInfo.java index 49ac66c6..309a5c0c 100644 --- a/source/net/filebot/web/EpisodeInfo.java +++ b/source/net/filebot/web/EpisodeInfo.java @@ -2,14 +2,13 @@ package net.filebot.web; import static java.util.Arrays.*; import static java.util.Collections.*; -import static java.util.stream.Collectors.*; import java.io.Serializable; import java.util.List; import java.util.Locale; import java.util.Objects; -public class EpisodeInfo implements Serializable { +public class EpisodeInfo implements Crew, Serializable { protected String database; protected Integer seriesId; @@ -64,22 +63,10 @@ public class EpisodeInfo implements Serializable { return language; } - public List getPeople() { + public List getCrew() { return unmodifiableList(asList(people)); } - public List getDirectors() { - return stream(people).filter(Person::isDirector).map(Person::getName).collect(toList()); - } - - public List getWriters() { - return stream(people).filter(Person::isWriter).map(Person::getName).collect(toList()); - } - - public List getGuestStars() { - return stream(people).filter(Person::isActor).map(Person::getName).collect(toList()); - } - public String getOverview() { return overview; } diff --git a/source/net/filebot/web/Person.java b/source/net/filebot/web/Person.java index 55f76899..5f6cc783 100644 --- a/source/net/filebot/web/Person.java +++ b/source/net/filebot/web/Person.java @@ -15,7 +15,7 @@ public class Person implements Serializable { protected Integer order; protected URL image; - protected Person() { + public Person() { // used by serializer } @@ -57,15 +57,15 @@ public class Person implements Serializable { } public boolean isActor() { - return character != null || ACTOR.equals(getJob()); + return character != null || ACTOR.equals(job) || GUEST_STAR.equals(job); } public boolean isDirector() { - return DIRECTOR.equals(getJob()); + return DIRECTOR.equals(job); } public boolean isWriter() { - return WRITER.equals(getJob()); + return WRITER.equals(job); } @Override @@ -76,6 +76,7 @@ public class Person implements Serializable { public static final String WRITER = "Writer"; public static final String DIRECTOR = "Director"; public static final String ACTOR = "Actor"; + public static final String GUEST_STAR = "Guest Star"; public static final Comparator CREDIT_ORDER = comparing(Person::getOrder, nullsLast(naturalOrder())); diff --git a/source/net/filebot/web/SearchResult.java b/source/net/filebot/web/SearchResult.java index ea54b3e4..4dc534a9 100644 --- a/source/net/filebot/web/SearchResult.java +++ b/source/net/filebot/web/SearchResult.java @@ -13,7 +13,7 @@ public class SearchResult implements Serializable { protected String name; protected String[] aliasNames; - protected SearchResult() { + public SearchResult() { // used by serializer } diff --git a/source/net/filebot/web/TMDbClient.java b/source/net/filebot/web/TMDbClient.java index b86865d1..40a685dd 100644 --- a/source/net/filebot/web/TMDbClient.java +++ b/source/net/filebot/web/TMDbClient.java @@ -408,7 +408,7 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { adult, backdrop_path, budget, homepage, id, imdb_id, original_title, overview, popularity, poster_path, release_date, revenue, runtime, tagline, title, vote_average, vote_count, certification, collection } - public static class MovieInfo implements Serializable { + public static class MovieInfo implements Crew, Serializable { protected Map fields; @@ -422,7 +422,7 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { protected Person[] people; protected Trailer[] trailers; - protected MovieInfo() { + public MovieInfo() { // used by serializer } @@ -534,26 +534,10 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { return stream(spokenLanguages).map(Locale::new).collect(toList()); } - public List getPeople() { + public List getCrew() { return unmodifiableList(asList(people)); } - public String getDirector() { - return stream(people).filter(Person::isDirector).map(Person::getName).findFirst().orElse(null); - } - - public String getWriter() { - return stream(people).filter(Person::isWriter).map(Person::getName).findFirst().orElse(null); - } - - public List getCast() { - return stream(people).filter(Person::isActor).collect(toList()); - } - - public List getActors() { - return stream(people).filter(Person::isActor).map(Person::getName).collect(toList()); - } - public Map getCertifications() { return unmodifiableMap(certifications); // e.g. ['US': PG-13] } @@ -578,6 +562,7 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { public String toString() { return fields.toString(); } + } public static class Trailer implements Serializable { @@ -586,6 +571,10 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider { protected String name; protected Map sources; + public Trailer() { + // used by serializer + } + public Trailer(String type, String name, Map sources) { this.type = type; this.name = name; diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index aeaf471a..1d7c8555 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -309,7 +309,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider implements Artwor people.add(new Person(it.toString(), Person.WRITER)); } for (Object it : getArray(data, "guestStars")) { - people.add(new Person(it.toString(), Person.ACTOR)); + people.add(new Person(it.toString(), Person.GUEST_STAR)); } return new EpisodeInfo(this, locale, seriesId, id, people, overview, rating, votes); diff --git a/test/net/filebot/web/TMDbClientTest.java b/test/net/filebot/web/TMDbClientTest.java index 8092c8a0..99b2dfea 100644 --- a/test/net/filebot/web/TMDbClientTest.java +++ b/test/net/filebot/web/TMDbClientTest.java @@ -115,7 +115,7 @@ public class TMDbClientTest { @Test public void getPeople() throws Exception { - Person p = db.getMovieInfo("16320", Locale.ENGLISH, true).getPeople().get(0); + Person p = db.getMovieInfo("16320", Locale.ENGLISH, true).getCrew().get(0); assertEquals("Nathan Fillion", p.getName()); assertEquals("Mal", p.getCharacter()); assertEquals(null, p.getJob()); diff --git a/test/net/filebot/web/TheTVDBClientTest.java b/test/net/filebot/web/TheTVDBClientTest.java index 3654c272..ce0023a9 100644 --- a/test/net/filebot/web/TheTVDBClientTest.java +++ b/test/net/filebot/web/TheTVDBClientTest.java @@ -175,7 +175,7 @@ public class TheTVDBClientTest { assertEquals("When Jaye Tyler is convinced by a waxed lion to chase after a shinny quarter, she finds herself returning a lost purse to a lady (who instead of thanking her, is punched in the face), meeting an attractive and sweet bartender names Eric, introducing her sister, Sharon to the EPS newly divorced bachelor, Thomas, she knows, and later discovering her sister, Sharon's sexuality.", i.getOverview().toString()); assertEquals("[Todd Holland, Bryan Fuller, Todd Holland]", i.getDirectors().toString()); assertEquals("[Todd Holland, Bryan Fuller]", i.getWriters().toString()); - assertEquals("[Scotch Ellis Loring, Gerry Fiorini, Kim Roberts, Corry Karpf, Curt Wu, Bailey Stocker, Lisa Marcos, Jorge Molina, Morgan Drmaj, Chantal Purdy, Kari Matchett, Neil Grayston, Anna Starnino, Melissa Grelo, Brandon Oakes, Scotch Ellis Loring, Ted Dykstra, Kathryn Greenwood, G]", i.getGuestStars().toString()); + assertEquals("[Scotch Ellis Loring, Gerry Fiorini, Kim Roberts, Corry Karpf, Curt Wu, Bailey Stocker, Lisa Marcos, Jorge Molina, Morgan Drmaj, Chantal Purdy, Kari Matchett, Neil Grayston, Anna Starnino, Melissa Grelo, Brandon Oakes, Scotch Ellis Loring, Ted Dykstra, Kathryn Greenwood, G]", i.getActors().toString()); } }