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

View File

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

View File

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