Return an optional episode instead of null.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / EpisodeFilter.java
1 /*
2  * Rhynodge - EpisodeFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.filters;
19
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Preconditions.checkState;
22 import static java.util.Arrays.asList;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Collection;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import net.pterodactylus.rhynodge.Filter;
31 import net.pterodactylus.rhynodge.State;
32 import net.pterodactylus.rhynodge.states.EpisodeState;
33 import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
34 import net.pterodactylus.rhynodge.states.FailedState;
35 import net.pterodactylus.rhynodge.states.TorrentState;
36 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
37
38 import com.google.common.base.Optional;
39
40 /**
41  * {@link Filter} implementation that extracts {@link Episode} information from
42  * the {@link TorrentFile}s contained in a {@link TorrentState}.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class EpisodeFilter implements Filter {
47
48         /** The pattern to parse episode information from the filename. */
49         private static final Collection<Pattern> episodePatterns = asList(Pattern.compile("[Ss](\\d{2})[Ee](\\d{2})"), Pattern.compile("[^\\d](\\d{1,2})x(\\d{2})[^\\d]"));
50
51         //
52         // FILTER METHODS
53         //
54
55         /**
56          * {@inheritDoc}
57          */
58         @Override
59         public State filter(State state) {
60                 if (!state.success()) {
61                         return FailedState.from(state);
62                 }
63                 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
64
65                 TorrentState torrentState = (TorrentState) state;
66                 Map<Episode, Episode> episodes = new HashMap<Episode, Episode>();
67                 for (TorrentFile torrentFile : torrentState) {
68                         Optional<Episode> episode = extractEpisode(torrentFile);
69                         if (!episode.isPresent()) {
70                                 continue;
71                         }
72                         if (!episodes.containsKey(episode.get())) {
73                                 episodes.put(episode.get(), episode.get());
74                         }
75                         episodes.get(episode.get()).addTorrentFile(torrentFile);
76                 }
77
78                 return new EpisodeState(episodes.values());
79         }
80
81         //
82         // STATIC METHODS
83         //
84
85         /**
86          * Extracts episode information from the given torrent file.
87          *
88          * @param torrentFile
89          *              The torrent file to extract the episode information from
90          * @return The extracted episode information, or {@link Optional#absent()} if
91          *         no episode information could be found
92          */
93         private static Optional<Episode> extractEpisode(TorrentFile torrentFile) {
94                 for (Pattern episodePattern : episodePatterns) {
95                         Matcher matcher = episodePattern.matcher(torrentFile.name());
96                         if (!matcher.find() || matcher.groupCount() < 2) {
97                                 continue;
98                         }
99                         String seasonString = matcher.group(1);
100                         String episodeString = matcher.group(2);
101                         int season = Integer.valueOf(seasonString);
102                         int episode = Integer.valueOf(episodeString);
103                         return Optional.of(new Episode(season, episode));
104                 }
105                 return absent();
106         }
107
108 }