Split out compare movies to utility class

This commit is contained in:
Stuart Boston
2015-03-04 10:15:10 +00:00
parent dd3e04e6d1
commit 8cb14aeb27
@@ -87,7 +87,6 @@ import com.omertron.themoviedbapi.wrapper.WrapperChanges;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpClient;
import org.yamj.api.common.http.SimpleHttpClientBuilder;
@@ -101,8 +100,6 @@ import org.yamj.api.common.http.SimpleHttpClientBuilder;
public class TheMovieDbApi {
private HttpTools httpTools;
// Constants
private static final int YEAR_LENGTH = 4;
// Sub-methods
private static TmdbAccount tmdbAccount;
private static TmdbAuthentication tmdbAuth;
@@ -174,103 +171,6 @@ public class TheMovieDbApi {
tmdbTv = new TmdbTV(apiKey, httpTools);
}
/**
* Compare the MovieDB object with a title & year
*
* @param moviedb The moviedb object to compare too
* @param title The title of the movie to compare
* @param year The year of the movie to compare exact match
* @return True if there is a match, False otherwise.
*/
public static boolean compareMovies(MovieDb moviedb, String title, String year) {
return compareMovies(moviedb, title, year, 0);
}
/**
* Compare the MovieDB object with a title & year
*
* @param moviedb The moviedb object to compare too
* @param title The title of the movie to compare
* @param year The year of the movie to compare
* @param maxDistance The Levenshtein Distance between the two titles. 0 = exact match
* @param caseSensitive true if the comparison is to be case sensitive
* @return True if there is a match, False otherwise.
*/
public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance, boolean caseSensitive) {
if ((moviedb == null) || (StringUtils.isBlank(title))) {
return Boolean.FALSE;
}
String cmpTitle, cmpOtherTitle, cmpOriginalTitle;
if (caseSensitive) {
cmpTitle = title;
cmpOtherTitle = moviedb.getOriginalTitle();
cmpOriginalTitle = moviedb.getTitle();
} else {
cmpTitle = title.toLowerCase();
cmpOtherTitle = moviedb.getTitle().toLowerCase();
cmpOriginalTitle = moviedb.getOriginalTitle().toLowerCase();
}
if (isValidYear(year) && isValidYear(moviedb.getReleaseDate())) {
// Compare with year
String movieYear = moviedb.getReleaseDate().substring(0, YEAR_LENGTH);
if (movieYear.equals(year)) {
if (compareDistance(cmpOriginalTitle, cmpTitle, maxDistance)) {
return Boolean.TRUE;
}
if (compareDistance(cmpOtherTitle, cmpTitle, maxDistance)) {
return Boolean.TRUE;
}
}
}
// Compare without year
if (compareDistance(cmpOriginalTitle, cmpTitle, maxDistance)) {
return Boolean.TRUE;
}
if (compareDistance(cmpOtherTitle, cmpTitle, maxDistance)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
/**
* Compare the MovieDB object with a title & year, case sensitive
*
* @param moviedb
* @param title
* @param year
* @param maxDistance
* @return
*/
public static boolean compareMovies(MovieDb moviedb, String title, String year, int maxDistance) {
return compareMovies(moviedb, title, year, maxDistance, true);
}
/**
* Compare the Levenshtein Distance between the two strings
*
* @param title1
* @param title2
* @param distance
*/
private static boolean compareDistance(String title1, String title2, int distance) {
return StringUtils.getLevenshteinDistance(title1, title2) <= distance;
}
/**
* Check the year is not blank or UNKNOWN
*
* @param year
*/
private static boolean isValidYear(String year) {
return StringUtils.isNotBlank(year) && !"UNKNOWN".equals(year);
}
//<editor-fold defaultstate="collapsed" desc="Account">
/**
* Get the basic information for an account. You will need to have a valid session id.