Remove generic TypeReference builder

This commit is contained in:
Stuart Boston
2015-03-02 13:35:36 +00:00
parent 9c473bf24b
commit c93600fff5
7 changed files with 46 additions and 41 deletions
@@ -22,14 +22,16 @@ package com.omertron.themoviedbapi.methods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.model2.movie.MovieBasic;
import com.omertron.themoviedbapi.model2.tv.TVBasic;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.omertron.themoviedbapi.wrapper.WrapperGenericList;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamj.api.common.exception.ApiExceptionType;
@@ -49,13 +51,20 @@ public class AbstractMethod {
protected static final ObjectMapper MAPPER = new ObjectMapper();
// Logger
protected static final Logger LOG = LoggerFactory.getLogger(TmdbAccount.class);
protected static final TypeReference<WrapperGenericList<MovieBasic>> TR_MOVIE_BASIC = getTypeReference(MovieBasic.class);
protected static final TypeReference<WrapperGenericList<TVBasic>> TR_TV_BASIC = getTypeReference(TVBasic.class);
protected static final TypeReference<WrapperGenericList<UserList>> TR_USER_LIST = getTypeReference(UserList.class);
private static <T> TypeReference getTypeReference(T Class) {
return new TypeReference<WrapperGenericList<T>>() {
};
private static final Map<Class, TypeReference> TYPE_REFS = new HashMap<Class, TypeReference>();
static {
TYPE_REFS.put(MovieBasic.class, new TypeReference<WrapperGenericList<MovieBasic>>() {
});
TYPE_REFS.put(TVBasic.class, new TypeReference<WrapperGenericList<TVBasic>>() {
});
TYPE_REFS.put(UserList.class, new TypeReference<WrapperGenericList<UserList>>() {
});
}
protected static TypeReference getTypeReference(Class aClass) {
return TYPE_REFS.get(aClass);
}
/**
@@ -22,11 +22,11 @@ package com.omertron.themoviedbapi.methods;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.enumeration.MediaType;
import com.omertron.themoviedbapi.enumeration.SortBy;
import com.omertron.themoviedbapi.model2.account.Account;
import com.omertron.themoviedbapi.model2.StatusCode;
import com.omertron.themoviedbapi.model2.account.Account;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.model2.movie.MovieBasic;
import com.omertron.themoviedbapi.model2.tv.TVBasic;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.tools.ApiUrl;
import com.omertron.themoviedbapi.tools.HttpTools;
import com.omertron.themoviedbapi.tools.MethodBase;
@@ -92,7 +92,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.LISTS).buildUrl(parameters);
return processWrapperList(TR_USER_LIST, url, "user list");
return processWrapperList(getTypeReference(UserList.class), url, "user list");
}
/**
@@ -109,7 +109,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_MOVIES).buildUrl(parameters);
return processWrapperList(TR_MOVIE_BASIC, url, "favorite movies");
return processWrapperList(getTypeReference(MovieBasic.class), url, "favorite movies");
}
/**
@@ -126,7 +126,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.ID, accountId);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.FAVORITE_TV).buildUrl(parameters);
return processWrapperList(TR_TV_BASIC, url, "favorite TV shows");
return processWrapperList(getTypeReference(TVBasic.class), url, "favorite TV shows");
}
/**
@@ -181,7 +181,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_MOVIES).buildUrl(parameters);
return processWrapperList(TR_MOVIE_BASIC, url, "rated movies");
return processWrapperList(getTypeReference(MovieBasic.class), url, "rated movies");
}
/**
@@ -204,7 +204,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.RATED_TV).buildUrl(parameters);
return processWrapperList(TR_TV_BASIC, url, "rated TV shows");
return processWrapperList(getTypeReference(TVBasic.class), url, "rated TV shows");
}
/**
@@ -227,7 +227,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_MOVIES).buildUrl(parameters);
return processWrapperList(TR_MOVIE_BASIC, url, "movie watch list");
return processWrapperList(getTypeReference(MovieBasic.class), url, "movie watch list");
}
/**
@@ -250,7 +250,7 @@ public class TmdbAccount extends AbstractMethod {
parameters.add(Param.LANGUAGE, language);
URL url = new ApiUrl(apiKey, MethodBase.ACCOUNT).setSubMethod(MethodSub.WATCHLIST_TV).buildUrl(parameters);
return processWrapperList(TR_TV_BASIC, url, "TV watch list");
return processWrapperList(getTypeReference(TVBasic.class), url, "TV watch list");
}
/**
@@ -306,6 +306,6 @@ public class TmdbAccount extends AbstractMethod {
}
URL url = new ApiUrl(apiKey, MethodBase.GUEST_SESSION).setSubMethod(MethodSub.RATED_MOVIES_GUEST).buildUrl(parameters);
return processWrapperList(TR_MOVIE_BASIC, url, "Guest Session Movies");
return processWrapperList(getTypeReference(MovieBasic.class), url, "Guest Session Movies");
}
}
@@ -92,7 +92,7 @@ public class TmdbCompanies extends AbstractMethod {
URL url = new ApiUrl(apiKey, MethodBase.COMPANY).setSubMethod(MethodSub.MOVIES).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
return processWrapperList(TR_MOVIE_BASIC, url, webpage);
return processWrapperList(getTypeReference(MovieBasic.class), url, webpage);
}
}
@@ -57,7 +57,7 @@ public class TmdbDiscover extends AbstractMethod {
public List<MovieBasic> getDiscoverMovies(Discover discover) throws MovieDbException {
URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.MOVIE).buildUrl(discover.getParams());
String webpage = httpTools.getRequest(url);
return processWrapperList(TR_MOVIE_BASIC, url, webpage);
return processWrapperList(getTypeReference(MovieBasic.class), url, webpage);
}
/**
@@ -70,6 +70,6 @@ public class TmdbDiscover extends AbstractMethod {
public List<TVBasic> getDiscoverTV(Discover discover) throws MovieDbException {
URL url = new ApiUrl(apiKey, MethodBase.DISCOVER).setSubMethod(MethodSub.TV).buildUrl(discover.getParams());
String webpage = httpTools.getRequest(url);
return processWrapperList(TR_TV_BASIC, url, webpage);
return processWrapperList(getTypeReference(TVBasic.class), url, webpage);
}
}
@@ -122,6 +122,6 @@ public class TmdbGenres extends AbstractMethod {
URL url = new ApiUrl(apiKey, MethodBase.GENRE).setSubMethod(MethodSub.MOVIES).buildUrl(parameters);
String webpage = httpTools.getRequest(url);
return processWrapperList(TR_MOVIE_BASIC, url, webpage);
return processWrapperList(getTypeReference(MovieBasic.class), url, webpage);
}
}
@@ -19,14 +19,13 @@
*/
package com.omertron.themoviedbapi.methods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.enumeration.SearchType;
import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model.keyword.Keyword;
import com.omertron.themoviedbapi.model.person.Person;
import com.omertron.themoviedbapi.model2.collection.Collection;
import com.omertron.themoviedbapi.model2.company.Company;
import com.omertron.themoviedbapi.model.keyword.Keyword;
import com.omertron.themoviedbapi.model.MovieDb;
import com.omertron.themoviedbapi.model.person.Person;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.model2.tv.TVBasic;
import com.omertron.themoviedbapi.results.TmdbResultsList;
@@ -265,8 +264,4 @@ public class TmdbSearch extends AbstractMethod {
return results;
}
private static <T> TypeReference getTypeReference(T Class) {
return new TypeReference<WrapperGenericList<T>>() {
};
}
}
@@ -23,12 +23,12 @@ import com.omertron.themoviedbapi.AbstractTests;
import com.omertron.themoviedbapi.MovieDbException;
import com.omertron.themoviedbapi.enumeration.MediaType;
import com.omertron.themoviedbapi.enumeration.SortBy;
import com.omertron.themoviedbapi.model2.account.Account;
import com.omertron.themoviedbapi.model2.StatusCode;
import com.omertron.themoviedbapi.model2.account.Account;
import com.omertron.themoviedbapi.model2.authentication.TokenSession;
import com.omertron.themoviedbapi.model2.list.UserList;
import com.omertron.themoviedbapi.model2.movie.MovieBasic;
import com.omertron.themoviedbapi.model2.tv.TVBasic;
import com.omertron.themoviedbapi.model2.list.UserList;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
@@ -39,6 +39,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
/**
@@ -78,7 +79,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetAccount() throws MovieDbException {
LOG.info("getAccount");
Account result = instance.getAccount(getSessionId());
@@ -92,7 +93,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetUserLists() throws MovieDbException {
LOG.info("getUserLists");
List<UserList> results = instance.getUserLists(getSessionId(), getAccountId());
@@ -108,7 +109,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetFavoriteMovies() throws MovieDbException {
LOG.info("getFavoriteMovies");
List<MovieBasic> results = instance.getFavoriteMovies(getSessionId(), getAccountId());
@@ -124,7 +125,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetFavoriteTv() throws MovieDbException {
LOG.info("getFavoriteTv");
List<TVBasic> results = instance.getFavoriteTv(getSessionId(), getAccountId());
@@ -140,7 +141,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testModifyFavoriteStatus() throws MovieDbException {
LOG.info("modifyFavoriteStatus");
@@ -170,7 +171,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetRatedMovies() throws MovieDbException {
LOG.info("getRatedMovies");
List<MovieBasic> results = instance.getRatedMovies(getSessionId(), getAccountId(), null, null, null);
@@ -183,7 +184,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetRatedTV() throws MovieDbException {
LOG.info("getRatedTV");
List<TVBasic> results = instance.getRatedTV(getSessionId(), getAccountId(), null, null, null);
@@ -199,7 +200,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetWatchListMovie() throws MovieDbException {
LOG.info("getWatchListMovie");
List<MovieBasic> results = instance.getWatchListMovie(getSessionId(), getAccountId(), null, null, null);
@@ -231,7 +232,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testModifyWatchList() throws MovieDbException {
LOG.info("modifyWatchList");
@@ -261,7 +262,7 @@ public class TmdbAccountTest extends AbstractTests {
*
* @throws com.omertron.themoviedbapi.MovieDbException
*/
@Test
@Ignore
public void testGetGuestRatedMovies() throws MovieDbException {
LOG.info("getGuestRatedMovies");