Skip to content

Commit 9b25c3b

Browse files
committed
Return Set instead of List from acceptedEvents
1 parent 01b34ac commit 9b25c3b

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

lib/src/main/kotlin/io/nexure/fsm/StateMachine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface StateMachine<S : Any, E : Any> {
3939
* Return a list of events that are accepted by the state machine in the given state. The returned list will be an
4040
* empty list if the state is a terminal state.
4141
*/
42-
fun acceptedEvents(state: S): List<E>
42+
fun acceptedEvents(state: S): Set<E>
4343

4444
companion object {
4545
fun <S : Any, E : Any> builder(): StateMachineBuilder.Uninitialized<S, E> = StateMachineBuilder()

lib/src/main/kotlin/io/nexure/fsm/StateMachineImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ internal class StateMachineImpl<S : Any, E : Any>(
2828
return Accepted(next)
2929
}
3030

31-
override fun acceptedEvents(state: S): List<E> =
32-
allowedTransitions.getOrDefault(state, emptySet()).map { it.second }
31+
override fun acceptedEvents(state: S): Set<E> =
32+
allowedTransitions.getOrDefault(state, emptySet()).map { it.second }.toSet()
3333

3434
private fun nextState(source: S, event: E): S? {
3535
val targets: Set<Pair<S, E>> = allowedTransitions.getOrDefault(source, emptySet())

lib/src/test/kotlin/io/nexure/fsm/StateMachineTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,19 @@ class StateMachineTest {
289289
.connect(State.S3, State.S4, Event.E3)
290290
.build()
291291

292-
assertEquals(listOf(Event.E1, Event.E2), fsm.acceptedEvents(State.S1))
292+
assertEquals(setOf(Event.E1, Event.E2), fsm.acceptedEvents(State.S1))
293293
}
294294

295295
@Test
296-
fun `test acceptedEvents on terminal state`() {
296+
fun `test acceptedEvents is empty on terminal state`() {
297297
val fsm = StateMachine.builder<State, Event>()
298298
.initial(State.S1)
299299
.connect(State.S1, State.S2, Event.E1)
300300
.connect(State.S1, State.S3, Event.E2)
301301
.connect(State.S3, State.S4, Event.E3)
302302
.build()
303303

304-
assertEquals(emptyList<Event>(), fsm.acceptedEvents(State.S4))
304+
assertEquals(emptySet<Event>(), fsm.acceptedEvents(State.S4))
305305
}
306306
}
307307

0 commit comments

Comments
 (0)