Use Parameters class
parent
47f1521bf9
commit
c6e9c3fcf5
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue