Add strips to comics.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / states / ComicState.java
1 /*
2  * rhynodge - ComicState.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.rhynodge.states;
19
20 import java.util.Iterator;
21 import java.util.List;
22
23 import net.pterodactylus.rhynodge.states.ComicState.Comic;
24
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.google.common.collect.Lists;
27
28 /**
29  * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
30  * {@link Comic}s.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class ComicState extends AbstractState implements Iterable<Comic> {
35
36         /** The comics in this state. */
37         @JsonProperty
38         private final List<Comic> comics = Lists.newArrayList();
39
40         /**
41          * Returns the list of comics contained in this state.
42          *
43          * @return The list of comics in this state
44          */
45         public List<Comic> comics() {
46                 return comics;
47         }
48
49         /**
50          * Adds the given comic to this state.
51          *
52          * @param comic
53          *              The comic to add
54          * @return This comic state
55          */
56         public ComicState add(Comic comic) {
57                 comics.add(comic);
58                 return this;
59         }
60
61         //
62         // ITERABLE METHODS
63         //
64
65         @Override
66         public Iterator<Comic> iterator() {
67                 return comics.iterator();
68         }
69
70         //
71         // OBJECT METHODS
72         //
73
74         @Override
75         public String toString() {
76                 return String.format("ComicState[comics=%s]", comics());
77         }
78
79         /**
80          * Defines a comic.
81          *
82          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
83          */
84         public static class Comic implements Iterable<Strip> {
85
86                 /** The title of the comic. */
87                 @JsonProperty
88                 private final String title;
89
90                 /** The strips of the comic. */
91                 @JsonProperty
92                 private final List<Strip> strips = Lists.newArrayList();
93
94                 /**
95                  * Creates a new comic with the given title.
96                  *
97                  * @param title
98                  *              The title of the comic
99                  */
100                 public Comic(@JsonProperty("title") String title) {
101                         this.title = title;
102                 }
103
104                 /**
105                  * Returns the title of this comic.
106                  *
107                  * @return The title of this comic
108                  */
109                 public String title() {
110                         return title;
111                 }
112
113                 /**
114                  * Returns the strips of this comic.
115                  *
116                  * @return The strips of this comic
117                  */
118                 public List<Strip> strips() {
119                         return strips;
120                 }
121
122                 /**
123                  * Adds a strip to this comic.
124                  *
125                  * @param strip
126                  *              The strip to add
127                  * @return This comic
128                  */
129                 public Comic add(Strip strip) {
130                         strips.add(strip);
131                         return this;
132                 }
133
134                 //
135                 // ITERABLE METHODS
136                 //
137
138                 @Override
139                 public Iterator<Strip> iterator() {
140                         return strips.iterator();
141                 }
142
143                 //
144                 // OBJECT METHODS
145                 //
146
147                 @Override
148                 public int hashCode() {
149                         return title.hashCode() ^ strips().hashCode();
150                 }
151
152                 @Override
153                 public boolean equals(Object object) {
154                         if (!(object instanceof Comic)) {
155                                 return false;
156                         }
157                         Comic comic = (Comic) object;
158                         return title().equals(comic.title()) && strips().equals(comic.strips());
159                 }
160
161                 @Override
162                 public String toString() {
163                         return String.format("Comic[title=%s,strips=%s]", title(), strips());
164                 }
165
166         }
167
168         /**
169          * A strip is a single image that belongs to a comic.
170          *
171          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
172          */
173         public static class Strip {
174
175                 /** The URL of the image. */
176                 @JsonProperty
177                 private final String imageUrl;
178
179                 /** The comment of the image. */
180                 @JsonProperty
181                 private final String comment;
182
183                 /**
184                  * Creates a new strip.
185                  *
186                  * @param imageUrl
187                  *              The URL of the image
188                  * @param comment
189                  *              The comment of the image
190                  */
191                 public Strip(@JsonProperty("imageUrl") String imageUrl, @JsonProperty("comment") String comment) {
192                         this.imageUrl = imageUrl;
193                         this.comment = comment;
194                 }
195
196                 /**
197                  * Returns the URL of the image.
198                  *
199                  * @return The URL of the image
200                  */
201                 public String imageUrl() {
202                         return imageUrl;
203                 }
204
205                 /**
206                  * Returns the comment of the image.
207                  *
208                  * @return The comment of the image
209                  */
210                 public String comment() {
211                         return comment;
212                 }
213
214                 //
215                 // OBJECT METHODS
216                 //
217
218                 @Override
219                 public int hashCode() {
220                         return imageUrl().hashCode() ^ comment().hashCode();
221                 }
222
223                 @Override
224                 public boolean equals(Object object) {
225                         if (!(object instanceof Strip)) {
226                                 return false;
227                         }
228                         Strip strip = (Strip) object;
229                         return imageUrl().equals(strip.imageUrl()) && comment().equals(strip.comment());
230                 }
231
232                 @Override
233                 public String toString() {
234                         return String.format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment());
235                 }
236
237         }
238
239 }