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 * Returns all episodes contained in this state.
71 * @return The episodes of this state
73 public Collection<Episode> episodes() {
74 return Collections.unmodifiableCollection(episodes);
85 public Iterator<Episode> iterator() {
86 return episodes.iterator();
93 public String toString() {
94 return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes);
98 * Stores attributes for an episode.
100 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
102 public static class Episode implements Comparable<Episode>, Iterable<TorrentFile> {
104 /** Function to extract the season of an episode. */
105 public static final Function<Episode, Integer> BY_SEASON = new Function<Episode, Integer>() {
108 public Integer apply(Episode episode) {
109 return episode.season();
113 /** The season of the episode. */
115 private final int season;
117 /** The number of the episode. */
119 private final int episode;
121 /** The torrent files for this episode. */
123 private final List<TorrentFile> torrentFiles = new ArrayList<TorrentFile>();
126 * No-arg constructor for deserialization.
128 @SuppressWarnings("unused")
134 * Creates a new episode.
137 * The season of the episode
139 * The number of the episode
141 public Episode(int season, int episode) {
142 this.season = season;
143 this.episode = episode;
151 * Returns the season of this episode.
153 * @return The season of this episode
155 public int season() {
160 * Returns the number of this episode.
162 * @return The number of this episode
164 public int episode() {
169 * Returns the torrent files of this episode.
171 * @return The torrent files of this episode
173 public Collection<TorrentFile> torrentFiles() {
178 * Returns the identifier of this episode.
180 * @return The identifier of this episode
182 public String identifier() {
183 return String.format("S%02dE%02d", season, episode);
191 * Adds the given torrent file to this episode.
194 * The torrent file to add
196 public void addTorrentFile(TorrentFile torrentFile) {
197 if (!torrentFiles.contains(torrentFile)) {
198 torrentFiles.add(torrentFile);
210 public Iterator<TorrentFile> iterator() {
211 return torrentFiles.iterator();
218 public int compareTo(Episode episode) {
219 if (season() < episode.season()) {
222 if (season() > episode.season()) {
225 if (episode() < episode.episode()) {
228 if (episode() > episode.episode()) {
242 public int hashCode() {
243 return season * 65536 + episode;
250 public boolean equals(Object obj) {
251 if (!(obj instanceof Episode)) {
254 Episode episode = (Episode) obj;
255 return (season == episode.season) && (this.episode == episode.episode);
262 public String toString() {
263 return String.format("%s[season=%d,episode=%d,torrentFiles=%s]", getClass().getSimpleName(), season, episode, torrentFiles);