X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityChangeDetector.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityChangeDetector.java;h=0000000000000000000000000000000000000000;hp=b2982e83c8d8c08b2eda0157f91b61021678df2f;hb=e46a8c717ccac5698162273275e4c74608a6365b;hpb=b56a4062442a92b2121f85a6aadd416be5eb03ac diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java deleted file mode 100644 index b2982e8..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Sone - IdentityChangeDetector.java - Copyright © 2013–2019 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 . - */ - -package net.pterodactylus.sone.freenet.wot; - -import java.util.*; -import java.util.Map.*; -import java.util.function.*; -import java.util.stream.*; - -import com.google.common.collect.*; - -/** - * 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. - */ -public class IdentityChangeDetector { - - private final Map oldIdentities; - private IdentityProcessor onNewIdentity; - private IdentityProcessor onRemovedIdentity; - private IdentityProcessor onChangedIdentity; - private IdentityProcessor onUnchangedIdentity; - - public IdentityChangeDetector(Collection oldIdentities) { - this.oldIdentities = convertToMap(oldIdentities); - } - - public void onNewIdentity(IdentityProcessor onNewIdentity) { - this.onNewIdentity = onNewIdentity; - } - - public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) { - this.onRemovedIdentity = onRemovedIdentity; - } - - public void onChangedIdentity(IdentityProcessor onChangedIdentity) { - this.onChangedIdentity = onChangedIdentity; - } - - public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) { - this.onUnchangedIdentity = onUnchangedIdentity; - } - - public void detectChanges(final Collection newIdentities) { - notifyForRemovedIdentities(oldIdentities.values().stream().filter(notContainedIn(newIdentities)).collect(Collectors.toList())); - notifyForNewIdentities(newIdentities.stream().filter(notContainedIn(oldIdentities.values())).collect(Collectors.toList())); - notifyForChangedIdentities(newIdentities.stream().filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)).collect(Collectors.toList())); - notifyForUnchangedIdentities(newIdentities.stream().filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities).negate()).collect(Collectors.toList())); - } - - private void notifyForRemovedIdentities(Iterable identities) { - notify(onRemovedIdentity, identities); - } - - private void notifyForNewIdentities(Iterable newIdentities) { - notify(onNewIdentity, newIdentities); - } - - private void notifyForChangedIdentities(Iterable identities) { - notify(onChangedIdentity, identities); - } - - private void notifyForUnchangedIdentities(Iterable identities) { - notify(onUnchangedIdentity, identities); - } - - private void notify(IdentityProcessor identityProcessor, Iterable identities) { - if (identityProcessor == null) { - return; - } - for (Identity identity : identities) { - identityProcessor.processIdentity(identity); - } - } - - private static Predicate hasChanged(final Map oldIdentities) { - return identity -> (identity != null) && 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 newIdentity.getContexts().stream().anyMatch(notAContextOf(oldIdentity)); - } - - private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) { - return oldIdentity.getContexts().stream().anyMatch(notAContextOf(newIdentity)); - } - - private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) { - return newIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(oldIdentity)); - } - - private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) { - return oldIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(newIdentity)); - } - - private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) { - return oldIdentity.getProperties().entrySet().stream().anyMatch(hasADifferentValueThanIn(newIdentity)); - } - - private static Predicate containedIn(final Map identities) { - return identity -> (identity != null) && identities.containsKey(identity.getId()); - } - - private static Predicate notAContextOf(final Identity identity) { - return context -> (identity != null) && !identity.getContexts().contains(context); - } - - private static Predicate notContainedIn(final Collection newIdentities) { - return identity -> (identity != null) && !newIdentities.contains(identity); - } - - private static Predicate> notAPropertyOf(final Identity identity) { - return property -> (property != null) && !identity.getProperties().containsKey(property.getKey()); - } - - private static Predicate> hasADifferentValueThanIn(final Identity newIdentity) { - return property -> (property != null) && !newIdentity.getProperty(property.getKey()).equals(property.getValue()); - } - - private static Map convertToMap(Collection identities) { - ImmutableMap.Builder mapBuilder = ImmutableMap.builder(); - for (Identity identity : identities) { - mapBuilder.put(identity.getId(), identity); - } - return mapBuilder.build(); - } - - public interface IdentityProcessor { - - void processIdentity(Identity identity); - - } - -}