Remove duplications in BuiltInPermissionService.SaveData

This commit is contained in:
Him188 2020-09-10 20:51:00 +08:00
parent 829bd6d454
commit 12662f4716
2 changed files with 17 additions and 19 deletions

View File

@ -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<P : Permission> : PermissionService<P> {
internal abstract class AbstractConcurrentPermissionService<P : Permission> : PermissionService<P> {
protected abstract val permissions: MutableMap<PermissionId, P>
protected abstract val grantedPermissionsMap: MutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
protected abstract val grantedPermissionsMap: PluginDataExtensions.NotNullMutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
protected abstract fun createPermission(
id: PermissionId,
@ -29,7 +29,6 @@ public abstract class AbstractConcurrentPermissionService<P : Permission> : 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<P : Permission> : 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<P> = permissions.values.asSequence()
public override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P> = sequence<P> {
override fun getGrantedPermissions(permissibleIdentifier: PermissibleIdentifier): Sequence<P> = sequence<P> {
for ((permissionIdentifier, permissibleIdentifiers) in grantedPermissionsMap) {
val granted =

View File

@ -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<PermissionImpl> {
private val all = ConcurrentHashMap<PermissionId, PermissionImpl>()
override val permissionType: KClass<PermissionImpl> 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<PermissionImpl> {
private val all = ConcurrentHashMap<PermissionId, PermissionImpl>()
override val permissionType: KClass<PermissionImpl>
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<P
@ExperimentalPermission
override val permissionType: KClass<PermissionImpl>
get() = PermissionImpl::class
override val permissions: MutableMap<PermissionId, PermissionImpl> = ConcurrentHashMap()
override val rootPermission: PermissionImpl
get() = RootPermissionImpl
override val permissions: ConcurrentHashMap<PermissionId, PermissionImpl> = ConcurrentHashMap()
override val rootPermission: PermissionImpl = RootPermissionImpl.also { permissions[it.id] = it }
@Suppress("UNCHECKED_CAST")
override val grantedPermissionsMap: MutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
get() = config.grantedPermissionMap as MutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
override val grantedPermissionsMap: PluginDataExtensions.NotNullMutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
get() = config.grantedPermissionMap as PluginDataExtensions.NotNullMutableMap<PermissionId, MutableCollection<PermissibleIdentifier>>
override fun createPermission(id: PermissionId, description: String, parent: Permission): PermissionImpl =
PermissionImpl(id, description, parent)
@ -114,9 +114,9 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
public override val saveName: String,
@Suppress("UNUSED_PARAMETER") primaryConstructorMark: Any?
) : AutoSavePluginConfig() {
public val grantedPermissionMap: MutableMap<PermissionId, MutableList<AbstractPermissibleIdentifier>>
by value<MutableMap<PermissionId, MutableList<AbstractPermissibleIdentifier>>>(ConcurrentHashMap())
.withDefault { CopyOnWriteArrayList() }
public val grantedPermissionMap: PluginDataExtensions.NotNullMutableMap<PermissionId, MutableSet<AbstractPermissibleIdentifier>>
by value<MutableMap<PermissionId, MutableSet<AbstractPermissibleIdentifier>>>(ConcurrentHashMap())
.withDefault { CopyOnWriteArraySet() }
public companion object {
@JvmStatic