From d66a56f793fecb5b6fc8cc1f30fdf76edd56a277 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Sep 2013 11:49:05 +0200 Subject: [PATCH] Return an optional episode instead of null. --- .../rhynodge/filters/EpisodeFilter.java | 26 ++++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java index 06e88e1..9318d84 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java @@ -17,6 +17,7 @@ package net.pterodactylus.rhynodge.filters; +import static com.google.common.base.Optional.absent; import static com.google.common.base.Preconditions.checkState; import static java.util.Arrays.asList; @@ -34,6 +35,8 @@ import net.pterodactylus.rhynodge.states.FailedState; import net.pterodactylus.rhynodge.states.TorrentState; import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; +import com.google.common.base.Optional; + /** * {@link Filter} implementation that extracts {@link Episode} information from * the {@link TorrentFile}s contained in a {@link TorrentState}. @@ -62,15 +65,14 @@ public class EpisodeFilter implements Filter { TorrentState torrentState = (TorrentState) state; Map episodes = new HashMap(); for (TorrentFile torrentFile : torrentState) { - Episode episode = extractEpisode(torrentFile); - if (episode == null) { + Optional episode = extractEpisode(torrentFile); + if (!episode.isPresent()) { continue; } - if (!episodes.containsKey(episode)) { - episodes.put(episode, episode); + if (!episodes.containsKey(episode.get())) { + episodes.put(episode.get(), episode.get()); } - episode = episodes.get(episode); - episode.addTorrentFile(torrentFile); + episodes.get(episode.get()).addTorrentFile(torrentFile); } return new EpisodeState(episodes.values()); @@ -84,11 +86,11 @@ public class EpisodeFilter implements Filter { * Extracts episode information from the given torrent file. * * @param torrentFile - * The torrent file to extract the episode information from - * @return The extracted episode information, or {@code null} if no episode - * information could be found + * The torrent file to extract the episode information from + * @return The extracted episode information, or {@link Optional#absent()} if + * no episode information could be found */ - private static Episode extractEpisode(TorrentFile torrentFile) { + private static Optional extractEpisode(TorrentFile torrentFile) { for (Pattern episodePattern : episodePatterns) { Matcher matcher = episodePattern.matcher(torrentFile.name()); if (!matcher.find() || matcher.groupCount() < 2) { @@ -98,9 +100,9 @@ public class EpisodeFilter implements Filter { String episodeString = matcher.group(2); int season = Integer.valueOf(seasonString); int episode = Integer.valueOf(episodeString); - return new Episode(season, episode); + return Optional.of(new Episode(season, episode)); } - return null; + return absent(); } } -- 2.7.4