2 * Rhynodge - NewEpisodeTrigger.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 java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
25 import net.pterodactylus.rhynodge.State;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.states.EpisodeState;
28 import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
29 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
31 import static com.google.common.base.Preconditions.checkState;
32 import static java.util.function.Function.identity;
33 import static java.util.stream.Collectors.toMap;
36 * {@link Trigger} implementation that compares two {@link EpisodeState}s for
37 * new and changed {@link Episode}s.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class NewEpisodeTrigger implements Trigger {
43 private boolean triggered = false;
49 public State mergeStates(State previousState, State currentState) {
50 checkState(currentState instanceof EpisodeState, "currentState is not a EpisodeState but a %s", currentState.getClass().getName());
51 checkState(previousState instanceof EpisodeState, "previousState is not a EpisodeState but a %s", currentState.getClass().getName());
53 Collection<Episode> newEpisodes = new HashSet<>();
54 Collection<Episode> changedEpisodes = new HashSet<>();
55 Collection<TorrentFile> newTorrentFiles = new HashSet<>();
56 Map<Episode, Episode> allEpisodes = ((EpisodeState) previousState).episodes().stream().collect(toMap(identity(), identity()));
57 for (Episode episode : ((EpisodeState) currentState).episodes()) {
58 if (!allEpisodes.containsKey(episode)) {
59 allEpisodes.put(episode, episode);
60 newEpisodes.add(episode);
63 Episode existingEpisode = allEpisodes.get(episode);
64 for (TorrentFile torrentFile : new ArrayList<>(episode.torrentFiles())) {
65 int oldSize = existingEpisode.torrentFiles().size();
66 existingEpisode.addTorrentFile(torrentFile);
67 int newSize = existingEpisode.torrentFiles().size();
68 if (oldSize != newSize) {
69 newTorrentFiles.add(torrentFile);
71 if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
72 changedEpisodes.add(existingEpisode);
76 return new EpisodeState(allEpisodes.values(), newEpisodes, changedEpisodes, newTorrentFiles);
83 public boolean triggers() {