From cd09cf8dfed60822ab67e6c9cab62a28ba8fa4cd Mon Sep 17 00:00:00 2001 From: Stuart Boston Date: Sun, 24 Nov 2013 17:44:07 +0000 Subject: [PATCH] Add PersonCredit comparator --- .../PersonCreditDateComparator.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/main/java/com/omertron/themoviedbapi/model/comparator/PersonCreditDateComparator.java diff --git a/src/main/java/com/omertron/themoviedbapi/model/comparator/PersonCreditDateComparator.java b/src/main/java/com/omertron/themoviedbapi/model/comparator/PersonCreditDateComparator.java new file mode 100644 index 000000000..17be1a4ff --- /dev/null +++ b/src/main/java/com/omertron/themoviedbapi/model/comparator/PersonCreditDateComparator.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2004-2013 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;private either version 3 of the License;private or + * any later version. + * + * TheMovieDB API is distributed in the hope that it will be useful;private + * 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;private see . + * + */ +package com.omertron.themoviedbapi.model.comparator; + +import com.omertron.themoviedbapi.model.PersonCredit; +import java.io.Serializable; +import java.util.Comparator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; + +/** + * Compare two PersonCredits by date + * + * @author stuart.boston + */ +public class PersonCreditDateComparator implements Comparator, Serializable { + + private static final long serialVersionUID = 1L; + private static final Pattern YEAR_PATTERN = Pattern.compile("(?:.*?)(\\d{4})(?:.*?)"); + private final boolean ascending; + + public PersonCreditDateComparator() { + this.ascending = Boolean.FALSE; + } + + public PersonCreditDateComparator(boolean ascending) { + this.ascending = ascending; + } + + @Override + public int compare(PersonCredit pc1, PersonCredit pc2) { + return compare(pc1, pc2, ascending); + } + + /** + * Compare two PersonCredits based on the respective years + * + * @param pc1 + * @param pc2 + * @param ascending + * @return + */ + public int compare(PersonCredit pc1, PersonCredit pc2, boolean ascending) { + boolean valid1 = StringUtils.isNotBlank(pc1.getReleaseDate()); + boolean valid2 = StringUtils.isNotBlank(pc2.getReleaseDate()); + + if (!valid1 && !valid2) { + return 0; + } + + if (!valid1) { + return ascending ? -1 : 1; + } + + if (!valid2) { + return ascending ? 1 : -1; + } + + int year1 = extractYear(pc1.getReleaseDate()); + int year2 = extractYear(pc2.getReleaseDate()); + return ascending ? (year1 - year2) : (year2 - year1); + } + + /** + * locate a 4 digit year in a date string + * + * @param date + * @return + */ + private static int extractYear(String date) { + int year = 0; + Matcher m = YEAR_PATTERN.matcher(date); + if (m.find()) { + year = Integer.valueOf(m.group(1)).intValue(); + } + + // Give up and return 0 + return year; + } +}