2 * Rhynodge - EpisodeState.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.rhynodge.states;
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;
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;
31 import com.fasterxml.jackson.annotation.JsonProperty;
34 * {@link State} implementation that stores episodes of TV shows, parsed via
35 * {@link EpisodeFilter} from a previous {@link TorrentState}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class EpisodeState extends AbstractState implements Iterable<Episode> {
41 /** The episodes found in the current request. */
43 private final List<Episode> episodes = new ArrayList<Episode>();
46 * No-arg constructor for deserialization.
48 @SuppressWarnings("unused")
49 private EpisodeState() {
50 this(Collections.<Episode> emptySet());
54 * Creates a new episode state.
57 * The episodes of the request
59 public EpisodeState(Collection<Episode> episodes) {
60 this.episodes.addAll(episodes);
68 * Returns all episodes contained in this state.
70 * @return The episodes of this state
72 public Collection<Episode> episodes() {
73 return Collections.unmodifiableCollection(episodes);
84 public Iterator<Episode> iterator() {
85 return episodes.iterator();
92 public String toString() {
93 return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes);
97 * Stores attributes for an episode.
99 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
101 public static class Episode implements Comparable<Episode>, Iterable<TorrentFile> {
103 /** The season of the episode. */
105 private final int season;
107 /** The number of the episode. */
109 private final int episode;
111 /** The torrent files for this episode. */
113 private final List<TorrentFile> torrentFiles = new ArrayList<TorrentFile>();
116 * No-arg constructor for deserialization.
118 @SuppressWarnings("unused")
124 * Creates a new episode.
127 * The season of the episode
129 * The number of the episode
131 public Episode(int season, int episode) {
132 this.season = season;
133 this.episode = episode;
141 * Returns the season of this episode.
143 * @return The season of this episode
145 public int season() {
150 * Returns the number of this episode.
152 * @return The number of this episode
154 public int episode() {
159 * Returns the torrent files of this episode.
161 * @return The torrent files of this episode
163 public Collection<TorrentFile> torrentFiles() {
168 * Returns the identifier of this episode.
170 * @return The identifier of this episode
172 public String identifier() {
173 return String.format("S%02dE%02d", season, episode);
181 * Adds the given torrent file to this episode.
184 * The torrent file to add
186 public void addTorrentFile(TorrentFile torrentFile) {
187 torrentFiles.add(torrentFile);
198 public Iterator<TorrentFile> iterator() {
199 return torrentFiles.iterator();
206 public int compareTo(Episode episode) {
207 if (season() < episode.season()) {
210 if (season() > episode.season()) {
213 if (episode() < episode.episode()) {
216 if (episode() > episode.episode()) {
230 public int hashCode() {
231 return season * 65536 + episode;
238 public boolean equals(Object obj) {
239 if (!(obj instanceof Episode)) {
242 Episode episode = (Episode) obj;
243 return (season == episode.season) && (this.episode == episode.episode);
250 public String toString() {
251 return String.format("%s[season=%d,episode=%d,torrentFiles=%s]", getClass().getSimpleName(), season, episode, torrentFiles);