X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fstates%2FEpisodeState.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fstates%2FEpisodeState.java;h=d7d90b2a60554269ba1ae1c8ed9e8b2de453c391;hb=6f69aff66ba5617d0bb27874014b4274bc551ab8;hp=0000000000000000000000000000000000000000;hpb=13a4fe6bece23b3dd561de657cf9bb7ea307e2b6;p=rhynodge.git diff --git a/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java new file mode 100644 index 0000000..d7d90b2 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java @@ -0,0 +1,236 @@ +/* + * Rhynodge - EpisodeState.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.states; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.filters.EpisodeFilter; +import net.pterodactylus.rhynodge.states.EpisodeState.Episode; +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * {@link State} implementation that stores episodes of TV shows, parsed via + * {@link EpisodeFilter} from a previous {@link TorrentState}. + * + * @author David ‘Bombe’ Roden + */ +public class EpisodeState extends AbstractState implements Iterable { + + /** The episodes found in the current request. */ + @JsonProperty + private final List episodes = new ArrayList(); + + /** + * No-arg constructor for deserialization. + */ + @SuppressWarnings("unused") + private EpisodeState() { + this(Collections. emptySet()); + } + + /** + * Creates a new episode state. + * + * @param episodes + * The episodes of the request + */ + public EpisodeState(Collection episodes) { + this.episodes.addAll(episodes); + } + + // + // ACCESSORS + // + + /** + * Returns all episodes contained in this state. + * + * @return The episodes of this state + */ + public Collection episodes() { + return Collections.unmodifiableCollection(episodes); + } + + // + // ITERABLE INTERFACE + // + + /** + * {@inheritDoc} + */ + @Override + public Iterator iterator() { + return episodes.iterator(); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes); + } + + /** + * Stores attributes for an episode. + * + * @author David ‘Bombe’ Roden + */ + public static class Episode implements Iterable { + + /** The season of the episode. */ + @JsonProperty + private final int season; + + /** The number of the episode. */ + @JsonProperty + private final int episode; + + /** The torrent files for this episode. */ + @JsonProperty + private final List torrentFiles = new ArrayList(); + + /** + * No-arg constructor for deserialization. + */ + @SuppressWarnings("unused") + private Episode() { + this(0, 0); + } + + /** + * Creates a new episode. + * + * @param season + * The season of the episode + * @param episode + * The number of the episode + */ + public Episode(int season, int episode) { + this.season = season; + this.episode = episode; + } + + // + // ACCESSORS + // + + /** + * Returns the season of this episode. + * + * @return The season of this episode + */ + public int season() { + return season; + } + + /** + * Returns the number of this episode. + * + * @return The number of this episode + */ + public int episode() { + return episode; + } + + /** + * Returns the torrent files of this episode. + * + * @return The torrent files of this episode + */ + public Collection torrentFiles() { + return torrentFiles; + } + + /** + * Returns the identifier of this episode. + * + * @return The identifier of this episode + */ + public String identifier() { + return String.format("S%02dE%02d", season, episode); + } + + // + // ACTIONS + // + + /** + * Adds the given torrent file to this episode. + * + * @param torrentFile + * The torrent file to add + */ + public void addTorrentFile(TorrentFile torrentFile) { + torrentFiles.add(torrentFile); + } + + // + // ITERABLE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public Iterator iterator() { + return torrentFiles.iterator(); + } + + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + return season * 65536 + episode; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Episode)) { + return false; + } + Episode episode = (Episode) obj; + return (season == episode.season) && (this.episode == episode.episode); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return String.format("%s[season=%d,episode=%d,torrentFiles=%s]", getClass().getSimpleName(), season, episode, torrentFiles); + } + + } + +}