2 * Sone - IdentityChangeEventSender.java - Copyright © 2013–2019 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.sone.freenet.wot;
20 import java.util.Collection;
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;
30 import com.google.common.eventbus.EventBus;
33 * Detects changes in {@link Identity}s trusted my multiple {@link
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(final Map<OwnIdentity, Collection<Identity>> newIdentities) {
57 return new IdentityProcessor() {
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));
68 private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map<OwnIdentity, Collection<Identity>> oldIdentities) {
69 return new IdentityProcessor() {
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));
80 private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
81 return new DefaultIdentityProcessor(oldIdentities, newIdentities);
84 private class DefaultIdentityProcessor implements IdentityProcessor {
86 private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
87 private final Map<OwnIdentity, Collection<Identity>> newIdentities;
89 public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
90 this.oldIdentities = oldIdentities;
91 this.newIdentities = newIdentities;
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));
103 private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) {
104 return new IdentityProcessor() {
106 public void processIdentity(Identity identity) {
107 eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
112 private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) {
113 return new IdentityProcessor() {
115 public void processIdentity(Identity identity) {
116 eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
121 private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) {
122 return new IdentityProcessor() {
124 public void processIdentity(Identity identity) {
125 eventBus.post(new IdentityAddedEvent(ownIdentity, identity));