Use multiple patterns instead of one large pattern.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / EpisodeFilter.java
index 91fe098..f206dc3 100644 (file)
 package net.pterodactylus.rhynodge.filters;
 
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Arrays.asList;
 
-import java.util.LinkedHashMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Collection;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -40,7 +43,7 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
 public class EpisodeFilter implements Filter {
 
        /** The pattern to parse episode information from the filename. */
-       private static Pattern episodePattern = Pattern.compile("S(\\d{2})E(\\d{2})|[^\\d](\\d{1,2})x(\\d{2})[^\\d]");
+       private static final Collection<Pattern> episodePatterns = asList(Pattern.compile("S(\\d{2})E(\\d{2})"), Pattern.compile("[^\\d](\\d{1,2})x(\\d{2})[^\\d]"));
 
        //
        // FILTER METHODS
@@ -57,13 +60,15 @@ public class EpisodeFilter implements Filter {
                checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
 
                TorrentState torrentState = (TorrentState) state;
-               LinkedHashMap<Episode, Episode> episodes = new LinkedHashMap<Episode, Episode>();
+               Map<Episode, Episode> episodes = new HashMap<Episode, Episode>();
                for (TorrentFile torrentFile : torrentState) {
                        Episode episode = extractEpisode(torrentFile);
                        if (episode == null) {
                                continue;
                        }
-                       episodes.put(episode, episode);
+                       if (!episodes.containsKey(episode)) {
+                               episodes.put(episode, episode);
+                       }
                        episode = episodes.get(episode);
                        episode.addTorrentFile(torrentFile);
                }
@@ -84,19 +89,18 @@ public class EpisodeFilter implements Filter {
         *         information could be found
         */
        private static Episode extractEpisode(TorrentFile torrentFile) {
-               Matcher matcher = episodePattern.matcher(torrentFile.name());
-               if (!matcher.find()) {
-                       return null;
-               }
-               String seasonString = matcher.group(1);
-               String episodeString = matcher.group(2);
-               if ((seasonString == null) && (episodeString == null)) {
-                       seasonString = matcher.group(3);
-                       episodeString = matcher.group(4);
+               for (Pattern episodePattern : episodePatterns) {
+                       Matcher matcher = episodePattern.matcher(torrentFile.name());
+                       if (!matcher.find() || matcher.groupCount() < 2) {
+                               continue;
+                       }
+                       String seasonString = matcher.group(1);
+                       String episodeString = matcher.group(2);
+                       int season = Integer.valueOf(seasonString);
+                       int episode = Integer.valueOf(episodeString);
+                       return new Episode(season, episode);
                }
-               int season = Integer.valueOf(seasonString);
-               int episode = Integer.valueOf(episodeString);
-               return new Episode(season, episode);
+               return null;
        }
 
 }