Move more of identity change detection into an easily testable class.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityManager.java
index 2933483..3b85e0d 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot;
 
+import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.collect.HashMultimap.create;
 
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
 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;
 
@@ -54,24 +48,17 @@ import com.google.inject.name.Named;
  */
 public class IdentityManager extends AbstractService {
 
-       /** Object used for synchronization. */
-       @SuppressWarnings("hiding")
-       private final Object syncObject = new Object() {
-               /* inner class for better lock names. */
-       };
-
        /** The logger. */
        private static final Logger logger = Logging.getLogger(IdentityManager.class);
 
        /** The event bus. */
        private final EventBus eventBus;
 
+       private final IdentityLoader identityLoader;
+
        /** The Web of Trust connector. */
        private final WebOfTrustConnector webOfTrustConnector;
 
-       /** The context to filter for. */
-       private final String context;
-
        /** The currently known own identities. */
        /* synchronize access on syncObject. */
        private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
@@ -91,7 +78,7 @@ public class IdentityManager extends AbstractService {
                super("Sone Identity Manager", false);
                this.eventBus = eventBus;
                this.webOfTrustConnector = webOfTrustConnector;
-               this.context = context;
+               this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context));
        }
 
        //
@@ -121,7 +108,7 @@ public class IdentityManager extends AbstractService {
         * @return All own identities
         */
        public Set<OwnIdentity> getAllOwnIdentities() {
-               synchronized (syncObject) {
+               synchronized (currentOwnIdentities) {
                        return new HashSet<OwnIdentity>(currentOwnIdentities);
                }
        }
@@ -136,15 +123,16 @@ public class IdentityManager extends AbstractService {
 
                while (!shouldStop()) {
                        try {
-                               Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
-                               Multimap<OwnIdentity, Identity> currentIdentities = loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
+                               Multimap<OwnIdentity, Identity> currentIdentities = identityLoader.loadIdentities();
+
+                               IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
+                               identityChangeEventSender.detectChanges(currentIdentities);
 
-                               detectChangesInIdentities(currentOwnIdentities, currentIdentities, oldIdentities);
                                oldIdentities = currentIdentities;
 
-                               synchronized (syncObject) {
-                                       this.currentOwnIdentities.clear();
-                                       this.currentOwnIdentities.addAll(currentOwnIdentities);
+                               synchronized (currentOwnIdentities) {
+                                       currentOwnIdentities.clear();
+                                       currentOwnIdentities.addAll(currentIdentities.keySet());
                                }
                        } catch (WebOfTrustException wote1) {
                                logger.log(Level.WARNING, "WoT has disappeared!", wote1);
@@ -155,105 +143,4 @@ public class IdentityManager extends AbstractService {
                }
        }
 
-       private void detectChangesInIdentities(Collection<OwnIdentity> currentOwnIdentities, 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(currentOwnIdentities);
-       }
-
-       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 Multimap<OwnIdentity, Identity> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
-               Multimap<OwnIdentity, Identity> currentIdentities = create();
-
-               for (OwnIdentity ownIdentity : ownIdentities) {
-                       if ((context != null) && !ownIdentity.hasContext(context)) {
-                               continue;
-                       }
-
-                       logger.finer(String.format("Getting trusted identities for %s...", ownIdentity.getId()));
-                       Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
-                       logger.finest(String.format("Got %d trusted identities.", trustedIdentities.size()));
-                       currentIdentities.putAll(ownIdentity, trustedIdentities);
-               }
-
-               return currentIdentities;
-       }
-
-       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));
-                               }
-                       };
-               }
-
-       }
-
 }