🐛 Fix broken change detection
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / mergers / EpisodeMerger.java
1 /*
2  * Rhynodge - EpisodeMerger.java - Copyright © 2013–2021 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.mergers;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Map;
24
25 import javax.annotation.Nonnull;
26
27 import net.pterodactylus.rhynodge.Merger;
28 import net.pterodactylus.rhynodge.State;
29 import net.pterodactylus.rhynodge.states.EpisodeState;
30 import net.pterodactylus.rhynodge.states.EpisodeState.Episode;
31 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
32
33 import static com.google.common.base.Preconditions.checkState;
34 import static java.util.function.Function.identity;
35 import static java.util.stream.Collectors.toMap;
36
37 /**
38  * {@link Merger} implementation that merges two {@link EpisodeState}s.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class EpisodeMerger implements Merger {
43
44         /**
45          * {@inheritDoc}
46          */
47         @Nonnull
48         @Override
49         public State mergeStates(@Nonnull State previousState, @Nonnull 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                         }
62                         Episode existingEpisode = allEpisodes.get(episode);
63                         for (TorrentFile torrentFile : new ArrayList<>(episode.torrentFiles())) {
64                                 int oldSize = existingEpisode.torrentFiles().size();
65                                 existingEpisode.addTorrentFile(torrentFile);
66                                 int newSize = existingEpisode.torrentFiles().size();
67                                 if (oldSize != newSize) {
68                                         newTorrentFiles.add(torrentFile);
69                                 }
70                                 if (!newEpisodes.contains(existingEpisode) && (oldSize != newSize)) {
71                                         changedEpisodes.add(existingEpisode);
72                                 }
73                         }
74                 }
75                 return new EpisodeState(allEpisodes.values(), newEpisodes, changedEpisodes, newTorrentFiles);
76         }
77
78 }