This repository has been archived by the owner on May 29, 2021. It is now read-only.
/
ReceiversManager.kt
88 lines (85 loc) · 3.16 KB
/
ReceiversManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.github.insanusmokrassar.ConfigsRemapper
import com.github.insanusmokrassar.IObjectK.interfaces.CommonIObject
import com.github.insanusmokrassar.IObjectK.interfaces.IObject
import com.github.insanusmokrassar.IObjectK.realisations.SimpleIObject
import com.github.insanusmokrassar.IObjectK.utils.plus
import com.github.insanusmokrassar.IObjectKRealisations.readIObject
import com.github.insanusmokrassar.IObjectKRealisations.toObject
import kotlinx.coroutines.experimental.async
import java.util.logging.Logger
class ReceiversManager(
private val handlingMixinObject: IObject<Any> = SimpleIObject(),
vararg configs: ConfigModel
) {
private val logger = Logger.getLogger(ReceiversManager::class.java.simpleName)
private val commandsMap = configs.let {
val map = HashMap<String, MutableSet<ConfigModel>>()
it.forEach {
currentConfigModel ->
currentConfigModel.commands.forEach {
command ->
(map[command] ?:let {
HashSet<ConfigModel>().apply {
map[command] = this
}
}).add(currentConfigModel)
}
}
map
}
constructor(
handlingMixinObject: IObject<Any> = SimpleIObject(),
vararg configs: IObject<Any>
) : this(
handlingMixinObject,
*configs.map { it.toObject(ConfigModel::class.java) }.toTypedArray()
)
constructor(
handlingMixinObject: IObject<Any> = SimpleIObject(),
vararg configs: String
) : this(
handlingMixinObject,
*configs.map { it.byteInputStream().readIObject() }.toTypedArray()
)
fun handle(
command: String,
config: CommonIObject<String, Any>,
async: Boolean = true,
exceptionHandler: (Exception) -> Unit = {
logger.throwing(
this::class.java.simpleName,
"handle",
it
)
}
) {
try {
commandsMap[command] ?.apply {
forEach {
configModel ->
if (async) {
async {
try {
configModel.receiverObject(
configModel.makeParamsObject(handlingMixinObject + config)
)
} catch (e: Exception) {
exceptionHandler(e)
}
}
} else {
try {
configModel.receiverObject(
configModel.makeParamsObject(handlingMixinObject + config)
)
} catch (e: Exception) {
exceptionHandler(e)
}
}
}
} ?: throw IllegalArgumentException("Command receiver not found: $command")
} catch (e: Exception) {
exceptionHandler(e)
}
}
}