Add class that compares two lists of identities.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 6 Nov 2013 21:38:45 +0000 (22:38 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:54 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/freenet/wot/Identity.java
src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java [new file with mode: 0644]

index bc594f8..ff87fcd 100644 (file)
 package net.pterodactylus.sone.freenet.wot;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
+import com.google.common.base.Function;
+
 /**
  * Interface for web of trust identities, defining all functions that can be
  * performed on an identity. An identity is only a container for identity data
@@ -30,6 +34,20 @@ import java.util.Set;
  */
 public interface Identity {
 
+       public static final Function<Identity, Set<String>> TO_CONTEXTS = new Function<Identity, Set<String>>() {
+               @Override
+               public Set<String> apply(Identity identity) {
+                       return (identity == null) ? Collections.<String>emptySet() : identity.getContexts();
+               }
+       };
+
+       public static final Function<Identity, Collection<Map.Entry<String, String>>> TO_PROPERTIES = new Function<Identity, Collection<Entry<String, String>>>() {
+               @Override
+               public Collection<Entry<String, String>> apply(Identity input) {
+                       return (input == null) ? Collections.<Map.Entry<String, String>>emptySet() : input.getProperties().entrySet();
+               }
+       };
+
        /**
         * Returns the ID of the identity.
         *
diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java
new file mode 100644 (file)
index 0000000..3a0cb4b
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Sone - IdentityChangeDetector.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 static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Predicates.not;
+import static com.google.common.collect.FluentIterable.from;
+import static net.pterodactylus.sone.freenet.wot.Identity.TO_CONTEXTS;
+import static net.pterodactylus.sone.freenet.wot.Identity.TO_PROPERTIES;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Detects changes between two lists of {@link Identity}s. The detector can find
+ * added and removed identities, and for identities that exist in both list
+ * their contexts and properties are checked for added, removed, or (in case of
+ * properties) changed values.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityChangeDetector {
+
+       private final Map<String, Identity> oldIdentities;
+       private Optional<IdentityProcessor> onNewIdentity = absent();
+       private Optional<IdentityProcessor> onRemovedIdentity = absent();
+       private Optional<IdentityProcessor> onChangedIdentity = absent();
+       private Optional<IdentityProcessor> onUnchangedIdentity = absent();
+
+       public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
+               this.oldIdentities = convertToMap(oldIdentities);
+       }
+
+       public void onNewIdentity(IdentityProcessor onNewIdentity) {
+               this.onNewIdentity = fromNullable(onNewIdentity);
+       }
+
+       public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
+               this.onRemovedIdentity = fromNullable(onRemovedIdentity);
+       }
+
+       public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
+               this.onChangedIdentity = fromNullable(onChangedIdentity);
+       }
+
+       public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
+               this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
+       }
+
+       public void detectChanges(final Collection<? extends Identity> newIdentities) {
+               notifyForRemovedIdentities(from(oldIdentities.values()).filter(notContainedIn(newIdentities)));
+               notifyForNewIdentities(from(newIdentities).filter(notContainedIn(oldIdentities.values())));
+               notifyForChangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)));
+               notifyForUnchangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(not(hasChanged(oldIdentities))));
+       }
+
+       private void notifyForRemovedIdentities(Iterable<Identity> identities) {
+               notify(onRemovedIdentity, identities);
+       }
+
+       private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
+               notify(onNewIdentity, newIdentities);
+       }
+
+       private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
+               notify(onChangedIdentity, identities);
+       }
+
+       private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
+               notify(onUnchangedIdentity, identities);
+       }
+
+       private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
+               if (!identityProcessor.isPresent()) {
+                       return;
+               }
+               for (Identity identity : identities) {
+                       identityProcessor.get().processIdentity(identity);
+               }
+       }
+
+       private static Predicate<Identity> hasChanged(final Map<String, Identity> oldIdentities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return (identity == null) ? false : identityHasChanged(oldIdentities.get(identity.getId()), identity);
+                       }
+               };
+       }
+
+       private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
+               return identityHasNewContexts(oldIdentity, newIdentity)
+                               || identityHasRemovedContexts(oldIdentity, newIdentity)
+                               || identityHasNewProperties(oldIdentity, newIdentity)
+                               || identityHasRemovedProperties(oldIdentity, newIdentity)
+                               || identityHasChangedProperties(oldIdentity, newIdentity);
+       }
+
+       private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_CONTEXTS.apply(newIdentity)).anyMatch(notAContextOf(oldIdentity));
+       }
+
+       private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_CONTEXTS.apply(oldIdentity)).anyMatch(notAContextOf(newIdentity));
+       }
+
+       private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(newIdentity)).anyMatch(notAPropertyOf(oldIdentity));
+       }
+
+       private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(oldIdentity)).anyMatch(notAPropertyOf(newIdentity));
+       }
+
+       private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
+               return from(TO_PROPERTIES.apply(oldIdentity)).anyMatch(hasADifferentValueThanIn(newIdentity));
+       }
+
+       private static Predicate<Identity> containedIn(final Map<String, Identity> identities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return identities.containsKey(identity.getId());
+                       }
+               };
+       }
+
+       private static Predicate<String> notAContextOf(final Identity identity) {
+               return new Predicate<String>() {
+                       @Override
+                       public boolean apply(String context) {
+                               return (identity == null) ? false : !identity.getContexts().contains(context);
+                       }
+               };
+       }
+
+       private static Predicate<Identity> notContainedIn(final Collection<? extends Identity> newIdentities) {
+               return new Predicate<Identity>() {
+                       @Override
+                       public boolean apply(Identity identity) {
+                               return (identity == null) ? false : !newIdentities.contains(identity);
+                       }
+               };
+       }
+
+       private static Predicate<Entry<String, String>> notAPropertyOf(final Identity identity) {
+               return new Predicate<Entry<String, String>>() {
+                       @Override
+                       public boolean apply(Entry<String, String> property) {
+                               return (property == null) ? false : !identity.getProperties().containsKey(property.getKey());
+                       }
+               };
+       }
+
+       private static Predicate<Entry<String, String>> hasADifferentValueThanIn(final Identity newIdentity) {
+               return new Predicate<Entry<String, String>>() {
+                       @Override
+                       public boolean apply(Entry<String, String> property) {
+                               return (property == null) ? false : !newIdentity.getProperty(property.getKey()).equals(property.getValue());
+                       }
+               };
+       }
+
+       private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
+               ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
+               for (Identity identity : identities) {
+                       mapBuilder.put(identity.getId(), identity);
+               }
+               return mapBuilder.build();
+       }
+
+       public interface IdentityProcessor {
+
+               void processIdentity(Identity identity);
+
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.java
new file mode 100644 (file)
index 0000000..d306ace
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Sone - IdentityChangeDetectorTest.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 static com.google.common.collect.Lists.newArrayList;
+import static java.util.Arrays.asList;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+
+import java.util.Collection;
+import java.util.Map;
+
+import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdentityChangeDetector}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdentityChangeDetectorTest {
+
+       private final IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(createOldIdentities());
+       private final Collection<Identity> newIdentities = newArrayList();
+       private final Collection<Identity> removedIdentities = newArrayList();
+       private final Collection<Identity> changedIdentities = newArrayList();
+       private final Collection<Identity> unchangedIdentities = newArrayList();
+
+       @Before
+       public void setup() {
+               identityChangeDetector.onNewIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               newIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onRemovedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               removedIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onChangedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               changedIdentities.add(identity);
+                       }
+               });
+               identityChangeDetector.onUnchangedIdentity(new IdentityProcessor() {
+                       @Override
+                       public void processIdentity(Identity identity) {
+                               unchangedIdentities.add(identity);
+                       }
+               });
+       }
+
+       @Test
+       public void noDifferencesAreDetectedWhenSendingTheOldIdentitiesAgain() {
+               identityChangeDetector.detectChanges(createOldIdentities());
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAnIdentityWasRemoved() {
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, containsInAnyOrder(createIdentity2()));
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAnIdentityWasAdded() {
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4()));
+               assertThat(newIdentities, containsInAnyOrder(createIdentity4()));
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, empty());
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAContextWasRemoved() {
+               Identity identity2 = createIdentity2();
+               identity2.removeContext("Context C");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity2));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAContextWasAdded() {
+               Identity identity2 = createIdentity2();
+               identity2.addContext("Context C1");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), identity2, createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity2));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasRemoved() {
+               Identity identity1 = createIdentity1();
+               identity1.removeProperty("Key A");
+               identityChangeDetector.detectChanges(asList(identity1, createIdentity2(), createIdentity3()));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity1));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasAdded() {
+               Identity identity3 = createIdentity3();
+               identity3.setProperty("Key A", "Value A");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity3));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
+       }
+
+       @Test
+       public void detectThatAPropertyWasChanged() {
+               Identity identity3 = createIdentity3();
+               identity3.setProperty("Key E", "Value F");
+               identityChangeDetector.detectChanges(asList(createIdentity1(), createIdentity2(), identity3));
+               assertThat(newIdentities, empty());
+               assertThat(removedIdentities, empty());
+               assertThat(changedIdentities, containsInAnyOrder(identity3));
+               assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2()));
+       }
+
+       private static Collection<Identity> createOldIdentities() {
+               return asList(createIdentity1(), createIdentity2(), createIdentity3());
+       }
+
+       private static Identity createIdentity1() {
+               return createIdentity("Test1", asList("Context A", "Context B"), ImmutableMap.of("Key A", "Value A", "Key B", "Value B"));
+       }
+
+       private static Identity createIdentity2() {
+               return createIdentity("Test2", asList("Context C", "Context D"), ImmutableMap.of("Key C", "Value C", "Key D", "Value D"));
+       }
+
+       private static Identity createIdentity3() {
+               return createIdentity("Test3", asList("Context E", "Context F"), ImmutableMap.of("Key E", "Value E", "Key F", "Value F"));
+       }
+
+       private static Identity createIdentity4() {
+               return createIdentity("Test4", asList("Context G", "Context H"), ImmutableMap.of("Key G", "Value G", "Key H", "Value H"));
+       }
+
+       private static Identity createIdentity(String id, Collection<String> contexts, Map<String, String> properties) {
+               DefaultIdentity identity = new DefaultIdentity(id, id, id);
+               identity.setContexts(contexts);
+               identity.setProperties(properties);
+               return identity;
+       }
+
+}