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

com.skillw.asahi.util.condition.ConditionOperator.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.util.condition

/**
 * @className A
 *
 * @author Glom
 * @date 2023/1/16 3:33 Copyright 2024 Glom.
 */
internal enum class ConditionOperator(
    private val symbol: String,
    val priority: Int,
    val calc: (Boolean, Boolean) -> Boolean = { _, _ -> true },
) {
    AND("and", 1, { a, b -> a && b }),
    OR("or", 1, { a, b -> b || a }),
    LEFT_BRACKET("(", 2),
    RIGHT_BRACKET(")", 2);

    override fun toString(): String {
        return symbol
    }

    companion object {
        private val symbols = ConditionOperator.values().map { it.symbol }.toHashSet()
        fun String.isConditionOperator(): Boolean = this in symbols
        fun String.toConditionOperator(): ConditionOperator {
            return when (this) {
                "and", "&&" -> AND
                "or", "||" -> OR
                "(" -> LEFT_BRACKET
                ")" -> RIGHT_BRACKET
                else -> error("No such Operator $this")
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy