Review: Add duplication details for DuplicatedPermissionRegistrationException

This commit is contained in:
Him188 2020-09-09 22:06:09 +08:00
parent 703c4feae1
commit 39ef007370
3 changed files with 14 additions and 16 deletions

View File

@ -12,6 +12,9 @@ package net.mamoe.mirai.console.permission
import net.mamoe.mirai.console.permission.PermissibleIdentifier.Companion.grantedWith
import java.util.concurrent.CopyOnWriteArrayList
/**
*
*/
@ExperimentalPermission
public abstract class AbstractConcurrentPermissionService<P : Permission> : PermissionService<P> {
protected abstract val permissions: MutableMap<PermissionId, P>
@ -28,9 +31,8 @@ public abstract class AbstractConcurrentPermissionService<P : Permission> : Perm
override fun register(id: PermissionId, description: String, base: PermissionId): P {
grantedPermissionsMap[id] = CopyOnWriteArrayList() // mutations are not quite often performed
val instance = createPermission(id, description, base)
if (permissions.putIfAbsent(id, instance) != null) {
throw DuplicatedPermissionRegistrationException("Duplicated Permission registry. new: $instance, old: ${permissions[id]}")
}
val old = permissions.putIfAbsent(id, instance)
if (old != null) throw DuplicatedPermissionRegistrationException(instance, old)
return instance
}

View File

@ -35,9 +35,8 @@ public object AllGrantPermissionService : PermissionService<PermissionImpl> {
base: PermissionId
): PermissionImpl {
val new = PermissionImpl(id, description, base)
if (all.putIfAbsent(id, new) != null) {
throw DuplicatedPermissionRegistrationException("Duplicated Permission registry: ${all[id]}")
}
val old = all.putIfAbsent(id, new)
if (old != null) throw DuplicatedPermissionRegistrationException(new, old)
return new
}
@ -67,9 +66,8 @@ public object AllDenyPermissionService : PermissionService<PermissionImpl> {
base: PermissionId
): PermissionImpl {
val new = PermissionImpl(id, description, base)
if (all.putIfAbsent(id, new) != null) {
throw DuplicatedPermissionRegistrationException("Duplicated Permission registry: ${all[id]}")
}
val old = all.putIfAbsent(id, new)
if (old != null) throw DuplicatedPermissionRegistrationException(new, old)
return new
}

View File

@ -7,14 +7,12 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("unused")
@file:Suppress("unused", "MemberVisibilityCanBePrivate", "CanBeParameter")
package net.mamoe.mirai.console.permission
@ExperimentalPermission
public open class DuplicatedPermissionRegistrationException : Exception {
public constructor() : super()
public constructor(message: String?) : super(message)
public constructor(message: String?, cause: Throwable?) : super(message, cause)
public constructor(cause: Throwable?) : super(cause)
}
public class DuplicatedPermissionRegistrationException(
newInstance: Permission,
public val existingInstance: Permission
) : Exception("Duplicated Permission registry. new: $newInstance, existing: $existingInstance")