From dfdb5045fcdb13c23782375f3990f0546ff71e6b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Nov 2013 22:41:16 +0100 Subject: [PATCH] Move more of identity change detection into an easily testable class. --- .../freenet/wot/IdentityChangeEventSender.java | 131 +++++++++++++++++++++ .../sone/freenet/wot/IdentityManager.java | 94 +-------------- 2 files changed, 134 insertions(+), 91 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java new file mode 100644 index 0000000..4f38f8e --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java @@ -0,0 +1,131 @@ +/* + * 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 . + */ + +package net.pterodactylus.sone.freenet.wot; + +import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor; +import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent; +import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; +import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; + +import com.google.common.collect.Multimap; +import com.google.common.eventbus.EventBus; + +/** + * Detects changes in {@link Identity}s trusted my multiple {@link + * OwnIdentity}s. + * + * @author David ‘Bombe’ Roden + * @see IdentityChangeDetector + */ +public class IdentityChangeEventSender { + + private final EventBus eventBus; + private final Multimap oldIdentities; + + public IdentityChangeEventSender(EventBus eventBus, Multimap oldIdentities) { + this.eventBus = eventBus; + this.oldIdentities = oldIdentities; + } + + public void detectChanges(Multimap 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(final Multimap newIdentities) { + return new IdentityProcessor() { + @Override + public void processIdentity(Identity identity) { + eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity)); + for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) { + eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity)); + } + } + }; + } + + private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap oldIdentities) { + return new IdentityProcessor() { + @Override + public void processIdentity(Identity identity) { + eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity)); + for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) { + eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity)); + } + } + }; + } + + private IdentityProcessor detectChangesInTrustedIdentities(Multimap newIdentities, Multimap oldIdentities) { + return new DefaultIdentityProcessor(oldIdentities, newIdentities); + } + + private class DefaultIdentityProcessor implements IdentityProcessor { + + private final Multimap oldIdentities; + private final Multimap newIdentities; + + public DefaultIdentityProcessor(Multimap oldIdentities, Multimap newIdentities) { + this.oldIdentities = oldIdentities; + this.newIdentities = newIdentities; + } + + @Override + public void processIdentity(Identity ownIdentity) { + IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity)); + identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity)); + identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity)); + identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity)); + identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity)); + } + + private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) { + return new IdentityProcessor() { + @Override + public void processIdentity(Identity identity) { + eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity)); + } + }; + } + + private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) { + return new IdentityProcessor() { + @Override + public void processIdentity(Identity identity) { + eventBus.post(new IdentityRemovedEvent(ownIdentity, identity)); + } + }; + } + + private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) { + return new IdentityProcessor() { + @Override + public void processIdentity(Identity identity) { + eventBus.post(new IdentityAddedEvent(ownIdentity, identity)); + } + }; + } + + } + +} diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java index 9817038..3b85e0d 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -26,12 +26,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.freenet.plugin.PluginException; -import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor; -import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; @@ -131,7 +125,9 @@ public class IdentityManager extends AbstractService { try { Multimap currentIdentities = identityLoader.loadIdentities(); - detectChangesInIdentities(currentIdentities, oldIdentities); + IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities); + identityChangeEventSender.detectChanges(currentIdentities); + oldIdentities = currentIdentities; synchronized (currentOwnIdentities) { @@ -147,88 +143,4 @@ public class IdentityManager extends AbstractService { } } - private void detectChangesInIdentities(Multimap newIdentities, Multimap oldIdentities) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(getAllOwnIdentities()); - identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(newIdentities)); - identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities)); - identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(newIdentities, oldIdentities)); - identityChangeDetector.detectChanges(newIdentities.keySet()); - } - - private IdentityProcessor detectChangesInTrustedIdentities(Multimap newIdentities, Multimap oldIdentities) { - return new DefaultIdentityProcessor(oldIdentities, newIdentities); - } - - private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap oldIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity)); - for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity)); - } - } - }; - } - - private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Multimap newIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity)); - for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity)); - } - } - }; - } - - private class DefaultIdentityProcessor implements IdentityProcessor { - - private final Multimap oldIdentities; - private final Multimap newIdentities; - - public DefaultIdentityProcessor(Multimap oldIdentities, Multimap newIdentities) { - this.oldIdentities = oldIdentities; - this.newIdentities = newIdentities; - } - - @Override - public void processIdentity(Identity ownIdentity) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity)); - identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity)); - } - - private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityRemovedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityAddedEvent(ownIdentity, identity)); - } - }; - } - - } - } -- 2.7.4