-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandDispatcherAsyncImpl.kt
211 lines (172 loc) · 6.46 KB
/
CommandDispatcherAsyncImpl.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package body.core.commandDispatcher
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executor
/**
* Created by vicboma on 08/02/17.
*/
class CommandDispatcherAsyncImpl<T> internal constructor() : CommandDispatcherAsync<T> {
private val map by lazy { ConcurrentHashMap<String, MutableList<CompletableFuture<T>>>() }
init {
}
companion object {
fun <J> create(): CommandDispatcherAsync<J> = CommandDispatcherAsyncImpl()
}
override fun add(command: String, completableList: List<CompletableFuture<T>>): CommandDispatcherAsync<T> {
completableList
.filter { it -> it != null }
.forEach { it -> add(command, it) }
return this
}
override fun add(command: String, completable: CompletableFuture<T>): CommandDispatcherAsync<T> {
val listFun = map.get(command)
when (listFun) {
null -> map.put(command, arrayListOf(completable))
else -> {
if (!listFun.contains(completable))
listFun.add(completable)
}
}
return this
}
override fun contains(command: String) = map.containsKey(command)
override fun remove(command: String, completable: CompletableFuture<T>): CommandDispatcherAsync<T> {
val listFun = map.get(command)
when (listFun) {
null -> {
}
else -> {
val index = listFun.indexOf(completable)
if (listFun.indexOf(completable) != -1) {
val completableFuture = listFun?.removeAt(index)
completableFuture?.cancel(true)
if (listFun?.size == 0)
listFun?.clear()
}
}
}
return this
}
override fun remove(command: String): CommandDispatcherAsync<T> {
map.remove(command)
return this
}
override fun dispatch(command: String): CommandDispatcherAsync<T> {
val listFun = map.get(command)
if (null != listFun) {
for (i in listFun!!.indices) {
val res = intArrayOf(i)
executeRunnable(completable(listFun, res))
}
}
return this
}
override fun dispatch(command: String, executor: Executor): CommandDispatcherAsync<T> {
val listFun = map.get(command)
if (null != listFun) {
for (i in listFun!!.indices) {
val res = intArrayOf(i)
executeRunnable(completable(listFun, res),executor)
}
}
return this
}
override fun dispatchOnce(command: String): CommandDispatcherAsync<T> {
val listFun = map.get(command)
if (null != listFun) {
for (i in listFun!!.indices) {
val res = intArrayOf(i)
executeRunnable(completable(listFun, res))
}
remove(command)
}
return this
}
override fun dispatchOnce(command: String,executor: Executor): CommandDispatcherAsync<T> {
val listFun = map.get(command)
if (null != listFun) {
for (i in listFun!!.indices) {
val res = intArrayOf(i)
executeRunnable(completable(listFun, res),executor)
}
remove(command)
}
return this
}
private fun completable(listFun: MutableList<CompletableFuture<T>>?, res: IntArray): Runnable {
return Runnable {
listFun!!.get(res[0])
}
}
override fun dispatchAll(command: String): CompletableFuture<Void> {
val child = map.get(command)
val toArray = child?.toTypedArray()
if(toArray?.size!! <= 0)
return CompletableFuture.completedFuture(null)
val res = hashSetOf<CompletableFuture<T>>()
return executeRunnable(this.all(res, toArray))
}
override fun dispatchAll(command: String, executor:Executor): CompletableFuture<Void> {
val child = map.get(command)
val toArray = child?.toTypedArray()
if(toArray?.size!! <= 0)
return CompletableFuture.completedFuture(null)
val res = hashSetOf<CompletableFuture<T>>()
return executeRunnable(this.all(res, toArray),executor)
}
private fun all(res: HashSet<CompletableFuture<T>>, toArray: Array<CompletableFuture<T>>?): Runnable {
return Runnable {
logger.debug("Start dispatchAll ")
while (res.size != toArray?.size) {
toArray?.
filter { it.isDone }?.
forEach {
res.add(it)
}
}
res.forEach { logger.debug("${it.toString()}") }
logger.debug("End dispatchAll ")
}
}
override fun <H> dispatchAny(command: String) : CompletableFuture<H> {
val child = map.get(command)
val toArray = child?.toTypedArray()
if(toArray?.size!! <= 0)
return CompletableFuture.completedFuture(null)
val res = CompletableFuture<H>()
executeRunnable(this.any(res, toArray))
return res
}
override fun <H> dispatchAny(command: String, executor: Executor) : CompletableFuture<H> {
val child = map.get(command)
val toArray = child?.toTypedArray()
if(toArray?.size!! <= 0)
return CompletableFuture.completedFuture(null)
val res = CompletableFuture<H>()
executeRunnable(this.any(res, toArray),executor)
return res
}
private fun <H> any(res: CompletableFuture<H>, toArray: Array<CompletableFuture<T>>?) : Runnable {
return Runnable {
logger.debug("Start dispatchAny ")
while (!res.isDone) {
toArray?.
filter { it.isDone }?.
forEach {
when (!res.isDone) {
true -> {
logger.debug("${res.toString()}")
res.complete(it.get() as (H))
logger.debug("${res.toString()}")
}
else -> {
}
}
}
}
logger.debug("End dispatchAny ")
}
}
override fun size() = this.map.size
}