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

com.lsd.core.properties.PropertiesLoader.kt Maven / Gradle / Ivy

The newest version!
package com.lsd.core.properties

import java.io.IOException
import java.io.InputStream
import java.util.*
import java.util.function.Consumer

class PropertiesLoader(
    private val fileName: String,
    private val defaults: Properties
) {
    fun load(): Properties {
        val inputStream = javaClass.getResourceAsStream("/$fileName")
        val properties = Properties(defaults)
        Optional.ofNullable(inputStream).ifPresent(loadInto(properties))
        properties.putAll(System.getProperties())
        return properties
    }

    private fun loadInto(properties: Properties): Consumer {
        return Consumer { stream: InputStream ->
            try {
                properties.load(stream)
            } catch (e: IOException) {
                println("Failed to load properties file, falling back to default values. Error: [${e.message}]")
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy