All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.pechorina.kairos.automat.State.kt Maven / Gradle / Ivy

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
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy