Simplify SimpleDate

This commit is contained in:
Reinhard Pointner 2016-02-10 11:47:13 +00:00
parent 058940e847
commit 3b79ef9e39
10 changed files with 51 additions and 43 deletions

View File

@ -125,7 +125,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
seriesInfo.setName(selectString("anime/titles/title[@type='main']", dom));
seriesInfo.setRating(getDecimal(selectString("anime/ratings/permanent", dom)));
seriesInfo.setRatingCount(matchInteger(getTextContent("anime/ratings/permanent/@count", dom)));
seriesInfo.setStartDate(SimpleDate.parse(selectString("anime/startdate", dom), "yyyy-MM-dd"));
seriesInfo.setStartDate(SimpleDate.parse(selectString("anime/startdate", dom)));
// add categories ordered by weight as genres
// * only use categories with weight >= 400
@ -155,7 +155,7 @@ public class AnidbClient extends AbstractEpisodeListProvider {
int type = Integer.parseInt(getAttribute("type", epno));
if (type == 1 || type == 2) {
SimpleDate airdate = SimpleDate.parse(getTextContent("airdate", node), "yyyy-MM-dd");
SimpleDate airdate = SimpleDate.parse(getTextContent("airdate", node));
String title = selectString(".//title[@lang='" + locale.getLanguage() + "']", node);
if (title.isEmpty()) { // English language fall-back
title = selectString(".//title[@lang='en']", node);

View File

@ -154,7 +154,7 @@ public class EpisodeFormat extends Format {
Matcher m;
if ((m = airdatePattern.matcher(source)).find()) {
airdate = SimpleDate.parse(m.group(1), "yyyy-MM-dd");
airdate = SimpleDate.parse(m.group(1));
source.replace(m.start(), m.end(), ""); // remove matched part from text
}

View File

@ -1,14 +1,14 @@
package net.filebot.web;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimpleDate implements Serializable, Comparable<Object> {
@ -26,11 +26,12 @@ public class SimpleDate implements Serializable, Comparable<Object> {
this.day = day;
}
public SimpleDate(LocalDate date) {
this(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
}
public SimpleDate(long t) {
LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault());
year = date.getYear();
month = date.getMonthValue();
day = date.getDayOfMonth();
this(LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault()).toLocalDate());
}
public int getYear() {
@ -94,32 +95,16 @@ public class SimpleDate implements Serializable, Comparable<Object> {
return String.format("%04d-%02d-%02d", year, month, day);
}
public String format(String pattern) {
return format(pattern, Locale.ROOT);
}
public String format(String pattern, Locale locale) {
return new SimpleDateFormat(pattern, locale).format(new GregorianCalendar(year, month - 1, day).getTime()); // Calendar months start at 0
}
public static SimpleDate parse(String string) {
return parse(string, "yyyy-MM-dd");
}
public static SimpleDate parse(String string, String pattern) {
if (string == null || string.isEmpty()) {
return null;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.ROOT);
formatter.setLenient(false); // enable strict mode (e.g. fail on invalid dates like 0000-00-00)
return new SimpleDate(formatter.parse(string).getTime());
} catch (ParseException e) {
// date is invalid
return null;
public static SimpleDate parse(String date) {
if (date != null && date.length() > 0) {
Matcher m = DATE_FORMAT.matcher(date);
if (m.matches()) {
return new SimpleDate(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)));
}
}
return null;
}
public static final Pattern DATE_FORMAT = Pattern.compile("(\\d{4}).(\\d{1,2}).(\\d{1,2})");
}

View File

@ -558,7 +558,7 @@ public class TMDbClient implements MovieIdentificationService {
public SimpleDate getReleased() {
// e.g. 2005-09-30
try {
return SimpleDate.parse(get(MovieProperty.release_date), "yyyy-MM-dd");
return SimpleDate.parse(get(MovieProperty.release_date));
} catch (Exception e) {
return null;
}

View File

@ -85,7 +85,7 @@ public class TVMazeClient extends AbstractEpisodeListProvider {
JsonObject<?, ?> response = request("shows/" + show.getId());
String status = getValue(response, "status", String::new);
SimpleDate premiered = getValue(response, "premiered", s -> SimpleDate.parse(s, "yyyy-MM-dd"));
SimpleDate premiered = getValue(response, "premiered", SimpleDate::parse);
Integer runtime = getValue(response, "runtime", Integer::new);
JsonObject<?, ?> genres = (JsonObject<?, ?>) response.get("genres");
JsonObject<?, ?> rating = (JsonObject<?, ?>) response.get("rating");
@ -125,7 +125,7 @@ public class TVMazeClient extends AbstractEpisodeListProvider {
String episodeTitle = getValue(episode, "name", String::new);
Integer seasonNumber = getValue(episode, "season", Integer::new);
Integer episodeNumber = getValue(episode, "number", Integer::new);
SimpleDate airdate = getValue(episode, "airdate", s -> SimpleDate.parse(s, "yyyy-MM-dd"));
SimpleDate airdate = getValue(episode, "airdate", SimpleDate::parse);
episodes.add(new Episode(seriesInfo.getName(), seasonNumber, episodeNumber, episodeTitle, null, null, airdate, new SeriesInfo(seriesInfo)));
}

View File

@ -155,7 +155,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
seriesInfo.setRuntime(matchInteger(getTextContent("Runtime", seriesNode)));
seriesInfo.setActors(getListContent("Actors", "\\|", seriesNode));
seriesInfo.setGenres(getListContent("Genre", "\\|", seriesNode));
seriesInfo.setStartDate(SimpleDate.parse(getTextContent("FirstAired", seriesNode), "yyyy-MM-dd"));
seriesInfo.setStartDate(SimpleDate.parse(getTextContent("FirstAired", seriesNode)));
seriesInfo.setBannerUrl(getResourceURL(MirrorType.BANNER, "/banners/" + getTextContent("banner", seriesNode)));
seriesInfo.setFanartUrl(getResourceURL(MirrorType.BANNER, "/banners/" + getTextContent("fanart", seriesNode)));
@ -170,7 +170,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
for (Node node : nodes) {
String episodeName = getTextContent("EpisodeName", node);
Integer absoluteNumber = matchInteger(getTextContent("absolute_number", node));
SimpleDate airdate = SimpleDate.parse(getTextContent("FirstAired", node), "yyyy-MM-dd");
SimpleDate airdate = SimpleDate.parse(getTextContent("FirstAired", node));
// default numbering
Integer episodeNumber = matchInteger(getTextContent("EpisodeNumber", node));

View File

@ -2,6 +2,7 @@ package net.filebot;
import net.filebot.format.ExpressionFormatTest;
import net.filebot.hash.VerificationFormatTest;
import net.filebot.media.MediaDetectionTest;
import net.filebot.media.ReleaseInfoTest;
import net.filebot.similarity.EpisodeMetricsTest;
import net.filebot.similarity.SimilarityTestSuite;
@ -15,7 +16,7 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ SimilarityTestSuite.class, WebTestSuite.class, ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, EpisodeMetricsTest.class, SubtitleReaderTestSuite.class, ReleaseInfoTest.class, UtilTestSuite.class })
@SuiteClasses({ SimilarityTestSuite.class, WebTestSuite.class, ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, EpisodeMetricsTest.class, SubtitleReaderTestSuite.class, ReleaseInfoTest.class, MediaDetectionTest.class, UtilTestSuite.class })
public class AllTests {
}

View File

@ -5,7 +5,7 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ SeriesNameMatcherTest.class, SeasonEpisodeMatcherTest.class, NameSimilarityMetricTest.class, NumericSimilarityMetricTest.class, SeasonEpisodeMetricTest.class, SimilarityComparatorTest.class })
@SuiteClasses({ SeriesNameMatcherTest.class, SeasonEpisodeMatcherTest.class, DateMatcherTest.class, NameSimilarityMetricTest.class, NumericSimilarityMetricTest.class, SeasonEpisodeMetricTest.class, SimilarityComparatorTest.class })
public class SimilarityTestSuite {
}

View File

@ -0,0 +1,22 @@
package net.filebot.web;
import static org.junit.Assert.*;
import org.junit.Test;
public class SimpleDateTest {
@Test
public void parse() {
assertEquals("2015-01-01", SimpleDate.parse("2015-1-1").toString());
assertEquals("2015-02-02", SimpleDate.parse("2015-02-02").toString());
}
@Test
public void parseIllegalDate() {
// simple date allows illegal values
assertEquals("2015-12-34", SimpleDate.parse("2015-12-34").toString());
}
}

View File

@ -5,7 +5,7 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AnidbClientTest.class, TheTVDBClientTest.class, TMDbClientTest.class, OMDbClientTest.class, OpenSubtitlesXmlRpcTest.class, AcoustIDClientTest.class })
@SuiteClasses({ SimpleDateTest.class, AnidbClientTest.class, TheTVDBClientTest.class, TVMazeClientTest.class, TMDbClientTest.class, OMDbClientTest.class, OpenSubtitlesXmlRpcTest.class, AcoustIDClientTest.class })
public class WebTestSuite {
}