Make episodes sortable.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / EpisodeState.java
1 /*
2  * Rhynodge - EpisodeState.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.states;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.State;
27 import net.pterodactylus.rhynodge.filters.EpisodeFilter;
28 import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
29 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
30
31 import com.fasterxml.jackson.annotation.JsonProperty;
32
33 /**
34  * {@link State} implementation that stores episodes of TV shows, parsed via
35  * {@link EpisodeFilter} from a previous {@link TorrentState}.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class EpisodeState extends AbstractState implements Iterable<Episode> {
40
41         /** The episodes found in the current request. */
42         @JsonProperty
43         private final List<Episode> episodes = new ArrayList<Episode>();
44
45         /**
46          * No-arg constructor for deserialization.
47          */
48         @SuppressWarnings("unused")
49         private EpisodeState() {
50                 this(Collections.<Episode> emptySet());
51         }
52
53         /**
54          * Creates a new episode state.
55          *
56          * @param episodes
57          *            The episodes of the request
58          */
59         public EpisodeState(Collection<Episode> episodes) {
60                 this.episodes.addAll(episodes);
61         }
62
63         //
64         // ACCESSORS
65         //
66
67         /**
68          * Returns all episodes contained in this state.
69          *
70          * @return The episodes of this state
71          */
72         public Collection<Episode> episodes() {
73                 return Collections.unmodifiableCollection(episodes);
74         }
75
76         //
77         // ITERABLE INTERFACE
78         //
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public Iterator<Episode> iterator() {
85                 return episodes.iterator();
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         @Override
92         public String toString() {
93                 return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes);
94         }
95
96         /**
97          * Stores attributes for an episode.
98          *
99          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
100          */
101         public static class Episode implements Comparable<Episode>, Iterable<TorrentFile> {
102
103                 /** The season of the episode. */
104                 @JsonProperty
105                 private final int season;
106
107                 /** The number of the episode. */
108                 @JsonProperty
109                 private final int episode;
110
111                 /** The torrent files for this episode. */
112                 @JsonProperty
113                 private final List<TorrentFile> torrentFiles = new ArrayList<TorrentFile>();
114
115                 /**
116                  * No-arg constructor for deserialization.
117                  */
118                 @SuppressWarnings("unused")
119                 private Episode() {
120                         this(0, 0);
121                 }
122
123                 /**
124                  * Creates a new episode.
125                  *
126                  * @param season
127                  *            The season of the episode
128                  * @param episode
129                  *            The number of the episode
130                  */
131                 public Episode(int season, int episode) {
132                         this.season = season;
133                         this.episode = episode;
134                 }
135
136                 //
137                 // ACCESSORS
138                 //
139
140                 /**
141                  * Returns the season of this episode.
142                  *
143                  * @return The season of this episode
144                  */
145                 public int season() {
146                         return season;
147                 }
148
149                 /**
150                  * Returns the number of this episode.
151                  *
152                  * @return The number of this episode
153                  */
154                 public int episode() {
155                         return episode;
156                 }
157
158                 /**
159                  * Returns the torrent files of this episode.
160                  *
161                  * @return The torrent files of this episode
162                  */
163                 public Collection<TorrentFile> torrentFiles() {
164                         return torrentFiles;
165                 }
166
167                 /**
168                  * Returns the identifier of this episode.
169                  *
170                  * @return The identifier of this episode
171                  */
172                 public String identifier() {
173                         return String.format("S%02dE%02d", season, episode);
174                 }
175
176                 //
177                 // ACTIONS
178                 //
179
180                 /**
181                  * Adds the given torrent file to this episode.
182                  *
183                  * @param torrentFile
184                  *            The torrent file to add
185                  */
186                 public void addTorrentFile(TorrentFile torrentFile) {
187                         torrentFiles.add(torrentFile);
188                 }
189
190                 //
191                 // ITERABLE METHODS
192                 //
193
194                 /**
195                  * {@inheritDoc}
196                  */
197                 @Override
198                 public Iterator<TorrentFile> iterator() {
199                         return torrentFiles.iterator();
200                 }
201
202                 /**
203                  * {@inheritDoc}
204                  */
205                 @Override
206                 public int compareTo(Episode episode) {
207                         if (season() < episode.season()) {
208                                 return -1;
209                         }
210                         if (season() > episode.season()) {
211                                 return 1;
212                         }
213                         if (episode() < episode.episode()) {
214                                 return -1;
215                         }
216                         if (episode() > episode.episode()) {
217                                 return 1;
218                         }
219                         return 0;
220                 }
221
222                 //
223                 // OBJECT METHODS
224                 //
225
226                 /**
227                  * {@inheritDoc}
228                  */
229                 @Override
230                 public int hashCode() {
231                         return season * 65536 + episode;
232                 }
233
234                 /**
235                  * {@inheritDoc}
236                  */
237                 @Override
238                 public boolean equals(Object obj) {
239                         if (!(obj instanceof Episode)) {
240                                 return false;
241                         }
242                         Episode episode = (Episode) obj;
243                         return (season == episode.season) && (this.episode == episode.episode);
244                 }
245
246                 /**
247                  * {@inheritDoc}
248                  */
249                 @Override
250                 public String toString() {
251                         return String.format("%s[season=%d,episode=%d,torrentFiles=%s]", getClass().getSimpleName(), season, episode, torrentFiles);
252                 }
253
254         }
255
256 }