From 462f485591dc67d63d538de023b7a1b4851664b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 24 Feb 2013 16:58:50 +0100 Subject: [PATCH] Add trigger for new comics. --- .../rhynodge/triggers/NewComicTrigger.java | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java diff --git a/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java new file mode 100644 index 0000000..c8852c7 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/triggers/NewComicTrigger.java @@ -0,0 +1,128 @@ +/* + * rhynodge - NewComicTrigger.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.triggers; + +import static com.google.common.base.Preconditions.*; + +import java.util.List; + +import net.pterodactylus.rhynodge.Reaction; +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.Trigger; +import net.pterodactylus.rhynodge.output.DefaultOutput; +import net.pterodactylus.rhynodge.output.Output; +import net.pterodactylus.rhynodge.states.ComicState; +import net.pterodactylus.rhynodge.states.ComicState.Comic; + +import com.google.common.collect.Lists; +import org.apache.commons.lang3.StringEscapeUtils; + +/** + * {@link Trigger} implementation that detects the presence of new {@link + * Comic}s in a {@link ComicState}. + * + * @author David ‘Bombe’ Roden + */ +public class NewComicTrigger implements Trigger { + + /** The new comics. */ + private final List newComics = Lists.newArrayList(); + + @Override + public State mergeStates(State previousState, State currentState) { + checkArgument(previousState instanceof ComicState, "previous state must be a comic state"); + checkArgument(currentState instanceof ComicState, "current state must be a comic state"); + + ComicState previousComicState = (ComicState) previousState; + ComicState currentComicState = (ComicState) currentState; + + /* copy old state into new state. */ + ComicState mergedComicState = new ComicState(); + for (Comic comic : previousComicState) { + mergedComicState.add(comic); + } + + newComics.clear(); + for (Comic comic : currentComicState) { + if (!mergedComicState.comics().contains(comic)) { + newComics.add(comic); + mergedComicState.add(comic); + } + } + + return mergedComicState; + } + + @Override + public boolean triggers() { + return !newComics.isEmpty(); + } + + @Override + public Output output(Reaction reaction) { + DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name())); + + output.addText("text/plain", generatePlainText()); + output.addText("text/html", generateHtmlText()); + + return output; + } + + // + // PRIVATE METHODS + // + + /** + * Generates a list of the new comics in plain text format. + * + * @return The list of new comics in plain text format + */ + private String generatePlainText() { + StringBuilder text = new StringBuilder(); + + for (Comic newComic : newComics) { + text.append("Comic Found: ").append(newComic.title()).append("\n\n"); + for (String imageUrl : newComic) { + text.append("Image: ").append(imageUrl).append("\n"); + } + text.append("\n\n"); + } + + return text.toString(); + } + + /** + * Generates a list of new comics in HTML format. + * + * @return The list of new comics in HTML format + */ + private String generateHtmlText() { + StringBuilder html = new StringBuilder(); + html.append(""); + + for (Comic newComic : newComics) { + html.append("

").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("

\n"); + for (String imageUrl : newComic) { + html.append("
\n"); + } + } + + return html.append("").toString(); + } + +} -- 2.7.4