public interface Trigger {
/**
- * Checks whether the given states warrant a change trigger.
+ * Merges the current state into the previous state, returning the merged
+ * state.
*
- * @param currentState
- * The current state of a system
* @param previousState
* The previous state of the system
- * @return {@code true} if the given states warrant a change trigger,
+ * @param currentState
+ * The current state of a system
+ * @return The new state, containing a meaningful merge between the previous
+ * and the current state
+ */
+ State mergeStates(State previousState, State currentState);
+
+ /**
+ * Checks whether the states given to {@link #mergeStates(State, State)}
+ * warrant a change trigger.
+ *
+ * @return {@code true} if the states given to
+ * {@link #mergeStates(State, State)} warrant a change trigger,
* {@code false} otherwise
*/
- boolean triggers(State currentState, State previousState);
+ boolean triggers();
/**
* Returns the output of this trigger. This will only return a meaningful
- * value if {@link #triggers(State, State)} returns {@code true}.
+ * value if {@link #triggers()} returns {@code true}.
*
* @param reaction
* The reaction being triggered
import static com.google.common.base.Preconditions.checkState;
import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
import net.pterodactylus.rhynodge.Reaction;
import net.pterodactylus.rhynodge.State;
import org.apache.commons.lang3.StringEscapeUtils;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Ordering;
+import com.google.common.collect.Sets;
/**
* {@link Trigger} implementation that compares two {@link EpisodeState}s for
public class NewEpisodeTrigger implements Trigger {
/** All new episodes. */
- private Collection<Episode> newEpisodes;
+ private final Collection<Episode> newEpisodes = Sets.newHashSet();
/** All changed episodes. */
- private Collection<Episode> changedEpisodes;
+ private final Collection<Episode> changedEpisodes = Sets.newHashSet();
+
+ /** All episodes. */
+ private final Collection<Episode> allEpisodes = Sets.newHashSet();
//
// TRIGGER METHODS
//
/**
- * {@inheritDoc}
+ * {@inheritDocs}
*/
@Override
- public boolean triggers(State currentState, State previousState) {
+ public State mergeStates(State previousState, State currentState) {
checkState(currentState instanceof EpisodeState, "currentState is not a EpisodeState but a %s", currentState.getClass().getName());
checkState(previousState instanceof EpisodeState, "previousState is not a EpisodeState but a %s", currentState.getClass().getName());
- final EpisodeState currentEpisodeState = (EpisodeState) currentState;
- final EpisodeState previousEpisodeState = (EpisodeState) previousState;
-
- newEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
+ newEpisodes.clear();
+ changedEpisodes.clear();
+ this.allEpisodes.clear();
+ Map<Episode, Episode> allEpisodes = FluentIterable.from(((EpisodeState) previousState).episodes()).toMap(new Function<Episode, Episode>() {
@Override
- public boolean apply(Episode episode) {
- return !previousEpisodeState.episodes().contains(episode);
+ public Episode apply(Episode episode) {
+ return episode;
}
});
-
- changedEpisodes = Collections2.filter(currentEpisodeState.episodes(), new Predicate<Episode>() {
-
- @Override
- public boolean apply(Episode episode) {
- if (!previousEpisodeState.episodes().contains(episode)) {
- return false;
- }
-
- /* find previous episode. */
- final Episode previousEpisode = findPreviousEpisode(episode);
-
- /* compare the list of torrent files. */
- Collection<TorrentFile> newTorrentFiles = Collections2.filter(episode.torrentFiles(), new Predicate<TorrentFile>() {
-
- @Override
- public boolean apply(TorrentFile torrentFile) {
- return !previousEpisode.torrentFiles().contains(torrentFile);
- }
- });
-
- return !newTorrentFiles.isEmpty();
+ for (Episode episode : ((EpisodeState) currentState).episodes()) {
+ if (!allEpisodes.containsKey(episode)) {
+ allEpisodes.put(episode, episode);
+ newEpisodes.add(episode);
}
-
- private Episode findPreviousEpisode(Episode episode) {
- for (Episode previousStateEpisode : previousEpisodeState) {
- if (previousStateEpisode.equals(episode)) {
- return previousStateEpisode;
- }
+ for (TorrentFile torrentFile : episode.torrentFiles()) {
+ int oldSize = allEpisodes.get(episode).torrentFiles().size();
+ allEpisodes.get(episode).addTorrentFile(torrentFile);
+ int newSize = allEpisodes.get(episode).torrentFiles().size();
+ if (!newEpisodes.contains(episode) && (oldSize != newSize)) {
+ changedEpisodes.add(episode);
}
- return null;
}
+ }
+ this.allEpisodes.addAll(allEpisodes.values());
+ return new EpisodeState(this.allEpisodes);
+ }
- });
-
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean triggers() {
return !newEpisodes.isEmpty() || !changedEpisodes.isEmpty();
}
summary = String.format("%d changed Torrent(s) for “%s!”", changedEpisodes.size(), reaction.name());
}
DefaultOutput output = new DefaultOutput(summary);
- output.addText("text/plain", generatePlainText(reaction, newEpisodes, changedEpisodes));
- output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes));
+ output.addText("text/plain", generatePlainText(reaction, newEpisodes, changedEpisodes, allEpisodes));
+ output.addText("text/html", generateHtmlText(reaction, newEpisodes, changedEpisodes, allEpisodes));
return output;
}
* The new episodes
* @param changedEpisodes
* The changed episodes
+ * @param allEpisodes
+ * All episodes
* @return The plain text output
*/
- private static String generatePlainText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
+ private static String generatePlainText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes, Collection<Episode> allEpisodes) {
StringBuilder stringBuilder = new StringBuilder();
if (!newEpisodes.isEmpty()) {
stringBuilder.append(reaction.name()).append(" - New Episodes\n\n");
}
}
}
+ /* list all known episodes. */
+ stringBuilder.append(reaction.name()).append(" - All Known Episodes\n\n");
+ ImmutableMap<Integer, Collection<Episode>> episodesBySeason = FluentIterable.from(allEpisodes).index(new Function<Episode, Integer>() {
+
+ @Override
+ public Integer apply(Episode episode) {
+ return episode.season();
+ }
+ }).asMap();
+ for (Entry<Integer, Collection<Episode>> seasonEntry : episodesBySeason.entrySet()) {
+ stringBuilder.append(" Season ").append(seasonEntry.getKey()).append("\n\n");
+ for (Episode episode : Ordering.natural().sortedCopy(seasonEntry.getValue())) {
+ stringBuilder.append(" Episode ").append(episode.episode()).append("\n");
+ for (TorrentFile torrentFile : episode) {
+ stringBuilder.append(" Size: ").append(torrentFile.size());
+ stringBuilder.append(" in ").append(torrentFile.fileCount()).append(" file(s): ");
+ stringBuilder.append(torrentFile.magnetUri());
+ }
+ }
+ }
+
return stringBuilder.toString();
}
* The new episodes
* @param changedEpisodes
* The changed episodes
+ * @param allEpisodes
+ * All episodes
* @return The HTML output
*/
- private static String generateHtmlText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes) {
+ private static String generateHtmlText(Reaction reaction, Collection<Episode> newEpisodes, Collection<Episode> changedEpisodes, Collection<Episode> allEpisodes) {
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<html><body>\n");
htmlBuilder.append("<h1>").append(StringEscapeUtils.escapeHtml4(reaction.name())).append("</h1>\n");
import static com.google.common.base.Preconditions.checkState;
import java.util.List;
+import java.util.Set;
import net.pterodactylus.rhynodge.Reaction;
import net.pterodactylus.rhynodge.State;
import org.apache.commons.lang3.StringEscapeUtils;
import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
/**
* {@link Trigger} implementation that is triggered by {@link TorrentFile}s that
*/
public class NewTorrentTrigger implements Trigger {
+ /** All known torrents. */
+ private final Set<TorrentFile> allTorrentFiles = Sets.newHashSet();
+
/** The newly detected torrent files. */
- private List<TorrentFile> torrentFiles = Lists.newArrayList();
+ private final List<TorrentFile> newTorrentFiles = Lists.newArrayList();
//
// TRIGGER METHODS
//
/**
- * {@inheritDoc}
+ * {@inheritDocs}
*/
@Override
- public boolean triggers(State currentState, State previousState) {
+ public State mergeStates(State previousState, State currentState) {
checkState(currentState instanceof TorrentState, "currentState is not a TorrentState but a %s", currentState.getClass().getName());
checkState(previousState instanceof TorrentState, "previousState is not a TorrentState but a %s", currentState.getClass().getName());
TorrentState currentTorrentState = (TorrentState) currentState;
TorrentState previousTorrentState = (TorrentState) previousState;
- torrentFiles.clear();
+
+ allTorrentFiles.clear();
+ newTorrentFiles.clear();
+ allTorrentFiles.addAll(previousTorrentState.torrentFiles());
for (TorrentFile torrentFile : currentTorrentState) {
- torrentFiles.add(torrentFile);
- }
- for (TorrentFile torrentFile : previousTorrentState) {
- torrentFiles.remove(torrentFile);
+ if (allTorrentFiles.add(torrentFile)) {
+ newTorrentFiles.add(torrentFile);
+ }
}
- return !torrentFiles.isEmpty();
+
+ return new TorrentState(allTorrentFiles);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean triggers() {
+ return !newTorrentFiles.isEmpty();
}
/**
*/
@Override
public Output output(Reaction reaction) {
- DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", torrentFiles.size(), reaction.name()));
- output.addText("text/plain", getPlainTextList(torrentFiles));
- output.addText("text/html", getHtmlTextList(torrentFiles));
+ DefaultOutput output = new DefaultOutput(String.format("Found %d new Torrent(s) for “%s!”", newTorrentFiles.size(), reaction.name()));
+ output.addText("text/plain", getPlainTextList(newTorrentFiles));
+ output.addText("text/html", getHtmlTextList(newTorrentFiles));
return output;
}