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.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
27 import javax.annotation.Nonnull;
28 import javax.annotation.Nullable;
30 import net.pterodactylus.rhynodge.Reaction;
31 import net.pterodactylus.rhynodge.states.ComicState.Comic;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34 import com.google.common.collect.Lists;
35 import org.apache.commons.lang3.StringEscapeUtils;
36 import org.apache.commons.lang3.StringUtils;
38 import static java.lang.String.format;
41 * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class ComicState extends AbstractState implements Iterable<Comic> {
49 private final List<Comic> comics = Lists.newArrayList();
50 private final Set<Comic> newComics = new HashSet<>();
52 @SuppressWarnings("unused")
53 // used for deserialization
54 private ComicState() {
57 public ComicState(Collection<Comic> allComics) {
58 this.comics.addAll(allComics);
61 public ComicState(Collection<Comic> allComics, Collection<Comic> newComics) {
63 this.newComics.addAll(newComics);
67 public boolean isEmpty() {
68 return comics.isEmpty();
71 public List<Comic> comics() {
76 public Iterator<Comic> iterator() {
77 return comics.iterator();
81 public String toString() {
82 return format("ComicState[comics=%s]", comics());
87 protected String summary(Reaction reaction) {
88 return format("New Comic found for “%s!”", reaction.name());
93 protected String plainText() {
94 StringBuilder text = new StringBuilder();
96 for (Comic newComic : newComics) {
97 text.append("Comic Found: ").append(newComic.title()).append("\n\n");
98 for (Strip strip : newComic) {
99 text.append("Image: ").append(strip.imageUrl()).append("\n");
100 if (!StringUtils.isBlank(strip.comment())) {
101 text.append("Comment: ").append(strip.comment()).append("\n");
107 return text.toString();
112 protected String htmlText() {
113 StringBuilder html = new StringBuilder();
114 html.append("<body>");
116 for (Comic newComic : newComics) {
117 generateComicHtml(html, newComic);
120 List<Comic> latestComics = new ArrayList<>(comics());
121 Collections.reverse(latestComics);
123 for (Comic comic : latestComics) {
124 if (newComics.contains(comic)) {
127 generateComicHtml(html, comic);
128 if (++comicCount == 7) {
133 return html.append("</body>").toString();
136 private void generateComicHtml(StringBuilder html, Comic comic) {
137 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
138 for (Strip strip : comic) {
139 html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
140 html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
141 html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
142 html.append("\"></div>\n");
143 html.append("<div>").append(StringEscapeUtils.escapeHtml4(strip.comment())).append("</div>\n");
150 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
152 public static class Comic implements Iterable<Strip> {
155 private final String title;
158 private final List<Strip> strips = Lists.newArrayList();
160 public Comic(@JsonProperty("title") String title) {
164 public String title() {
168 public List<Strip> strips() {
172 public Comic add(Strip strip) {
178 public Iterator<Strip> iterator() {
179 return strips.iterator();
183 public int hashCode() {
184 return title.hashCode() ^ strips().hashCode();
188 public boolean equals(Object object) {
189 if (!(object instanceof Comic)) {
192 Comic comic = (Comic) object;
193 return title().equals(comic.title()) && strips().equals(comic.strips());
197 public String toString() {
198 return format("Comic[title=%s,strips=%s]", title(), strips());
204 * A strip is a single image that belongs to a comic.
206 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
208 public static class Strip {
211 private final String imageUrl;
214 private final String comment;
216 public Strip(@JsonProperty("imageUrl") String imageUrl, @JsonProperty("comment") String comment) {
217 this.imageUrl = imageUrl;
218 this.comment = comment;
221 public String imageUrl() {
225 public String comment() {
230 public int hashCode() {
231 return imageUrl().hashCode() ^ comment().hashCode();
235 public boolean equals(Object object) {
236 if (!(object instanceof Strip)) {
239 Strip strip = (Strip) object;
240 return imageUrl().equals(strip.imageUrl()) && comment().equals(strip.comment());
244 public String toString() {
245 return format("Strip[imageUrl=%s,comment=%s]", imageUrl(), comment());