X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityChangeDetector.java;h=b2982e83c8d8c08b2eda0157f91b61021678df2f;hb=a9cff8f9aaba8930338b17f73f7c2257a1cc1530;hp=9703891990ed0d4b841fed8177fab5277232b43a;hpb=76ed638264e531a26e35647d13702db865a52321;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java index 9703891..b2982e8 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java @@ -1,5 +1,5 @@ /* - * Sone - IdentityChangeDetector.java - Copyright © 2013–2015 David Roden + * 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 @@ -17,97 +17,81 @@ 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.*; +import java.util.Map.*; +import java.util.function.*; +import java.util.stream.*; -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; +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. - * - * @author David ‘Bombe’ Roden */ public class IdentityChangeDetector { private final Map oldIdentities; - private Optional onNewIdentity = absent(); - private Optional onRemovedIdentity = absent(); - private Optional onChangedIdentity = absent(); - private Optional onUnchangedIdentity = absent(); + 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 = fromNullable(onNewIdentity); + this.onNewIdentity = onNewIdentity; } public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) { - this.onRemovedIdentity = fromNullable(onRemovedIdentity); + this.onRemovedIdentity = onRemovedIdentity; } public void onChangedIdentity(IdentityProcessor onChangedIdentity) { - this.onChangedIdentity = fromNullable(onChangedIdentity); + this.onChangedIdentity = onChangedIdentity; } public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) { - this.onUnchangedIdentity = fromNullable(onUnchangedIdentity); + this.onUnchangedIdentity = onUnchangedIdentity; } public void detectChanges(final Collection 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)))); + 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(FluentIterable newIdentities) { + private void notifyForNewIdentities(Iterable newIdentities) { notify(onNewIdentity, newIdentities); } - private void notifyForChangedIdentities(FluentIterable identities) { + private void notifyForChangedIdentities(Iterable identities) { notify(onChangedIdentity, identities); } - private void notifyForUnchangedIdentities(FluentIterable identities) { + private void notifyForUnchangedIdentities(Iterable identities) { notify(onUnchangedIdentity, identities); } - private void notify(Optional identityProcessor, Iterable identities) { - if (!identityProcessor.isPresent()) { + private void notify(IdentityProcessor identityProcessor, Iterable identities) { + if (identityProcessor == null) { return; } for (Identity identity : identities) { - identityProcessor.get().processIdentity(identity); + identityProcessor.processIdentity(identity); } } private static Predicate hasChanged(final Map oldIdentities) { - return new Predicate() { - @Override - public boolean apply(Identity identity) { - return (identity == null) ? false : identityHasChanged(oldIdentities.get(identity.getId()), identity); - } - }; + return identity -> (identity != null) && identityHasChanged(oldIdentities.get(identity.getId()), identity); } private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) { @@ -119,68 +103,43 @@ public class IdentityChangeDetector { } private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) { - return from(TO_CONTEXTS.apply(newIdentity)).anyMatch(notAContextOf(oldIdentity)); + return newIdentity.getContexts().stream().anyMatch(notAContextOf(oldIdentity)); } private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) { - return from(TO_CONTEXTS.apply(oldIdentity)).anyMatch(notAContextOf(newIdentity)); + return oldIdentity.getContexts().stream().anyMatch(notAContextOf(newIdentity)); } private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) { - return from(TO_PROPERTIES.apply(newIdentity).entrySet()).anyMatch(notAPropertyOf(oldIdentity)); + return newIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(oldIdentity)); } private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) { - return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(notAPropertyOf(newIdentity)); + return oldIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(newIdentity)); } private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) { - return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(hasADifferentValueThanIn(newIdentity)); + return oldIdentity.getProperties().entrySet().stream().anyMatch(hasADifferentValueThanIn(newIdentity)); } private static Predicate containedIn(final Map identities) { - return new Predicate() { - @Override - public boolean apply(Identity identity) { - return (identity != null) && identities.containsKey(identity.getId()); - } - }; + return identity -> (identity != null) && identities.containsKey(identity.getId()); } private static Predicate notAContextOf(final Identity identity) { - return new Predicate() { - @Override - public boolean apply(String context) { - return (identity == null) ? false : !identity.getContexts().contains(context); - } - }; + return context -> (identity != null) && !identity.getContexts().contains(context); } private static Predicate notContainedIn(final Collection newIdentities) { - return new Predicate() { - @Override - public boolean apply(Identity identity) { - return (identity == null) ? false : !newIdentities.contains(identity); - } - }; + return identity -> (identity != null) && !newIdentities.contains(identity); } private static Predicate> notAPropertyOf(final Identity identity) { - return new Predicate>() { - @Override - public boolean apply(Entry property) { - return (property == null) ? false : !identity.getProperties().containsKey(property.getKey()); - } - }; + return property -> (property != null) && !identity.getProperties().containsKey(property.getKey()); } private static Predicate> hasADifferentValueThanIn(final Identity newIdentity) { - return new Predicate>() { - @Override - public boolean apply(Entry property) { - return (property == null) ? false : !newIdentity.getProperty(property.getKey()).equals(property.getValue()); - } - }; + return property -> (property != null) && !newIdentity.getProperty(property.getKey()).equals(property.getValue()); } private static Map convertToMap(Collection identities) {