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

name.remal.gradle_plugins.dsl.cache.PropertiesCache.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.gradle_plugins.dsl.cache

import name.remal.buildMap
import name.remal.loadProperties
import name.remal.storeAsString
import java.io.ByteArrayInputStream
import java.nio.charset.StandardCharsets.UTF_8
import java.util.*

class PropertiesCache(cacheId: String, version: Int) : BaseCache, Map>("$cacheId.properties", version) {

    override val serializer: (cacheValue: Map) -> ByteArray? = serializer@{ cacheValue ->
        val props = Properties()
        cacheValue.forEach { key, value ->
            if (value != null) {
                props[key] = value.toString()
            }
        }
        if (props.isEmpty) {
            return@serializer null
        } else {
            return@serializer props.storeAsString().toByteArray(UTF_8)
        }
    }

    override val deserializer: (bytes: ByteArray) -> Map? = deserializer@{ bytes ->
        if (bytes.isEmpty()) {
            return@deserializer null

        } else {
            return@deserializer buildMap {
                val props = loadProperties(ByteArrayInputStream(bytes))
                props.forEach { key, value ->
                    if (key != null && value != null) {
                        put(key.toString(), value.toString())
                    }
                }
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy