2 * rhynodge - NewComicTrigger.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.triggers;
20 import static com.google.common.base.Preconditions.*;
22 import java.util.List;
24 import net.pterodactylus.rhynodge.Reaction;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.output.DefaultOutput;
28 import net.pterodactylus.rhynodge.output.Output;
29 import net.pterodactylus.rhynodge.states.ComicState;
30 import net.pterodactylus.rhynodge.states.ComicState.Comic;
32 import com.google.common.collect.Lists;
33 import org.apache.commons.lang3.StringEscapeUtils;
36 * {@link Trigger} implementation that detects the presence of new {@link
37 * Comic}s in a {@link ComicState}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class NewComicTrigger implements Trigger {
43 /** The new comics. */
44 private final List<Comic> newComics = Lists.newArrayList();
47 public State mergeStates(State previousState, State currentState) {
48 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
49 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
51 ComicState previousComicState = (ComicState) previousState;
52 ComicState currentComicState = (ComicState) currentState;
54 /* copy old state into new state. */
55 ComicState mergedComicState = new ComicState();
56 for (Comic comic : previousComicState) {
57 mergedComicState.add(comic);
61 for (Comic comic : currentComicState) {
62 if (!mergedComicState.comics().contains(comic)) {
64 mergedComicState.add(comic);
68 return mergedComicState;
72 public boolean triggers() {
73 return !newComics.isEmpty();
77 public Output output(Reaction reaction) {
78 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
80 output.addText("text/plain", generatePlainText());
81 output.addText("text/html", generateHtmlText());
91 * Generates a list of the new comics in plain text format.
93 * @return The list of new comics in plain text format
95 private String generatePlainText() {
96 StringBuilder text = new StringBuilder();
98 for (Comic newComic : newComics) {
99 text.append("Comic Found: ").append(newComic.title()).append("\n\n");
100 for (String imageUrl : newComic) {
101 text.append("Image: ").append(imageUrl).append("\n");
106 return text.toString();
110 * Generates a list of new comics in HTML format.
112 * @return The list of new comics in HTML format
114 private String generateHtmlText() {
115 StringBuilder html = new StringBuilder();
116 html.append("<body>");
118 for (Comic newComic : newComics) {
119 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("</h1>\n");
120 for (String imageUrl : newComic) {
121 html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(imageUrl)).append("\"></div>\n");
125 return html.append("</body>").toString();