--- /dev/null
+/*
+ * Sone - IdentityChangeEventSender.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.freenet.wot;
+
+import java.util.Collection;
+import java.util.Map;
+
+import net.pterodactylus.freenet.wot.IdentityChangeDetector.IdentityProcessor;
+import net.pterodactylus.freenet.wot.event.IdentityAddedEvent;
+import net.pterodactylus.freenet.wot.event.IdentityRemovedEvent;
+import net.pterodactylus.freenet.wot.event.IdentityUpdatedEvent;
+import net.pterodactylus.freenet.wot.event.OwnIdentityAddedEvent;
+import net.pterodactylus.freenet.wot.event.OwnIdentityRemovedEvent;
+
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Detects changes in {@link Identity}s trusted my multiple {@link OwnIdentity}s.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see IdentityChangeDetector
+ */
+public class IdentityChangeEventSender {
+
+ private final EventBus eventBus;
+ private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
+
+ public IdentityChangeEventSender(EventBus eventBus, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
+ this.eventBus = eventBus;
+ this.oldIdentities = oldIdentities;
+ }
+
+ public void detectChanges(Map<OwnIdentity, Collection<Identity>> identities) {
+ IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet());
+ identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities));
+ identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities));
+ identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(identities, oldIdentities));
+ identityChangeDetector.detectChanges(identities.keySet());
+ }
+
+ private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities) {
+ return identity -> {
+ eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity));
+ for (Identity newIdentity : newIdentities.get(identity)) {
+ eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity));
+ }
+ };
+ }
+
+ private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(Map<OwnIdentity, Collection<Identity>> oldIdentities) {
+ return identity -> {
+ eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity));
+ for (Identity removedIdentity : oldIdentities.get(identity)) {
+ eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity));
+ }
+ };
+ }
+
+ private IdentityProcessor detectChangesInTrustedIdentities(Map<OwnIdentity, Collection<Identity>> newIdentities, Map<OwnIdentity, Collection<Identity>> oldIdentities) {
+ return new DefaultIdentityProcessor(oldIdentities, newIdentities);
+ }
+
+ private class DefaultIdentityProcessor implements IdentityProcessor {
+
+ private final Map<OwnIdentity, Collection<Identity>> oldIdentities;
+ private final Map<OwnIdentity, Collection<Identity>> newIdentities;
+
+ public DefaultIdentityProcessor(Map<OwnIdentity, Collection<Identity>> oldIdentities, Map<OwnIdentity, Collection<Identity>> newIdentities) {
+ this.oldIdentities = oldIdentities;
+ this.newIdentities = newIdentities;
+ }
+
+ @Override
+ public void processIdentity(Identity ownIdentity) {
+ IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get(ownIdentity));
+ identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity));
+ identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity));
+ identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity));
+ identityChangeDetector.detectChanges(newIdentities.get(ownIdentity));
+ }
+
+ private IdentityProcessor notifyForChangedIdentities(OwnIdentity ownIdentity) {
+ return identity -> eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity));
+ }
+
+ private IdentityProcessor notifyForRemovedIdentities(OwnIdentity ownIdentity) {
+ return identity -> eventBus.post(new IdentityRemovedEvent(ownIdentity, identity));
+ }
+
+ private IdentityProcessor notifyForAddedIdentities(OwnIdentity ownIdentity) {
+ return identity -> eventBus.post(new IdentityAddedEvent(ownIdentity, identity));
+ }
+
+ }
+
+}