Use Parameters class
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -19,13 +19,8 @@
|
||||
*/
|
||||
package com.omertron.themoviedbapi.model;
|
||||
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_ADULT;
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_LANGUAGE;
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_PAGE;
|
||||
import static com.omertron.themoviedbapi.tools.ApiUrl.PARAM_YEAR;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.omertron.themoviedbapi.tools.Param;
|
||||
import com.omertron.themoviedbapi.tools.TmdbParameters;
|
||||
|
||||
/**
|
||||
* Generate a discover object for use in the MovieDbApi
|
||||
@@ -36,17 +31,8 @@ import org.apache.commons.lang3.StringUtils;
|
||||
*/
|
||||
public class Discover {
|
||||
|
||||
private final Map<String, String> params = new HashMap<String, String>();
|
||||
private static final String PARAM_PRIMARY_RELEASE_YEAR = "primary_release_year=";
|
||||
private static final String PARAM_VOTE_COUNT_GTE = "vote_count.gte=";
|
||||
private static final String PARAM_VOTE_AVERAGE_GTE = "vote_average.gte=";
|
||||
private static final String PARAM_WITH_GENRES = "with_genres=";
|
||||
private static final String PARAM_RELEASE_DATE_GTE = "release_date.gte=";
|
||||
private static final String PARAM_RELEASE_DATE_LTE = "release_date.lte=";
|
||||
private static final String PARAM_CERTIFICATION_COUNTRY = "certification_country=";
|
||||
private static final String PARAM_CERTIFICATION_LTE = "certification.lte=";
|
||||
private static final String PARAM_WITH_COMPANIES = "with_companies=";
|
||||
private static final String PARAM_SORT_BY = "sort_by=";
|
||||
private final TmdbParameters params = new TmdbParameters();
|
||||
|
||||
private static final int YEAR_MIN = 1900;
|
||||
private static final int YEAR_MAX = 2100;
|
||||
|
||||
@@ -57,7 +43,7 @@ public class Discover {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String, String> getParams() {
|
||||
public TmdbParameters getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
@@ -68,9 +54,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover page(int page) {
|
||||
if (page > 0) {
|
||||
params.put(PARAM_PAGE, String.valueOf(page));
|
||||
}
|
||||
params.add(Param.PAGE, page);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -81,9 +65,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover language(String language) {
|
||||
if (StringUtils.isNotBlank(language)) {
|
||||
params.put(PARAM_LANGUAGE, language);
|
||||
}
|
||||
params.add(Param.LANGUAGE, language);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -100,9 +82,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover sortBy(String sortBy) {
|
||||
if (StringUtils.isNotBlank(sortBy)) {
|
||||
params.put(PARAM_SORT_BY, sortBy);
|
||||
}
|
||||
params.add(Param.SORT_BY, sortBy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -113,7 +93,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover includeAdult(boolean includeAdult) {
|
||||
params.put(PARAM_ADULT, String.valueOf(includeAdult));
|
||||
params.add(Param.ADULT, includeAdult);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -125,47 +105,46 @@ public class Discover {
|
||||
*/
|
||||
public Discover year(int year) {
|
||||
if (checkYear(year)) {
|
||||
params.put(PARAM_YEAR, String.valueOf(year));
|
||||
params.add(Param.YEAR, year);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the results so that only the primary release date year has this value
|
||||
* Filter the results so that only the primary release date year has this
|
||||
* value
|
||||
*
|
||||
* @param primaryReleaseYear
|
||||
* @return
|
||||
*/
|
||||
public Discover primaryReleaseYear(int primaryReleaseYear) {
|
||||
if (checkYear(primaryReleaseYear)) {
|
||||
params.put(PARAM_PRIMARY_RELEASE_YEAR, String.valueOf(primaryReleaseYear));
|
||||
params.add(Param.PRIMARY_RELEASE_YEAR, primaryReleaseYear);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only include movies that are equal to, or have a vote count higher than this value
|
||||
* Only include movies that are equal to, or have a vote count higher than
|
||||
* this value
|
||||
*
|
||||
* @param voteCountGte
|
||||
* @return
|
||||
*/
|
||||
public Discover voteCountGte(int voteCountGte) {
|
||||
if (voteCountGte > 0) {
|
||||
params.put(PARAM_VOTE_COUNT_GTE, String.valueOf(voteCountGte));
|
||||
}
|
||||
params.add(Param.VOTE_COUNT_GTE, voteCountGte);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only include movies that are equal to, or have a higher average rating than this value
|
||||
* Only include movies that are equal to, or have a higher average rating
|
||||
* than this value
|
||||
*
|
||||
* @param voteAverageGte
|
||||
* @return
|
||||
*/
|
||||
public Discover voteAverageGte(float voteAverageGte) {
|
||||
if (voteAverageGte > 0) {
|
||||
params.put(PARAM_VOTE_AVERAGE_GTE, String.valueOf(voteAverageGte));
|
||||
}
|
||||
params.add(Param.VOTE_AVERAGE_GTE, voteAverageGte);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -176,15 +155,14 @@ public class Discover {
|
||||
* <p/>
|
||||
* Multiple values can be specified.
|
||||
* <p/>
|
||||
* Comma separated indicates an 'AND' query, while a pipe (|) separated value indicates an 'OR'
|
||||
* Comma separated indicates an 'AND' query, while a pipe (|) separated
|
||||
* value indicates an 'OR'
|
||||
*
|
||||
* @param withGenres
|
||||
* @return
|
||||
*/
|
||||
public Discover withGenres(String withGenres) {
|
||||
if (StringUtils.isNotBlank(withGenres)) {
|
||||
params.put(PARAM_WITH_GENRES, withGenres);
|
||||
}
|
||||
params.add(Param.WITH_GENRES, withGenres);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -197,9 +175,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover releaseDateGte(String releaseDateGte) {
|
||||
if (StringUtils.isNotBlank(releaseDateGte)) {
|
||||
params.put(PARAM_RELEASE_DATE_GTE, releaseDateGte);
|
||||
}
|
||||
params.add(Param.RELEASE_DATE_GTE, releaseDateGte);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -212,9 +188,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover releaseDateLte(String releaseDateLte) {
|
||||
if (StringUtils.isNotBlank(releaseDateLte)) {
|
||||
params.put(PARAM_RELEASE_DATE_LTE, releaseDateLte);
|
||||
}
|
||||
params.add(Param.RELEASE_DATE_LTE, releaseDateLte);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -229,24 +203,21 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover certificationCountry(String certificationCountry) {
|
||||
if (StringUtils.isNotBlank(certificationCountry)) {
|
||||
params.put(PARAM_CERTIFICATION_COUNTRY, certificationCountry);
|
||||
}
|
||||
params.add(Param.CERTIFICATION_COUNTRY, certificationCountry);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only include movies with this certification and lower.
|
||||
* <p/>
|
||||
* Expected value is a valid certification for the specified 'certificationCountry'.
|
||||
* Expected value is a valid certification for the specified
|
||||
* 'certificationCountry'.
|
||||
*
|
||||
* @param certificationLte
|
||||
* @return
|
||||
*/
|
||||
public Discover certificationLte(String certificationLte) {
|
||||
if (StringUtils.isNotBlank(certificationLte)) {
|
||||
params.put(PARAM_CERTIFICATION_LTE, certificationLte);
|
||||
}
|
||||
params.add(Param.CERTIFICATION_LTE, certificationLte);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -261,9 +232,7 @@ public class Discover {
|
||||
* @return
|
||||
*/
|
||||
public Discover withCompanies(String withCompanies) {
|
||||
if (StringUtils.isNotBlank(withCompanies)) {
|
||||
params.put(PARAM_WITH_COMPANIES, withCompanies);
|
||||
}
|
||||
params.add(Param.WITH_COMPANIES, withCompanies);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,51 +36,19 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
public class ApiUrl {
|
||||
|
||||
/*
|
||||
* Logger
|
||||
*/
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ApiUrl.class);
|
||||
/*
|
||||
* TheMovieDbApi API Base URL
|
||||
*/
|
||||
// TheMovieDbApi API Base URL
|
||||
private static final String TMDB_API_BASE = "http://api.themoviedb.org/3/";
|
||||
/*
|
||||
* Parameter configuration
|
||||
*/
|
||||
// Parameter configuration
|
||||
private static final String DELIMITER_FIRST = "?";
|
||||
private static final String DELIMITER_SUBSEQUENT = "&";
|
||||
private static final String DEFAULT_STRING = "";
|
||||
/*
|
||||
* Properties
|
||||
*/
|
||||
// Properties
|
||||
private final String apiKey;
|
||||
private final String method;
|
||||
private final String submethod;
|
||||
private String submethod;
|
||||
private final Map<String, String> arguments = new HashMap<String, String>();
|
||||
/*
|
||||
* API Parameters
|
||||
*/
|
||||
public static final String PARAM_ADULT = "include_adult=";
|
||||
public static final String PARAM_API_KEY = "api_key=";
|
||||
public static final String PARAM_COUNTRY = "country=";
|
||||
public static final String PARAM_FAVORITE = "favorite=";
|
||||
public static final String PARAM_ID = "id=";
|
||||
public static final String PARAM_LANGUAGE = "language=";
|
||||
public static final String PARAM_INCLUDE_ALL_MOVIES = "include_all_movies=";
|
||||
public static final String PARAM_MOVIE_WATCHLIST = "movie_watchlist=";
|
||||
public static final String PARAM_PAGE = "page=";
|
||||
public static final String PARAM_QUERY = "query=";
|
||||
public static final String PARAM_SESSION = "session_id=";
|
||||
public static final String PARAM_TOKEN = "request_token=";
|
||||
public static final String PARAM_VALUE = "value=";
|
||||
public static final String PARAM_YEAR = "year=";
|
||||
public static final String PARAM_START_DATE = "start_date=";
|
||||
public static final String PARAM_END_DATE = "end_date=";
|
||||
public static final String PARAM_USERNAME = "username=";
|
||||
public static final String PARAM_PASSWORD = "password=";
|
||||
private static final String APPEND_TO_RESPONSE = "append_to_response=";
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Constructor Methods">
|
||||
/**
|
||||
* Constructor for the simple API URL method without a sub-method
|
||||
*
|
||||
@@ -90,36 +58,66 @@ public class ApiUrl {
|
||||
public ApiUrl(String apiKey, String method) {
|
||||
this.apiKey = apiKey;
|
||||
this.method = method;
|
||||
this.submethod = DEFAULT_STRING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for the API URL with a sub-method
|
||||
* Add a sub-methods
|
||||
*
|
||||
* @param apiKey
|
||||
* @param method
|
||||
* @param submethod
|
||||
* @return
|
||||
*/
|
||||
public ApiUrl(String apiKey, String method, String submethod) {
|
||||
this.apiKey = apiKey;
|
||||
this.method = method;
|
||||
public ApiUrl setSubMethod(String submethod) {
|
||||
this.submethod = submethod;
|
||||
return this;
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
/**
|
||||
* Build the URL from the pre-created arguments.
|
||||
* Add a sub-method that has an ID at the start
|
||||
*
|
||||
* @param someId
|
||||
* @param submethod
|
||||
* @return
|
||||
*/
|
||||
public ApiUrl setSubMethod(int someId, String submethod) {
|
||||
this.submethod = someId + submethod;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a sub-method that has an ID at the start
|
||||
*
|
||||
* @param someId
|
||||
* @param submethod
|
||||
* @return
|
||||
*/
|
||||
public ApiUrl setSubMethod(String someId, String submethod) {
|
||||
this.submethod = someId + submethod;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the URL with the default parameters
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public URL buildUrl() {
|
||||
return buildUrl(new TmdbParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the URL from the pre-created parameters.
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public URL buildUrl(TmdbParameters params) {
|
||||
StringBuilder urlString = new StringBuilder(TMDB_API_BASE);
|
||||
|
||||
// Get the start of the URL
|
||||
urlString.append(method);
|
||||
|
||||
// We have either a queury, or a direct request
|
||||
if (arguments.containsKey(PARAM_QUERY)) {
|
||||
if (params.has(Param.QUERY)) {
|
||||
// Append the suffix of the API URL
|
||||
if (StringUtils.endsWith(urlString, "/") && submethod.startsWith("/")) {
|
||||
urlString.deleteCharAt(urlString.length() - 1);
|
||||
@@ -127,14 +125,14 @@ public class ApiUrl {
|
||||
urlString.append(submethod);
|
||||
|
||||
// Append the key information
|
||||
urlString.append(DELIMITER_FIRST).append(PARAM_API_KEY);
|
||||
urlString.append(DELIMITER_FIRST).append(Param.API_KEY.getValue());
|
||||
urlString.append(apiKey);
|
||||
|
||||
// Append the search term
|
||||
urlString.append(DELIMITER_SUBSEQUENT);
|
||||
urlString.append(PARAM_QUERY);
|
||||
urlString.append(Param.QUERY.getValue());
|
||||
|
||||
String query = arguments.get(PARAM_QUERY);
|
||||
String query = (String) params.get(Param.QUERY);
|
||||
|
||||
try {
|
||||
urlString.append(URLEncoder.encode(query, "UTF-8"));
|
||||
@@ -145,12 +143,12 @@ public class ApiUrl {
|
||||
}
|
||||
|
||||
// Remove the query from the arguments so it is not added later
|
||||
arguments.remove(PARAM_QUERY);
|
||||
params.remove(Param.QUERY);
|
||||
} else {
|
||||
// Append the ID if provided
|
||||
if (arguments.containsKey(PARAM_ID)) {
|
||||
urlString.append(arguments.get(PARAM_ID));
|
||||
arguments.remove(PARAM_ID);
|
||||
if (params.has(Param.ID)) {
|
||||
urlString.append(params.get(Param.ID));
|
||||
params.remove(Param.ID);
|
||||
}
|
||||
|
||||
// Append the suffix of the API URL
|
||||
@@ -160,7 +158,7 @@ public class ApiUrl {
|
||||
urlString.append(submethod);
|
||||
|
||||
// Append the key information
|
||||
urlString.append(DELIMITER_FIRST).append(PARAM_API_KEY);
|
||||
urlString.append(DELIMITER_FIRST).append(Param.API_KEY);
|
||||
urlString.append(apiKey);
|
||||
}
|
||||
|
||||
@@ -179,81 +177,4 @@ public class ApiUrl {
|
||||
arguments.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add arguments individually
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void addArgument(String key, String value) {
|
||||
arguments.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add arguments individually
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void addArgument(String key, int value) {
|
||||
arguments.put(key, Integer.toString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add arguments individually
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void addArgument(String key, boolean value) {
|
||||
arguments.put(key, Boolean.toString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add arguments individually
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void addArgument(String key, float value) {
|
||||
arguments.put(key, Float.toString(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the arguments
|
||||
*/
|
||||
public void clearArguments() {
|
||||
arguments.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the arguments directly
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public void setArguments(Map<String, String> args) {
|
||||
arguments.putAll(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append any optional parameters to the URL
|
||||
*
|
||||
* @param appendToResponse
|
||||
*/
|
||||
public void appendToResponse(String[] appendToResponse) {
|
||||
if (appendToResponse.length > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean first = Boolean.TRUE;
|
||||
for (String append : appendToResponse) {
|
||||
if (first) {
|
||||
first = Boolean.FALSE;
|
||||
} else {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(append);
|
||||
}
|
||||
addArgument(APPEND_TO_RESPONSE, sb.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (c) 2004-2015 Stuart Boston
|
||||
*
|
||||
* This file is part of TheMovieDB API.
|
||||
*
|
||||
* TheMovieDB API is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* TheMovieDB API is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with TheMovieDB API. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.omertron.themoviedbapi.tools;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Parameters for use in the URL
|
||||
*
|
||||
* @author Stuart
|
||||
*/
|
||||
public enum Param {
|
||||
|
||||
ADULT("include_adult="),
|
||||
API_KEY("api_key="),
|
||||
APPEND("append_to_response="),
|
||||
COUNTRY("country="),
|
||||
END_DATE("end_date="),
|
||||
FAVORITE("favorite="),
|
||||
ID("id="),
|
||||
INCLUDE_ALL_MOVIES("include_all_movies="),
|
||||
LANGUAGE("language="),
|
||||
MOVIE_WATCHLIST("movie_watchlist="),
|
||||
PAGE("page="),
|
||||
PASSWORD("password="),
|
||||
QUERY("query="),
|
||||
SESSION("session_id="),
|
||||
START_DATE("start_date="),
|
||||
TOKEN("request_token="),
|
||||
USERNAME("username="),
|
||||
VALUE("value="),
|
||||
YEAR("year="),
|
||||
/*
|
||||
* Discover parameters
|
||||
*/
|
||||
CERTIFICATION_COUNTRY("certification_country="),
|
||||
CERTIFICATION_LTE("certification.lte="),
|
||||
PRIMARY_RELEASE_YEAR("primary_release_year="),
|
||||
RELEASE_DATE_GTE("release_date.gte="),
|
||||
RELEASE_DATE_LTE("release_date.lte="),
|
||||
SORT_BY("sort_by="),
|
||||
VOTE_AVERAGE_GTE("vote_average.gte="),
|
||||
VOTE_COUNT_GTE("vote_count.gte="),
|
||||
WITH_COMPANIES("with_companies="),
|
||||
WITH_GENRES("with_genres=");
|
||||
|
||||
private final String value;
|
||||
|
||||
private Param(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL parameter to use
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string into an Enum type
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static Param fromString(String value) {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
for (final Param param : EnumSet.allOf(Param.class)) {
|
||||
if (value.equalsIgnoreCase(param.value)) {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We've not found the type!
|
||||
throw new IllegalArgumentException("Value '" + value + "' not recognised");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* Copyright (c) 2004-2015 Stuart Boston
|
||||
*
|
||||
* This file is part of TheMovieDB API.
|
||||
*
|
||||
* TheMovieDB API is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* TheMovieDB API is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with TheMovieDB API. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.omertron.themoviedbapi.tools;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
/**
|
||||
* Parameters for the TMDB API
|
||||
*
|
||||
* @author Stuart
|
||||
*/
|
||||
public class TmdbParameters {
|
||||
|
||||
private final Map<Param, String> parameters = new EnumMap<Param, String>(Param.class);
|
||||
|
||||
/**
|
||||
* Construct an empty set of parameters
|
||||
*/
|
||||
public TmdbParameters() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a parameter to the collection
|
||||
*
|
||||
* @param key Parameter to add
|
||||
* @param value The array value to use (will be converted into a comma
|
||||
* separated list)
|
||||
*/
|
||||
public void add(Param key, String[] value) {
|
||||
parameters.put(key, toList(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a string parameter to the collection
|
||||
*
|
||||
* @param key Parameter to add
|
||||
* @param value The value to add (will be checked to ensure it's valid)
|
||||
*/
|
||||
public void add(Param key, String value) {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
parameters.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an integer parameter to the collection
|
||||
*
|
||||
* @param key Parameter to add
|
||||
* @param value The value to add (will be checked to ensure >0)
|
||||
*/
|
||||
public void add(Param key, int value) {
|
||||
if (value > 0) {
|
||||
parameters.put(key, String.valueOf(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a float parameter to the collection
|
||||
*
|
||||
* @param key Parameter to add
|
||||
* @param value The value to add (will be checked to ensure >0)
|
||||
*/
|
||||
public void add(Param key, float value) {
|
||||
if (value > 0) {
|
||||
parameters.put(key, String.valueOf(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a boolean parameter to the collection
|
||||
*
|
||||
* @param key Parameter to add
|
||||
* @param value The value to add (will be checked to ensure >0)
|
||||
*/
|
||||
public void add(Param key, boolean value) {
|
||||
parameters.put(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the collection has a certain parameter
|
||||
*
|
||||
* @param key The Parameter to check
|
||||
* @return
|
||||
*/
|
||||
public boolean has(Param key) {
|
||||
return parameters.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parameter from the collection
|
||||
*
|
||||
* @param key The parameter to get
|
||||
* @return
|
||||
*/
|
||||
public Object get(Param key) {
|
||||
return parameters.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a parameter from the collection
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public void remove(Param key) {
|
||||
parameters.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the collection has no items
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return parameters.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the collection has items
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isNotEmpty() {
|
||||
return !isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Append any optional parameters to the URL
|
||||
*
|
||||
* @param appendToResponse
|
||||
* @return
|
||||
*/
|
||||
public String toList(String[] appendToResponse) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (appendToResponse.length > 0) {
|
||||
boolean first = Boolean.TRUE;
|
||||
for (String append : appendToResponse) {
|
||||
if (first) {
|
||||
first = Boolean.FALSE;
|
||||
} else {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(append);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(parameters, ToStringStyle.SHORT_PREFIX_STYLE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user