
net.pechorina.kairos.automat.State.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kairos-automat Show documentation
Show all versions of kairos-automat Show documentation
Kairos Automat is a finite-state machine library
The newest version!
package net.pechorina.kairos.automat
class State(
val value: S,
var initialState: Boolean = false,
var finalState: Boolean = false,
var onEntry: Action? = null,
var onExit: Action? = null,
var parent: State? = null,
var transitions: List> = listOf()
) {
fun findTransition(event: Event): Transition? {
val t = transitions.find {
it.event == event
}
val transitionNotFound = t == null
val hasParent = parent != null
return if (transitionNotFound && hasParent) {
parent!!.findTransition(event)
} else t
}
fun addTransition(transition: Transition): State {
this.transitions = this.transitions + transition
return this
}
override fun toString(): String {
return "State($value)"
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as State<*, *>
if (value != other.value) return false
return true
}
override fun hashCode(): Int {
return value?.hashCode() ?: 0
}
}