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;
32 import com.google.common.base.Function;
35 * {@link State} implementation that stores episodes of TV shows, parsed via
36 * {@link EpisodeFilter} from a previous {@link TorrentState}.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class EpisodeState extends AbstractState implements Iterable<Episode> {
42 /** The episodes found in the current request. */
44 private final List<Episode> episodes = new ArrayList<Episode>();
47 * No-arg constructor for deserialization.
49 @SuppressWarnings("unused")
50 private EpisodeState() {
51 this(Collections.<Episode> emptySet());
55 * Creates a new episode state.
58 * The episodes of the request
60 public EpisodeState(Collection<Episode> episodes) {
61 this.episodes.addAll(episodes);
69 public boolean isEmpty() {
70 return episodes.isEmpty();
74 * Returns all episodes contained in this state.
76 * @return The episodes of this state
78 public Collection<Episode> episodes() {
79 return Collections.unmodifiableCollection(episodes);
90 public Iterator<Episode> iterator() {
91 return episodes.iterator();
98 public String toString() {
99 return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes);
103 * Stores attributes for an episode.
105 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
107 public static class Episode implements Comparable<Episode>, Iterable<TorrentFile> {
109 /** Function to extract the season of an episode. */
110 public static final Function<Episode, Integer> BY_SEASON = new Function<Episode, Integer>() {
113 public Integer apply(Episode episode) {
114 return (episode != null ) ? episode.season() : -1;
118 /** The season of the episode. */
120 private final int season;
122 /** The number of the episode. */
124 private final int episode;
126 /** The torrent files for this episode. */
128 private final List<TorrentFile> torrentFiles = new ArrayList<TorrentFile>();
131 * No-arg constructor for deserialization.
133 @SuppressWarnings("unused")
139 * Creates a new episode.
142 * The season of the episode
144 * The number of the episode
146 public Episode(int season, int episode) {
147 this.season = season;
148 this.episode = episode;
156 * Returns the season of this episode.
158 * @return The season of this episode
160 public int season() {
165 * Returns the number of this episode.
167 * @return The number of this episode
169 public int episode() {
174 * Returns the torrent files of this episode.
176 * @return The torrent files of this episode
178 public Collection<TorrentFile> torrentFiles() {
183 * Returns the identifier of this episode.
185 * @return The identifier of this episode
187 public String identifier() {
188 return String.format("S%02dE%02d", season, episode);
196 * Adds the given torrent file to this episode.
199 * The torrent file to add
201 public void addTorrentFile(TorrentFile torrentFile) {
202 if (!torrentFiles.contains(torrentFile)) {
203 torrentFiles.add(torrentFile);
215 public Iterator<TorrentFile> iterator() {
216 return torrentFiles.iterator();
223 public int compareTo(Episode episode) {
224 if (season() < episode.season()) {
227 if (season() > episode.season()) {
230 if (episode() < episode.episode()) {
233 if (episode() > episode.episode()) {
247 public int hashCode() {
248 return season * 65536 + episode;
255 public boolean equals(Object obj) {
256 if (!(obj instanceof Episode)) {
259 Episode episode = (Episode) obj;
260 return (season == episode.season) && (this.episode == episode.episode);
267 public String toString() {
268 return String.format("%s[season=%d,episode=%d,torrentFiles=%s]", getClass().getSimpleName(), season, episode, torrentFiles);