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

commonMain.com.shakelang.util.jvmlib.infos.attributes.AttributeMap.kt Maven / Gradle / Ivy

The newest version!
package com.shakelang.util.jvmlib.infos.attributes

import com.shakelang.util.io.streaming.input.DataInputStream
import com.shakelang.util.io.streaming.output.DataOutputStream
import com.shakelang.util.jvmlib.infos.constants.ConstantInfo
import com.shakelang.util.jvmlib.infos.constants.ConstantPool
import com.shakelang.util.jvmlib.infos.constants.ConstantUser
import com.shakelang.util.primitives.bytes.toBytes

open class AttributeMap(open val map: Map) : Map, ConstantUser {

    override val uses: Array get() = map.values.map { it.uses.toList() }.flatten().toTypedArray()
    override val users: Array get() = map.values.toTypedArray()

    private lateinit var clazz: com.shakelang.util.jvmlib.infos.ClassInfo

    override fun get(key: String): AttributeInfo? = map[key]
    override val entries: Set>
        get() = map.entries

    override val keys: Set
        get() = map.keys

    override val size: Int
        get() = map.size

    override val values: Collection
        get() = map.values

    override fun containsKey(key: String): Boolean = map.containsKey(key)
    override fun containsValue(value: AttributeInfo): Boolean = map.containsValue(value)
    override fun isEmpty(): Boolean = map.isEmpty()

    fun toBytes(): ByteArray {
        val childBytes = mutableListOf()
        childBytes.addAll(this.size.toUShort().toBytes().toList())
        for (i in map.values.indices) {
            val attribute = map.values.elementAt(i)
            childBytes.addAll(attribute.toBytes().toList())
        }
        return childBytes.toByteArray()
    }

    fun toJson() = map.map {
        it.value.toJson()
    }

    fun init(clazz: com.shakelang.util.jvmlib.infos.ClassInfo) {
        this.clazz = clazz
        for (i in map.values.indices) {
            val attribute = map.values.elementAt(i)
            attribute.init(clazz)
        }
    }

    fun dump(out: DataOutputStream) = out.writeByteArray(this.toBytes())

    companion object {

        fun fromStream(pool: ConstantPool, stream: DataInputStream): AttributeMap {
            val size = stream.readUnsignedShort()
            val map = mutableMapOf()
            for (i in 0 until size.toInt()) {
                val attribute = AttributeInfo.fromStream(pool, stream)
                map[attribute.name.value] = attribute
            }
            return AttributeMap(map)
        }
    }
}

class MutableAttributeMap(map: MutableMap) :
    AttributeMap(map),
    MutableMap {

    override val map: MutableMap
        get() = super.map as MutableMap

    override val entries: MutableSet>
        get() = map.entries

    override val keys: MutableSet
        get() = map.keys

    override val values: MutableCollection
        get() = map.values

    override fun clear() = map.clear()

    override fun put(key: String, value: AttributeInfo): AttributeInfo? = map.put(key, value)

    override fun putAll(from: Map) = map.putAll(from)

    override fun remove(key: String): AttributeInfo? = map.remove(key)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy