2 * Sone - IdentityChangeEventSender.java - Copyright © 2013–2019 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.freenet.wot.event.*
24 * Detects changes in [Identity]s trusted by multiple [OwnIdentity]s.
26 * @see IdentityChangeDetector
28 class IdentityChangeEventSender(private val eventBus: EventBus, private val oldIdentities: Map<OwnIdentity, Collection<Identity>>) {
30 fun detectChanges(identities: Map<OwnIdentity, Collection<Identity>>) {
31 val identityChangeDetector = IdentityChangeDetector(oldIdentities.keys)
32 identityChangeDetector.onNewIdentity = addNewOwnIdentityAndItsTrustedIdentities(identities)
33 identityChangeDetector.onRemovedIdentity = removeOwnIdentityAndItsTrustedIdentities(oldIdentities)
34 identityChangeDetector.onUnchangedIdentity = detectChangesInTrustedIdentities(identities, oldIdentities)
35 identityChangeDetector.detectChanges(identities.keys)
38 private fun addNewOwnIdentityAndItsTrustedIdentities(newIdentities: Map<OwnIdentity, Collection<Identity>>) =
39 { identity: Identity ->
40 eventBus.post(OwnIdentityAddedEvent(identity as OwnIdentity))
41 newIdentities[identity]
42 ?.map { IdentityAddedEvent(identity, it) }
43 ?.forEach(eventBus::post) ?: Unit
46 private fun removeOwnIdentityAndItsTrustedIdentities(oldIdentities: Map<OwnIdentity, Collection<Identity>>) =
47 { identity: Identity ->
48 eventBus.post(OwnIdentityRemovedEvent(identity as OwnIdentity))
49 oldIdentities[identity]
50 ?.map { IdentityRemovedEvent(identity, it) }
51 ?.forEach(eventBus::post) ?: Unit
54 private fun detectChangesInTrustedIdentities(newIdentities: Map<OwnIdentity, Collection<Identity>>, oldIdentities: Map<OwnIdentity, Collection<Identity>>) =
55 { ownIdentity: Identity ->
56 val identityChangeDetector = IdentityChangeDetector(oldIdentities[ownIdentity as OwnIdentity]!!)
57 identityChangeDetector.onNewIdentity = { eventBus.post(IdentityAddedEvent(ownIdentity, it)) }
58 identityChangeDetector.onRemovedIdentity = { eventBus.post(IdentityRemovedEvent(ownIdentity, it)) }
59 identityChangeDetector.onChangedIdentity = { eventBus.post(IdentityUpdatedEvent(ownIdentity, it)) }
60 identityChangeDetector.detectChanges(newIdentities[ownIdentity]!!)