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

com.github.insanusmokrassar.iobjectk.realisations.SimpleIObject.kt Maven / Gradle / Ivy

package com.github.insanusmokrassar.iobjectk.realisations

import com.github.insanusmokrassar.iobjectk.exceptions.ReadException
import com.github.insanusmokrassar.iobjectk.exceptions.WriteException
import com.github.insanusmokrassar.iobjectk.interfaces.IObject
import java.util.HashMap

open class SimpleIObject : IObject {
    protected val objects: MutableMap

    constructor(from: Map) {
        objects = HashMap(from)
    }

    constructor(from: IObject?) : this() {
        if (from == null) {
            return
        }
        for (key in from.keys()) {
            try {
                objects.put(key, from.get(key))
            } catch (e: ReadException) {
                //TODO: add exception handling
            }

        }
    }

    constructor() {
        objects = HashMap()
    }

    @Throws(WriteException::class)
    override fun put(key: String, value: Any) {
        objects.put(key, value)
    }

    @Throws(WriteException::class)
    override fun putAll(toPutMap: Map) {
        objects.putAll(toPutMap)
    }

    @Throws(ReadException::class)
    override fun get(key: String): T {
        val toReturn = objects[key] ?: throw ReadException("Can't return value - value from key($key) - is null")
        return toReturn as T
    }

    @Throws(WriteException::class)
    override fun remove(key: String) {
        if (objects.remove(key) == null) {
            throw WriteException("Can't remove value for key($key)")
        }
    }

    override fun keys(): Set {
        return objects.keys
    }

    override fun toString(): String {
        return objects.toString()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy