package net.pterodactylus.wotns.freenet.wot;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
private Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
/** The currently trusted identities. */
- private Map<OwnIdentity, Set<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Set<Identity>>();
+ private Map<OwnIdentity, Collection<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
/**
* Creates a new identity manager.
*/
@Override
protected void serviceRun() {
- Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
while (!shouldStop()) {
Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
@SuppressWarnings("hiding")
/* 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<String> oldContexts = oldIdentity.getContexts();
- Set<String> 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<String, String> oldProperties = oldIdentity.getProperties();
- Map<String, String> newProperties = newIdentity.getProperties();
- if (oldProperties.size() != newProperties.size()) {
- identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
- continue;
- }
- for (Entry<String, String> 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<OwnIdentity, Map<String, Identity>> entry : currentIdentities.entrySet()) {
- Set<Identity> identities = new HashSet<Identity>();
- currentTrustedIdentities.put(entry.getKey(), identities);
- for (Identity identity : entry.getValue().values()) {
- identities.add(identity);
- }
- }
+ checkTrustedIdentities(ownIdentity, currentIdentities.get(ownIdentity));
}
}
}
}
+ /**
+ * 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<String, Identity> trustedIdentities) {
+
+ @SuppressWarnings("hiding")
+ Map<String, Identity> currentTrustedIdentities = new HashMap<String, Identity>();
+ 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<String> oldContexts = oldIdentity.getContexts();
+ Set<String> 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<String, String> oldProperties = oldIdentity.getProperties();
+ Map<String, String> newProperties = newIdentity.getProperties();
+ if (oldProperties.size() != newProperties.size()) {
+ identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
+ continue;
+ }
+ for (Entry<String, String> 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());
+ }
+
+ }
+
}