♻️ Move output generation to state
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewEpisodeTrigger.java
1 /*
2  * Rhynodge - NewEpisodeTrigger.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 java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Map;
24
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;
30
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;
34
35 /**
36  * {@link Trigger} implementation that compares two {@link EpisodeState}s for
37  * new and changed {@link Episode}s.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class NewEpisodeTrigger implements Trigger {
42
43         private boolean triggered = false;
44
45         /**
46          * {@inheritDoc}
47          */
48         @Override
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());
52
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);
61                                 triggered = true;
62                         }
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);
70                                 }
71                                 if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
72                                         changedEpisodes.add(existingEpisode);
73                                 }
74                         }
75                 }
76                 return new EpisodeState(allEpisodes.values(), newEpisodes, changedEpisodes, newTorrentFiles);
77         }
78
79         /**
80          * {@inheritDoc}
81          */
82         @Override
83         public boolean triggers() {
84                 return triggered;
85         }
86
87 }