From: David ‘Bombe’ Roden Date: Fri, 16 Sep 2011 09:11:33 +0000 (+0200) Subject: Move identity change detection into its own method. X-Git-Tag: 0.0.3^2~4 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ab09aee8c42db059abbb568f5e2a3047f6fc3002;p=WoTNS.git Move identity change detection into its own method. --- diff --git a/src/main/java/net/pterodactylus/wotns/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/wotns/freenet/wot/IdentityManager.java index 1411ab3..3086f27 100644 --- a/src/main/java/net/pterodactylus/wotns/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/wotns/freenet/wot/IdentityManager.java @@ -17,6 +17,7 @@ package net.pterodactylus.wotns.freenet.wot; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -67,7 +68,7 @@ public class IdentityManager extends AbstractService { private Map currentOwnIdentities = new HashMap(); /** The currently trusted identities. */ - private Map> currentTrustedIdentities = new HashMap>(); + private Map> currentTrustedIdentities = new HashMap>(); /** * Creates a new identity manager. @@ -192,7 +193,6 @@ public class IdentityManager extends AbstractService { */ @Override protected void serviceRun() { - Map> oldIdentities = Collections.emptyMap(); while (!shouldStop()) { Map> currentIdentities = new HashMap>(); @SuppressWarnings("hiding") @@ -241,75 +241,7 @@ public class IdentityManager extends AbstractService { /* now check for changes in remote identities. */ for (OwnIdentity ownIdentity : currentOwnIdentities.values()) { - - /* find new identities. */ - for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) { - if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) { - identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity); - } - } - - /* find removed identities. */ - if (oldIdentities.containsKey(ownIdentity)) { - for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) { - if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) { - identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity); - } - } - - /* check for changes in the contexts. */ - for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) { - if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) { - continue; - } - Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId()); - Set oldContexts = oldIdentity.getContexts(); - Set newContexts = newIdentity.getContexts(); - if (oldContexts.size() != newContexts.size()) { - identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); - continue; - } - for (String oldContext : oldContexts) { - if (!newContexts.contains(oldContext)) { - identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); - break; - } - } - } - - /* check for changes in the properties. */ - for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) { - if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) { - continue; - } - Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId()); - Map oldProperties = oldIdentity.getProperties(); - Map newProperties = newIdentity.getProperties(); - if (oldProperties.size() != newProperties.size()) { - identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); - continue; - } - for (Entry oldProperty : oldProperties.entrySet()) { - if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) { - identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); - break; - } - } - } - } - } - - /* remember the current set of identities. */ - oldIdentities = currentIdentities; - synchronized (syncObject) { - currentTrustedIdentities.clear(); - for (Entry> entry : currentIdentities.entrySet()) { - Set identities = new HashSet(); - currentTrustedIdentities.put(entry.getKey(), identities); - for (Identity identity : entry.getValue().values()) { - identities.add(identity); - } - } + checkTrustedIdentities(ownIdentity, currentIdentities.get(ownIdentity)); } } @@ -351,4 +283,81 @@ public class IdentityManager extends AbstractService { } } + /** + * Checks the given identities for changes since the last check. + * + * @param ownIdentity + * The own identity trusting the given identities + * @param trustedIdentities + * The trusted identities + */ + private void checkTrustedIdentities(OwnIdentity ownIdentity, Map trustedIdentities) { + + @SuppressWarnings("hiding") + Map currentTrustedIdentities = new HashMap(); + synchronized (syncObject) { + if (this.currentTrustedIdentities.containsKey(ownIdentity)) { + for (Identity identity : this.currentTrustedIdentities.get(ownIdentity)) { + currentTrustedIdentities.put(identity.getId(), identity); + } + } + } + + /* find new identities. */ + for (Identity currentIdentity : trustedIdentities.values()) { + if (!currentTrustedIdentities.containsKey(currentIdentity.getId())) { + identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity); + } + } + + /* find removed identities. */ + for (Identity oldIdentity : currentTrustedIdentities.values()) { + if (!trustedIdentities.containsKey(oldIdentity.getId())) { + identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity); + } + } + + /* check for changes in the contexts. */ + for (Identity oldIdentity : currentTrustedIdentities.values()) { + if (!trustedIdentities.containsKey(oldIdentity.getId())) { + continue; + } + Identity newIdentity = trustedIdentities.get(oldIdentity.getId()); + Set oldContexts = oldIdentity.getContexts(); + Set newContexts = newIdentity.getContexts(); + if (oldContexts.size() != newContexts.size()) { + identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); + continue; + } + boolean changed = false; + for (String oldContext : oldContexts) { + if (!newContexts.contains(oldContext)) { + identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); + changed = true; + break; + } + } + if (changed) { + continue; + } + Map oldProperties = oldIdentity.getProperties(); + Map newProperties = newIdentity.getProperties(); + if (oldProperties.size() != newProperties.size()) { + identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); + continue; + } + for (Entry oldProperty : oldProperties.entrySet()) { + if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) { + identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity); + break; + } + } + } + + synchronized (syncObject) { + this.currentTrustedIdentities.put(ownIdentity, trustedIdentities.values()); + } + + } + }