2 * rhynodge - ComicState.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.Iterator;
21 import java.util.List;
23 import net.pterodactylus.rhynodge.states.ComicState.Comic;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.google.common.collect.Lists;
29 * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class ComicState extends AbstractState implements Iterable<Comic> {
36 /** The comics in this state. */
38 private final List<Comic> comics = Lists.newArrayList();
41 * Returns the list of comics contained in this state.
43 * @return The list of comics in this state
45 public List<Comic> comics() {
50 * Adds the given comic to this state.
54 * @return This comic state
56 public ComicState add(Comic comic) {
66 public Iterator<Comic> iterator() {
67 return comics.iterator();
75 public String toString() {
76 return String.format("ComicState[comics=%s]", comics());
82 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
84 public static class Comic implements Iterable<Strip> {
86 /** The title of the comic. */
88 private final String title;
90 /** The strips of the comic. */
92 private final List<Strip> strips = Lists.newArrayList();
95 * Creates a new comic with the given title.
98 * The title of the comic
100 public Comic(@JsonProperty("title") String title) {
105 * Returns the title of this comic.
107 * @return The title of this comic
109 public String title() {
114 * Returns the strips of this comic.
116 * @return The strips of this comic
118 public List<Strip> strips() {
123 * Adds a strip to this comic.
129 public Comic add(Strip strip) {
139 public Iterator<Strip> iterator() {
140 return strips.iterator();
148 public int hashCode() {
149 return title.hashCode() ^ strips().hashCode();
153 public boolean equals(Object object) {
154 if (!(object instanceof Comic)) {
157 Comic comic = (Comic) object;
158 return title().equals(comic.title()) && strips().equals(comic.strips());
162 public String toString() {
163 return String.format("Comic[title=%s,strips=%s]", title(), strips());
169 * A strip is a single image that belongs to a comic.
171 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
173 public static class Strip {
175 /** The URL of the image. */
177 private final String imageUrl;
179 /** The comment of the image. */
181 private final String comment;
184 * Creates a new strip.
187 * The URL of the image
189 * The comment of the image
191 public Strip(@JsonProperty("imageUrl") String imageUrl, @JsonProperty("comment") String comment) {
192 this.imageUrl = imageUrl;
193 this.comment = comment;
197 * Returns the URL of the image.
199 * @return The URL of the image
201 public String imageUrl() {
206 * Returns the comment of the image.
208 * @return The comment of the image
210 public String comment() {
219 public int hashCode() {
220 return imageUrl().hashCode() ^ comment().hashCode();
224 public boolean equals(Object object) {
225 if (!(object instanceof Strip)) {
228 Strip strip = (Strip) object;
229 return imageUrl().equals(strip.imageUrl()) && comment().equals(strip.comment());
233 public String toString() {
234 return String.format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment());