Add trigger for new comics.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewComicTrigger.java
1 /*
2  * rhynodge - NewComicTrigger.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.triggers;
19
20 import static com.google.common.base.Preconditions.*;
21
22 import java.util.List;
23
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
32 import com.google.common.collect.Lists;
33 import org.apache.commons.lang3.StringEscapeUtils;
34
35 /**
36  * {@link Trigger} implementation that detects the presence of new {@link
37  * Comic}s in a {@link ComicState}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class NewComicTrigger implements Trigger {
42
43         /** The new comics. */
44         private final List<Comic> newComics = Lists.newArrayList();
45
46         @Override
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");
50
51                 ComicState previousComicState = (ComicState) previousState;
52                 ComicState currentComicState = (ComicState) currentState;
53
54                 /* copy old state into new state. */
55                 ComicState mergedComicState = new ComicState();
56                 for (Comic comic : previousComicState) {
57                         mergedComicState.add(comic);
58                 }
59
60                 newComics.clear();
61                 for (Comic comic : currentComicState) {
62                         if (!mergedComicState.comics().contains(comic)) {
63                                 newComics.add(comic);
64                                 mergedComicState.add(comic);
65                         }
66                 }
67
68                 return mergedComicState;
69         }
70
71         @Override
72         public boolean triggers() {
73                 return !newComics.isEmpty();
74         }
75
76         @Override
77         public Output output(Reaction reaction) {
78                 DefaultOutput output = new DefaultOutput(String.format("New Comic found for “%s!”", reaction.name()));
79
80                 output.addText("text/plain", generatePlainText());
81                 output.addText("text/html", generateHtmlText());
82
83                 return output;
84         }
85
86         //
87         // PRIVATE METHODS
88         //
89
90         /**
91          * Generates a list of the new comics in plain text format.
92          *
93          * @return The list of new comics in plain text format
94          */
95         private String generatePlainText() {
96                 StringBuilder text = new StringBuilder();
97
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");
102                         }
103                         text.append("\n\n");
104                 }
105
106                 return text.toString();
107         }
108
109         /**
110          * Generates a list of new comics in HTML format.
111          *
112          * @return The list of new comics in HTML format
113          */
114         private String generateHtmlText() {
115                 StringBuilder html = new StringBuilder();
116                 html.append("<body>");
117
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");
122                         }
123                 }
124
125                 return html.append("</body>").toString();
126         }
127
128 }