Move more of identity change detection into an easily testable class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Nov 2013 21:41:16 +0000 (22:41 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:07 +0000 (22:26 +0100)
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.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 (file)
index 0000000..4f38f8e
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see IdentityChangeDetector
+ */
+public class IdentityChangeEventSender {
+
+       private final EventBus eventBus;
+       private final Multimap<OwnIdentity, Identity> oldIdentities;
+
+       public IdentityChangeEventSender(EventBus eventBus, Multimap<OwnIdentity, Identity> oldIdentities) {
+               this.eventBus = eventBus;
+               this.oldIdentities = oldIdentities;
+       }
+
+       public void detectChanges(Multimap<OwnIdentity, 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(final Multimap<OwnIdentity, Identity> 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<OwnIdentity, Identity> 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<OwnIdentity, Identity> newIdentities, Multimap<OwnIdentity, Identity> oldIdentities) {
+               return new DefaultIdentityProcessor(oldIdentities, newIdentities);
+       }
+
+       private class DefaultIdentityProcessor implements IdentityProcessor {
+
+               private final Multimap<OwnIdentity, Identity> oldIdentities;
+               private final Multimap<OwnIdentity, Identity> newIdentities;
+
+               public DefaultIdentityProcessor(Multimap<OwnIdentity, Identity> oldIdentities, Multimap<OwnIdentity, Identity> 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));
+                               }
+                       };
+               }
+
+       }
+
+}
index 9817038..3b85e0d 100644 (file)
@@ -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<OwnIdentity, Identity> 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<OwnIdentity, Identity> newIdentities, Multimap<OwnIdentity, Identity> 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<OwnIdentity, Identity> newIdentities, Multimap<OwnIdentity, Identity> oldIdentities) {
-               return new DefaultIdentityProcessor(oldIdentities, newIdentities);
-       }
-
-       private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap<OwnIdentity, Identity> 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<OwnIdentity, Identity> 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<OwnIdentity, Identity> oldIdentities;
-               private final Multimap<OwnIdentity, Identity> newIdentities;
-
-               public DefaultIdentityProcessor(Multimap<OwnIdentity, Identity> oldIdentities, Multimap<OwnIdentity, Identity> 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));
-                               }
-                       };
-               }
-
-       }
-
 }