2 * Sone - IdentityChangeEventSender.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.freenet.wot;
20 import java.util.Collection;
23 import net.pterodactylus.freenet.wot.IdentityChangeDetector.IdentityProcessor;
24 import net.pterodactylus.freenet.wot.event.IdentityAddedEvent;
25 import net.pterodactylus.freenet.wot.event.IdentityRemovedEvent;
26 import net.pterodactylus.freenet.wot.event.IdentityUpdatedEvent;
27 import net.pterodactylus.freenet.wot.event.OwnIdentityAddedEvent;
28 import net.pterodactylus.freenet.wot.event.OwnIdentityRemovedEvent;
30 import com.google.common.eventbus.EventBus;
33 * Detects changes in {@link Identity}s trusted my multiple {@link OwnIdentity}s.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 * @see IdentityChangeDetector
38 public class IdentityChangeEventSender {
40 private final EventBus eventBus;
41 private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
43 public IdentityChangeEventSender(EventBus eventBus, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
44 this.eventBus = eventBus;
45 this.oldIdentities = oldIdentities;
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());
56 private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities) {
58 eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity));
59 for (Identity newIdentity : newIdentities.get(identity)) {
60 eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity));
65 private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(Map<OwnIdentity, Collection<Identity>> oldIdentities) {
67 eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity));
68 for (Identity removedIdentity : oldIdentities.get(identity)) {
69 eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity));
74 private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
75 return new DefaultIdentityProcessor(oldIdentities, newIdentities);
78 private class DefaultIdentityProcessor implements IdentityProcessor {
80 private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
81 private final Map<OwnIdentity, Collection<Identity>> newIdentities;
83 public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
84 this.oldIdentities = oldIdentities;
85 this.newIdentities = newIdentities;
89 public void processIdentity(Identity ownIdentity) {
90 IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get(ownIdentity));
91 identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity));
92 identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity));
93 identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity));
94 identityChangeDetector.detectChanges(newIdentities.get(ownIdentity));
97 private IdentityProcessor notifyForChangedIdentities(OwnIdentity ownIdentity) {
98 return identity -> eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
101 private IdentityProcessor notifyForRemovedIdentities(OwnIdentity ownIdentity) {
102 return identity -> eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
105 private IdentityProcessor notifyForAddedIdentities(OwnIdentity ownIdentity) {
106 return identity -> eventBus.post(new IdentityAddedEvent(ownIdentity, identity));