Change dates in file headers
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityChangeEventSender.java
1 /*
2  * Sone - IdentityChangeEventSender.java - Copyright © 2013–2016 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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  * @see IdentityChangeDetector
38  */
39 public class IdentityChangeEventSender {
40
41         private final EventBus eventBus;
42         private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
43
44         public IdentityChangeEventSender(EventBus eventBus, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
45                 this.eventBus = eventBus;
46                 this.oldIdentities = oldIdentities;
47         }
48
49         public void detectChanges(Map<OwnIdentity, Collection<Identity>> identities) {
50                 IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet());
51                 identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities));
52                 identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities));
53                 identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(identities, oldIdentities));
54                 identityChangeDetector.detectChanges(identities.keySet());
55         }
56
57         private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> newIdentities) {
58                 return new IdentityProcessor() {
59                         @Override
60                         public void processIdentity(Identity identity) {
61                                 eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity));
62                                 for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) {
63                                         eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity));
64                                 }
65                         }
66                 };
67         }
68
69         private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> oldIdentities) {
70                 return new IdentityProcessor() {
71                         @Override
72                         public void processIdentity(Identity identity) {
73                                 eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity));
74                                 for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) {
75                                         eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity));
76                                 }
77                         }
78                 };
79         }
80
81         private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
82                 return new DefaultIdentityProcessor(oldIdentities, newIdentities);
83         }
84
85         private class DefaultIdentityProcessor implements IdentityProcessor {
86
87                 private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
88                 private final Map<OwnIdentity, Collection<Identity>> newIdentities;
89
90                 public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
91                         this.oldIdentities = oldIdentities;
92                         this.newIdentities = newIdentities;
93                 }
94
95                 @Override
96                 public void processIdentity(Identity ownIdentity) {
97                         IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity));
98                         identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity));
99                         identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity));
100                         identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity));
101                         identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity));
102                 }
103
104                 private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) {
105                         return new IdentityProcessor() {
106                                 @Override
107                                 public void processIdentity(Identity identity) {
108                                         eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
109                                 }
110                         };
111                 }
112
113                 private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) {
114                         return new IdentityProcessor() {
115                                 @Override
116                                 public void processIdentity(Identity identity) {
117                                         eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
118                                 }
119                         };
120                 }
121
122                 private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) {
123                         return new IdentityProcessor() {
124                                 @Override
125                                 public void processIdentity(Identity identity) {
126                                         eventBus.post(new IdentityAddedEvent(ownIdentity, identity));
127                                 }
128                         };
129                 }
130
131         }
132
133 }