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;
31 import net.pterodactylus.rhynodge.states.ComicState.Strip;
33 import com.google.common.collect.Lists;
34 import org.apache.commons.lang3.StringEscapeUtils;
35 import org.apache.commons.lang3.StringUtils;
38 * {@link Trigger} implementation that detects the presence of new {@link
39 * Comic}s in a {@link ComicState}.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class NewComicTrigger implements Trigger {
45 /** The new comics. */
46 private final List<Comic> newComics = Lists.newArrayList();
49 public State mergeStates(State previousState, State currentState) {
50 checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
51 checkArgument(currentState instanceof ComicState, "current state must be a comic state");
53 ComicState previousComicState = (ComicState) previousState;
54 ComicState currentComicState = (ComicState) currentState;
56 /* copy old state into new state. */
57 ComicState mergedComicState = new ComicState();
58 for (Comic comic : previousComicState) {
59 mergedComicState.add(comic);
63 for (Comic comic : currentComicState) {
64 if (!mergedComicState.comics().contains(comic)) {
66 mergedComicState.add(comic);
70 return mergedComicState;
74 public boolean triggers() {
75 return !newComics.isEmpty();
79 public Output output(Reaction reaction) {
80 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
82 output.addText("text/plain", generatePlainText());
83 output.addText("text/html", generateHtmlText());
93 * Generates a list of the new comics in plain text format.
95 * @return The list of new comics in plain text format
97 private String generatePlainText() {
98 StringBuilder text = new StringBuilder();
100 for (Comic newComic : newComics) {
101 text.append("Comic Found: ").append(newComic.title()).append("\n\n");
102 for (Strip strip : newComic) {
103 text.append("Image: ").append(strip.imageUrl()).append("\n");
104 if (!StringUtils.isBlank(strip.comment())) {
105 text.append("Comment: ").append(strip.comment()).append("\n");
111 return text.toString();
115 * Generates a list of new comics in HTML format.
117 * @return The list of new comics in HTML format
119 private String generateHtmlText() {
120 StringBuilder html = new StringBuilder();
121 html.append("<body>");
123 for (Comic newComic : newComics) {
124 html.append("<h1>").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("</h1>\n");
125 for (Strip strip : newComic) {
126 html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
127 html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
128 html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
129 html.append("\"></div>\n");
133 return html.append("</body>").toString();