🔀 Merge branch 'release-79'
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityChangeEventSender.java
1 /*
2  * Sone - IdentityChangeEventSender.java - Copyright Â© 2013–2019 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.sone.freenet.wot;
19
20 import java.util.Collection;
21 import java.util.Map;
22
23 import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
24 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
25 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
26 import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
27 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
28 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
29
30 import com.google.common.eventbus.EventBus;
31
32 /**
33  * Detects changes in {@link Identity}s trusted my multiple {@link
34  * OwnIdentity}s.
35  *
36  * @see IdentityChangeDetector
37  */
38 public class IdentityChangeEventSender {
39
40         private final EventBus eventBus;
41         private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
42
43         public IdentityChangeEventSender(EventBus eventBus, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
44                 this.eventBus = eventBus;
45                 this.oldIdentities = oldIdentities;
46         }
47
48         public void detectChanges(Map<OwnIdentity, Collection<Identity>> identities) {
49                 IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet());
50                 identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities));
51                 identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities));
52                 identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(identities, oldIdentities));
53                 identityChangeDetector.detectChanges(identities.keySet());
54         }
55
56         private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> newIdentities) {
57                 return new IdentityProcessor() {
58                         @Override
59                         public void processIdentity(Identity identity) {
60                                 eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity));
61                                 for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) {
62                                         eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity));
63                                 }
64                         }
65                 };
66         }
67
68         private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> oldIdentities) {
69                 return new IdentityProcessor() {
70                         @Override
71                         public void processIdentity(Identity identity) {
72                                 eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity));
73                                 for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) {
74                                         eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity));
75                                 }
76                         }
77                 };
78         }
79
80         private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
81                 return new DefaultIdentityProcessor(oldIdentities, newIdentities);
82         }
83
84         private class DefaultIdentityProcessor implements IdentityProcessor {
85
86                 private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
87                 private final Map<OwnIdentity, Collection<Identity>> newIdentities;
88
89                 public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
90                         this.oldIdentities = oldIdentities;
91                         this.newIdentities = newIdentities;
92                 }
93
94                 @Override
95                 public void processIdentity(Identity ownIdentity) {
96                         IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity));
97                         identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity));
98                         identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity));
99                         identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity));
100                         identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity));
101                 }
102
103                 private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) {
104                         return new IdentityProcessor() {
105                                 @Override
106                                 public void processIdentity(Identity identity) {
107                                         eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
108                                 }
109                         };
110                 }
111
112                 private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) {
113                         return new IdentityProcessor() {
114                                 @Override
115                                 public void processIdentity(Identity identity) {
116                                         eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
117                                 }
118                         };
119                 }
120
121                 private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) {
122                         return new IdentityProcessor() {
123                                 @Override
124                                 public void processIdentity(Identity identity) {
125                                         eventBus.post(new IdentityAddedEvent(ownIdentity, identity));
126                                 }
127                         };
128                 }
129
130         }
131
132 }