X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ffilters%2FEpisodeFilter.java;h=821c1f134c31e6ef8fc12c5b90673c01e3c45516;hb=35c7951892f6d9444e6b0324cd325183bcc78653;hp=06e88e16d4b7e5dc0f3ac5758724808e12ddc4b9;hpb=bfdb02dbeca63d3caa30e93ec1595f2274929ab6;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java index 06e88e1..821c1f1 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java @@ -17,11 +17,11 @@ package net.pterodactylus.rhynodge.filters; +import static com.google.common.base.Optional.absent; import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.FluentIterable.from; import static java.util.Arrays.asList; -import java.util.HashMap; -import java.util.Map; import java.util.Collection; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -34,6 +34,11 @@ import net.pterodactylus.rhynodge.states.FailedState; import net.pterodactylus.rhynodge.states.TorrentState; import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; + /** * {@link Filter} implementation that extracts {@link Episode} information from * the {@link TorrentFile}s contained in a {@link TorrentState}. @@ -60,20 +65,16 @@ public class EpisodeFilter implements Filter { checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass()); TorrentState torrentState = (TorrentState) state; - Map episodes = new HashMap(); + final Multimap episodes = HashMultimap.create(); 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); - } - episode = episodes.get(episode); - episode.addTorrentFile(torrentFile); + episodes.put(episode.get(), torrentFile); } - return new EpisodeState(episodes.values()); + return new EpisodeState(from(episodes.keySet()).transform(episodeFiller(episodes)).toSet()); } // @@ -81,14 +82,35 @@ public class EpisodeFilter implements Filter { // /** + * Returns a function that creates an {@link Episode} that contains all {@link + * TorrentFile}s. + * + * @param episodeTorrents + * A multimap mapping episodes to torrent files. + * @return The function that performs the extraction of torrent files + */ + private static Function episodeFiller(final Multimap episodeTorrents) { + return new Function() { + @Override + public Episode apply(Episode episode) { + Episode completeEpisode = new Episode(episode.season(), episode.episode()); + for (TorrentFile torrentFile : episodeTorrents.get(episode)) { + completeEpisode.addTorrentFile(torrentFile); + } + return completeEpisode; + } + }; + } + + /** * 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 +120,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(); } }