1
+ /*
2
+ * Author: Mikhail Fedotov
3
+ * Github: https://github.com/KStateMachine
4
+ * Copyright (c) 2024.
5
+ * All rights reserved.
6
+ */
7
+
8
+ package ru.nsk.kstatemachine.statemachine
9
+
10
+ interface CreationArguments {
11
+ /* *
12
+ * Allows the library to automatically call destroy() on current state owning machine instance if user tries
13
+ * to reuse its states in another machine. Usually this is a result of using object states in sequentially created
14
+ * similar machines. destroy() will be called on the previous machine instance.
15
+ * If set to false an exception will be thrown on state reuse attempt.
16
+ * Default: true
17
+ */
18
+ val autoDestroyOnStatesReuse: Boolean
19
+
20
+ /* *
21
+ * Enables Undo transition.
22
+ * Default: false
23
+ */
24
+ val isUndoEnabled: Boolean
25
+
26
+ /* *
27
+ * If set to true, when multiple transitions match event the first matching transition is selected.
28
+ * if set to false, when multiple transitions match event exception is thrown.
29
+ * Default: false
30
+ */
31
+ val doNotThrowOnMultipleTransitionsMatch: Boolean
32
+
33
+ /* *
34
+ * If enabled, throws exception on the machine start,
35
+ * if it contains states or transitions with null or blank names
36
+ * Default: false
37
+ */
38
+ val requireNonBlankNames: Boolean
39
+
40
+ /* *
41
+ * If set, enables incoming events recording in order to restore [StateMachine] later.
42
+ * By default, event recording is disabled.
43
+ * Use [StateMachine.eventRecorder] to access the recording result.
44
+ * Default: null
45
+ */
46
+ val eventRecordingArguments: EventRecordingArguments ?
47
+ }
48
+
49
+ interface CreationArgumentsBuilder : CreationArguments {
50
+ override var autoDestroyOnStatesReuse: Boolean
51
+ override var isUndoEnabled: Boolean
52
+ override var doNotThrowOnMultipleTransitionsMatch: Boolean
53
+ override var requireNonBlankNames: Boolean
54
+ override var eventRecordingArguments: EventRecordingArguments ?
55
+ }
56
+
57
+ private data class CreationArgumentsBuilderImpl (
58
+ override var autoDestroyOnStatesReuse : Boolean = true ,
59
+ override var isUndoEnabled : Boolean = false ,
60
+ override var doNotThrowOnMultipleTransitionsMatch : Boolean = false ,
61
+ override var requireNonBlankNames : Boolean = false ,
62
+ override var eventRecordingArguments : EventRecordingArguments ? = null
63
+ ) : CreationArgumentsBuilder
64
+
65
+ fun buildCreationArguments (builder : CreationArgumentsBuilder .() -> Unit ): CreationArguments =
66
+ CreationArgumentsBuilderImpl ().apply (builder).copy()
67
+
68
+ interface EventRecordingArguments {
69
+ /* *
70
+ * If enabled removes all recorded events when detects that the machine was stopped and started again.
71
+ * Default: true
72
+ */
73
+ val clearRecordsOnMachineRestart: Boolean
74
+
75
+ /* *
76
+ * If enabled skips ignored events, supposing they do not affect restoration of the machine
77
+ * Default: true
78
+ */
79
+ val skipIgnoredEvents: Boolean
80
+ }
81
+
82
+ interface EventRecordingArgumentsBuilder : EventRecordingArguments {
83
+ override var clearRecordsOnMachineRestart: Boolean
84
+ override var skipIgnoredEvents: Boolean
85
+ }
86
+
87
+ private data class EventRecordingArgumentsBuilderImpl (
88
+ override var clearRecordsOnMachineRestart : Boolean = true ,
89
+ override var skipIgnoredEvents : Boolean = true ,
90
+ ) : EventRecordingArgumentsBuilder
91
+
92
+ fun buildEventRecordingArguments (builder : EventRecordingArgumentsBuilder .() -> Unit ): EventRecordingArguments =
93
+ EventRecordingArgumentsBuilderImpl ().apply (builder).copy()
0 commit comments