📄 Update filenames in file headers
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityChangeEventSender.kt
1 /*
2  * Sone - IdentityChangeEventSender.kt - Copyright Â© 2013–2019 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.freenet.wot.event.*
22
23 /**
24  * Detects changes in [Identity]s trusted by multiple [OwnIdentity]s.
25  *
26  * @see IdentityChangeDetector
27  */
28 class IdentityChangeEventSender(private val eventBus: EventBus, private val oldIdentities: Map<OwnIdentity, Collection<Identity>>) {
29
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)
36         }
37
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
44                         }
45
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
52                         }
53
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]!!)
61                         }
62
63 }