Add function to extract the season number of an episode.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / EpisodeState.java
index d7d90b2..7ed3dce 100644 (file)
@@ -29,6 +29,7 @@ import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Function;
 
 /**
  * {@link State} implementation that stores episodes of TV shows, parsed via
@@ -98,7 +99,16 @@ public class EpisodeState extends AbstractState implements Iterable<Episode> {
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       public static class Episode implements Iterable<TorrentFile> {
+       public static class Episode implements Comparable<Episode>, Iterable<TorrentFile> {
+
+               /** Function to extract the season of an episode. */
+               public static final Function<Episode, Integer> BY_SEASON = new Function<Episode, Integer>() {
+
+                       @Override
+                       public Integer apply(Episode episode) {
+                               return episode.season();
+                       }
+               };
 
                /** The season of the episode. */
                @JsonProperty
@@ -199,6 +209,26 @@ public class EpisodeState extends AbstractState implements Iterable<Episode> {
                        return torrentFiles.iterator();
                }
 
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public int compareTo(Episode episode) {
+                       if (season() < episode.season()) {
+                               return -1;
+                       }
+                       if (season() > episode.season()) {
+                               return 1;
+                       }
+                       if (episode() < episode.episode()) {
+                               return -1;
+                       }
+                       if (episode() > episode.episode()) {
+                               return 1;
+                       }
+                       return 0;
+               }
+
                //
                // OBJECT METHODS
                //