Adds a state for 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.google.common.collect.Lists;
26
27 /**
28  * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
29  * {@link Comic}s.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class ComicState extends AbstractState implements Iterable<Comic> {
34
35         /** The comics in this state. */
36         private List<Comic> comics = Lists.newArrayList();
37
38         /**
39          * Returns the list of comics contained in this state.
40          *
41          * @return The list of comics in this state
42          */
43         public List<Comic> comics() {
44                 return comics;
45         }
46
47         /**
48          * Adds the given comic to this state.
49          *
50          * @param comic
51          *              The comic to add
52          * @return This comic state
53          */
54         public ComicState add(Comic comic) {
55                 comics.add(comic);
56                 return this;
57         }
58
59         //
60         // ITERABLE METHODS
61         //
62
63         @Override
64         public Iterator<Comic> iterator() {
65                 return comics.iterator();
66         }
67
68         //
69         // OBJECT METHODS
70         //
71
72         @Override
73         public String toString() {
74                 return String.format("ComicState[comics=%s]", comics());
75         }
76
77         /**
78          * Defines a comic.
79          *
80          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
81          */
82         public static class Comic implements Iterable<String> {
83
84                 /** The title of the comic. */
85                 private final String title;
86
87                 /** The URLs of the comic’s images. */
88                 private final List<String> imageUrls = Lists.newArrayList();
89
90                 /**
91                  * Creates a new comic with the given title.
92                  *
93                  * @param title
94                  *              The title of the comic
95                  */
96                 public Comic(String title) {
97                         this.title = title;
98                 }
99
100                 /**
101                  * Returns the title of this comic.
102                  *
103                  * @return The title of this comic
104                  */
105                 public String title() {
106                         return title;
107                 }
108
109                 /**
110                  * Returns the URLs of this comic’s images.
111                  *
112                  * @return The URLs of this comic’s images
113                  */
114                 public List<String> imageUrls() {
115                         return imageUrls();
116                 }
117
118                 /**
119                  * Adds an image URL to this comic.
120                  *
121                  * @param imageUrl
122                  *              The URL of the comic image to add
123                  * @return This comic
124                  */
125                 public Comic addImageUrl(String imageUrl) {
126                         imageUrls.add(imageUrl);
127                         return this;
128                 }
129
130                 //
131                 // ITERABLE METHODS
132                 //
133
134                 @Override
135                 public Iterator<String> iterator() {
136                         return imageUrls.iterator();
137                 }
138
139                 //
140                 // OBJECT METHODS
141                 //
142
143                 @Override
144                 public int hashCode() {
145                         return title.hashCode() ^ imageUrls().hashCode();
146                 }
147
148                 @Override
149                 public boolean equals(Object object) {
150                         if (!(object instanceof Comic)) {
151                                 return false;
152                         }
153                         Comic comic = (Comic) object;
154                         return title().equals(comic.title()) && imageUrls().equals(comic.imageUrls());
155                 }
156
157                 @Override
158                 public String toString() {
159                         return String.format("Comic[title=%s,imageUrls=%s]", title(), imageUrls());
160                 }
161
162         }
163
164 }