AstrideUG / DarkBedrock

DarkBedrock is a API collection with focus on Minecraft server modding
2 stars 0 forks source link

Add DefaultWithMap #175

Open LarsArtmann opened 5 years ago

LarsArtmann commented 5 years ago

    class DefaultWithMap<M, O>(private val entries: MutableMap<M, O> = mutableMapOf(), private val default: O) {

        operator fun getValue(input: M, property: KProperty<*>): O = entries[input] ?: default

        operator fun setValue(input: M, property: KProperty<*>, value: O) {
            if (value == null) entries.remove(input) else entries[input] = value
        }

    }