From 12662f47166bf072b951abad921b848f6e217968 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 10 Sep 2020 20:51:00 +0800 Subject: [PATCH] Remove duplications in BuiltInPermissionService.SaveData --- .../AbstractConcurrentPermissionService.kt | 14 +++++------- .../permission/BuiltInPermissionServices.kt | 22 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt index 7dc14bc51..21563301a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt @@ -9,16 +9,16 @@ package net.mamoe.mirai.console.permission +import net.mamoe.mirai.console.data.PluginDataExtensions import net.mamoe.mirai.console.permission.PermissibleIdentifier.Companion.grantedWith -import java.util.concurrent.CopyOnWriteArrayList /** * */ @ExperimentalPermission -public abstract class AbstractConcurrentPermissionService

: PermissionService

{ +internal abstract class AbstractConcurrentPermissionService

: PermissionService

{ protected abstract val permissions: MutableMap - protected abstract val grantedPermissionsMap: MutableMap> + protected abstract val grantedPermissionsMap: PluginDataExtensions.NotNullMutableMap> protected abstract fun createPermission( id: PermissionId, @@ -29,7 +29,6 @@ public abstract class AbstractConcurrentPermissionService

: Perm override fun get(id: PermissionId): P? = permissions[id] override fun register(id: PermissionId, description: String, parent: Permission): P { - grantedPermissionsMap[id] = CopyOnWriteArrayList() // mutations are not quite often performed val instance = createPermission(id, description, parent) val old = permissions.putIfAbsent(id, instance) if (old != null) throw DuplicatedPermissionRegistrationException(instance, old) @@ -38,16 +37,15 @@ public abstract class AbstractConcurrentPermissionService

: Perm override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: P) { val id = permission.id - grantedPermissionsMap[id]?.add(permissibleIdentifier) - ?: error("Bad PermissionService implementation: grantedPermissionsMap[id] is null.") + grantedPermissionsMap[id].add(permissibleIdentifier) } override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: P) { - grantedPermissionsMap[permission.id]?.remove(permissibleIdentifier) + grantedPermissionsMap[permission.id].remove(permissibleIdentifier) } override fun getRegisteredPermissions(): Sequence

= permissions.values.asSequence() - public override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence

= sequence

{ + override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence

= sequence

{ for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) { val granted = diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt index 18dde66af..647399841 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt @@ -11,10 +11,11 @@ package net.mamoe.mirai.console.permission import kotlinx.serialization.Serializable import net.mamoe.mirai.console.data.AutoSavePluginConfig +import net.mamoe.mirai.console.data.PluginDataExtensions import net.mamoe.mirai.console.data.PluginDataExtensions.withDefault import net.mamoe.mirai.console.data.value import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CopyOnWriteArraySet import kotlin.reflect.KClass @@ -22,7 +23,7 @@ import kotlin.reflect.KClass internal object AllGrantPermissionService : PermissionService { private val all = ConcurrentHashMap() override val permissionType: KClass get() = PermissionImpl::class - override val rootPermission: PermissionImpl get() = RootPermissionImpl + override val rootPermission: PermissionImpl get() = RootPermissionImpl.also { all[it.id] = it } override fun register( id: PermissionId, @@ -59,7 +60,7 @@ internal object AllDenyPermissionService : PermissionService { private val all = ConcurrentHashMap() override val permissionType: KClass get() = PermissionImpl::class - override val rootPermission: PermissionImpl get() = RootPermissionImpl + override val rootPermission: PermissionImpl = RootPermissionImpl.also { all[it.id] = it } override fun register( id: PermissionId, @@ -94,13 +95,12 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService

get() = PermissionImpl::class - override val permissions: MutableMap = ConcurrentHashMap() - override val rootPermission: PermissionImpl - get() = RootPermissionImpl + override val permissions: ConcurrentHashMap = ConcurrentHashMap() + override val rootPermission: PermissionImpl = RootPermissionImpl.also { permissions[it.id] = it } @Suppress("UNCHECKED_CAST") - override val grantedPermissionsMap: MutableMap> - get() = config.grantedPermissionMap as MutableMap> + override val grantedPermissionsMap: PluginDataExtensions.NotNullMutableMap> + get() = config.grantedPermissionMap as PluginDataExtensions.NotNullMutableMap> override fun createPermission(id: PermissionId, description: String, parent: Permission): PermissionImpl = PermissionImpl(id, description, parent) @@ -114,9 +114,9 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService

> - by value>>(ConcurrentHashMap()) - .withDefault { CopyOnWriteArrayList() } + public val grantedPermissionMap: PluginDataExtensions.NotNullMutableMap> + by value>>(ConcurrentHashMap()) + .withDefault { CopyOnWriteArraySet() } public companion object { @JvmStatic