From e46a8c717ccac5698162273275e4c74608a6365b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 23 Sep 2019 19:06:53 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20change=20detectors=20wi?= =?utf8?q?th=20Kotlin=20versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/IdentityChangeDetector.java | 159 --------------------- .../sone/freenet/wot/IdentityChangeDetector.kt | 68 +++++++++ .../freenet/wot/IdentityChangeEventSender.java | 132 ----------------- .../sone/freenet/wot/IdentityChangeEventSender.kt | 63 ++++++++ .../sone/freenet/wot/IdentityChangeDetectorTest.kt | 112 +++++++-------- 5 files changed, 183 insertions(+), 351 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java create mode 100644 src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.kt delete mode 100644 src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.kt 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); - - } - -} diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.kt b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.kt new file mode 100644 index 0000000..deb35b1 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.kt @@ -0,0 +1,68 @@ +/* + * 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 + +/** + * Detects changes between two lists of [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. + */ +class IdentityChangeDetector(oldIdentities: Collection) { + + private val oldIdentities: Map = oldIdentities.associateBy { it.id } + var onNewIdentity: IdentityProcessor? = null + var onRemovedIdentity: IdentityProcessor? = null + var onChangedIdentity: IdentityProcessor? = null + var onUnchangedIdentity: IdentityProcessor? = null + + fun detectChanges(newIdentities: Collection) { + onRemovedIdentity.notify(oldIdentities.values.filter { it !in newIdentities }) + onNewIdentity.notify(newIdentities.filter { it !in oldIdentities.values }) + onChangedIdentity.notify(newIdentities.filter { it.id in oldIdentities }.filter { identityHasChanged(oldIdentities[it.id]!!, it) }) + onUnchangedIdentity.notify(newIdentities.filter { it.id in oldIdentities }.filterNot { identityHasChanged(oldIdentities[it.id]!!, it) }) + } + + private fun identityHasChanged(oldIdentity: Identity, newIdentity: Identity?) = + identityHasNewContexts(oldIdentity, newIdentity!!) + || identityHasRemovedContexts(oldIdentity, newIdentity) + || identityHasNewProperties(oldIdentity, newIdentity) + || identityHasRemovedProperties(oldIdentity, newIdentity) + || identityHasChangedProperties(oldIdentity, newIdentity) + + private fun identityHasNewContexts(oldIdentity: Identity, newIdentity: Identity) = + newIdentity.contexts.any { it !in oldIdentity.contexts } + + private fun identityHasRemovedContexts(oldIdentity: Identity, newIdentity: Identity) = + oldIdentity.contexts.any { it !in newIdentity.contexts } + + private fun identityHasNewProperties(oldIdentity: Identity, newIdentity: Identity) = + newIdentity.properties.keys.any { it !in oldIdentity.properties } + + private fun identityHasRemovedProperties(oldIdentity: Identity, newIdentity: Identity) = + oldIdentity.properties.keys.any { it !in newIdentity.properties } + + private fun identityHasChangedProperties(oldIdentity: Identity, newIdentity: Identity) = + oldIdentity.properties.entries.any { newIdentity.properties[it.key] != it.value } + +} + +typealias IdentityProcessor = (Identity) -> Unit + +private fun IdentityProcessor?.notify(identities: Iterable) = + this?.let { identities.forEach(this::invoke) } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java deleted file mode 100644 index fd57c38..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Sone - IdentityChangeEventSender.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.Collection; -import java.util.Map; - -import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor; -import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; - -import com.google.common.eventbus.EventBus; - -/** - * Detects changes in {@link Identity}s trusted my multiple {@link - * OwnIdentity}s. - * - * @see IdentityChangeDetector - */ -public class IdentityChangeEventSender { - - private final EventBus eventBus; - private final Map> oldIdentities; - - public IdentityChangeEventSender(EventBus eventBus, Map> oldIdentities) { - this.eventBus = eventBus; - this.oldIdentities = oldIdentities; - } - - public void detectChanges(Map> identities) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.keySet()); - identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(identities)); - identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities)); - identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(identities, oldIdentities)); - identityChangeDetector.detectChanges(identities.keySet()); - } - - private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Map> newIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity)); - for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity)); - } - } - }; - } - - private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Map> oldIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity)); - for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity)); - } - } - }; - } - - private IdentityProcessor detectChangesInTrustedIdentities(Map> newIdentities, Map> oldIdentities) { - return new DefaultIdentityProcessor(oldIdentities, newIdentities); - } - - private class DefaultIdentityProcessor implements IdentityProcessor { - - private final Map> oldIdentities; - private final Map> newIdentities; - - public DefaultIdentityProcessor(Map> oldIdentities, Map> newIdentities) { - this.oldIdentities = oldIdentities; - this.newIdentities = newIdentities; - } - - @Override - public void processIdentity(Identity ownIdentity) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity)); - identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity)); - } - - private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityRemovedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityAddedEvent(ownIdentity, identity)); - } - }; - } - - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.kt new file mode 100644 index 0000000..5ffffa3 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSender.kt @@ -0,0 +1,63 @@ +/* + * Sone - IdentityChangeEventSender.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 com.google.common.eventbus.* +import net.pterodactylus.sone.freenet.wot.event.* + +/** + * Detects changes in [Identity]s trusted by multiple [OwnIdentity]s. + * + * @see IdentityChangeDetector + */ +class IdentityChangeEventSender(private val eventBus: EventBus, private val oldIdentities: Map>) { + + fun detectChanges(identities: Map>) { + val identityChangeDetector = IdentityChangeDetector(oldIdentities.keys) + identityChangeDetector.onNewIdentity = addNewOwnIdentityAndItsTrustedIdentities(identities) + identityChangeDetector.onRemovedIdentity = removeOwnIdentityAndItsTrustedIdentities(oldIdentities) + identityChangeDetector.onUnchangedIdentity = detectChangesInTrustedIdentities(identities, oldIdentities) + identityChangeDetector.detectChanges(identities.keys) + } + + private fun addNewOwnIdentityAndItsTrustedIdentities(newIdentities: Map>) = + { identity: Identity -> + eventBus.post(OwnIdentityAddedEvent(identity as OwnIdentity)) + newIdentities[identity] + ?.map { IdentityAddedEvent(identity, it) } + ?.forEach(eventBus::post) ?: Unit + } + + private fun removeOwnIdentityAndItsTrustedIdentities(oldIdentities: Map>) = + { identity: Identity -> + eventBus.post(OwnIdentityRemovedEvent(identity as OwnIdentity)) + oldIdentities[identity] + ?.map { IdentityRemovedEvent(identity, it) } + ?.forEach(eventBus::post) ?: Unit + } + + private fun detectChangesInTrustedIdentities(newIdentities: Map>, oldIdentities: Map>) = + { ownIdentity: Identity -> + val identityChangeDetector = IdentityChangeDetector(oldIdentities[ownIdentity as OwnIdentity]!!) + identityChangeDetector.onNewIdentity = { eventBus.post(IdentityAddedEvent(ownIdentity, it)) } + identityChangeDetector.onRemovedIdentity = { eventBus.post(IdentityRemovedEvent(ownIdentity, it)) } + identityChangeDetector.onChangedIdentity = { eventBus.post(IdentityUpdatedEvent(ownIdentity, it)) } + identityChangeDetector.detectChanges(newIdentities[ownIdentity]!!) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt index aa1b7f0..ab9570d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeDetectorTest.kt @@ -17,13 +17,10 @@ package net.pterodactylus.sone.freenet.wot -import net.pterodactylus.sone.freenet.wot.Identities.createIdentity -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.containsInAnyOrder -import org.hamcrest.Matchers.empty - -import org.junit.Before -import org.junit.Test +import net.pterodactylus.sone.freenet.wot.Identities.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* /** * Unit test for [IdentityChangeDetector]. @@ -38,37 +35,37 @@ class IdentityChangeDetectorTest { @Before fun setup() { - identityChangeDetector.onNewIdentity { identity -> newIdentities.add(identity) } - identityChangeDetector.onRemovedIdentity { identity -> removedIdentities.add(identity) } - identityChangeDetector.onChangedIdentity { identity -> changedIdentities.add(identity) } - identityChangeDetector.onUnchangedIdentity { identity -> unchangedIdentities.add(identity) } + identityChangeDetector.onNewIdentity = { identity -> newIdentities.add(identity) } + identityChangeDetector.onRemovedIdentity = { identity -> removedIdentities.add(identity) } + identityChangeDetector.onChangedIdentity = { identity -> changedIdentities.add(identity) } + identityChangeDetector.onUnchangedIdentity = { identity -> unchangedIdentities.add(identity) } } @Test fun `no differences are detected when sending the old identities again`() { identityChangeDetector.detectChanges(createOldIdentities()) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, empty()) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, empty()) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) } @Test fun `detect that an identity was removed`() { identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3())) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, containsInAnyOrder(createIdentity2())) - assertThat>(changedIdentities, empty()) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, containsInAnyOrder(createIdentity2())) + assertThat(changedIdentities, empty()) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) } @Test fun `detect that an identity was added`() { identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())) - assertThat>(newIdentities, containsInAnyOrder(createIdentity4())) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, empty()) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) + assertThat(newIdentities, containsInAnyOrder(createIdentity4())) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, empty()) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2(), createIdentity3())) } @Test @@ -76,10 +73,10 @@ class IdentityChangeDetectorTest { val identity2 = createIdentity2() identity2.removeContext("Context C") identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3())) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, containsInAnyOrder(identity2)) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, containsInAnyOrder(identity2)) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) } @Test @@ -87,10 +84,10 @@ class IdentityChangeDetectorTest { val identity2 = createIdentity2() identity2.addContext("Context C1") identityChangeDetector.detectChanges(listOf(createIdentity1(), identity2, createIdentity3())) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, containsInAnyOrder(identity2)) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, containsInAnyOrder(identity2)) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity3())) } @Test @@ -98,10 +95,10 @@ class IdentityChangeDetectorTest { val identity1 = createIdentity1() identity1.removeProperty("Key A") identityChangeDetector.detectChanges(listOf(identity1, createIdentity2(), createIdentity3())) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, containsInAnyOrder(identity1)) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, containsInAnyOrder(identity1)) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity2(), createIdentity3())) } @Test @@ -109,10 +106,10 @@ class IdentityChangeDetectorTest { val identity3 = createIdentity3() identity3.setProperty("Key A", "Value A") identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3)) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, containsInAnyOrder(identity3)) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, containsInAnyOrder(identity3)) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) } @Test @@ -120,44 +117,39 @@ class IdentityChangeDetectorTest { val identity3 = createIdentity3() identity3.setProperty("Key E", "Value F") identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), identity3)) - assertThat>(newIdentities, empty()) - assertThat>(removedIdentities, empty()) - assertThat>(changedIdentities, containsInAnyOrder(identity3)) - assertThat>(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) + assertThat(newIdentities, empty()) + assertThat(removedIdentities, empty()) + assertThat(changedIdentities, containsInAnyOrder(identity3)) + assertThat(unchangedIdentities, containsInAnyOrder(createIdentity1(), createIdentity2())) } @Test fun `no removed identities are detected without an identity processor`() { - identityChangeDetector.onRemovedIdentity(null) + identityChangeDetector.onRemovedIdentity = null identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity3())) assertThat(removedIdentities, empty()) } @Test fun `no added identities are detected without an identity processor`() { - identityChangeDetector.onNewIdentity(null) + identityChangeDetector.onNewIdentity = null identityChangeDetector.detectChanges(listOf(createIdentity1(), createIdentity2(), createIdentity3(), createIdentity4())) assertThat(newIdentities, empty()) } - private fun createOldIdentities(): Collection { - return listOf(createIdentity1(), createIdentity2(), createIdentity3()) - } + private fun createOldIdentities() = + listOf(createIdentity1(), createIdentity2(), createIdentity3()) - private fun createIdentity1(): Identity { - return createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B")) - } + private fun createIdentity1() = + createIdentity("Test1", listOf("Context A", "Context B"), mapOf("Key A" to "Value A", "Key B" to "Value B")) - private fun createIdentity2(): Identity { - return createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D")) - } + private fun createIdentity2() = + createIdentity("Test2", listOf("Context C", "Context D"), mapOf("Key C" to "Value C", "Key D" to "Value D")) - private fun createIdentity3(): Identity { - return createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F")) - } + private fun createIdentity3() = + createIdentity("Test3", listOf("Context E", "Context F"), mapOf("Key E" to "Value E", "Key F" to "Value F")) - private fun createIdentity4(): Identity { - return createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H")) - } + private fun createIdentity4() = + createIdentity("Test4", listOf("Context G", "Context H"), mapOf("Key G" to "Value G", "Key H" to "Value H")) } -- 2.7.4