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

eu.vaadinonkotlin.Utils.kt Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
package eu.vaadinonkotlin

import org.slf4j.LoggerFactory
import java.io.Closeable
import java.time.Instant
import java.util.*
import kotlin.time.Duration
import kotlin.time.toJavaDuration

/**
 * Closes [this] quietly - if [Closeable.close] fails, an INFO message is logged. The exception is not
 * rethrown.
 */
public fun Closeable.closeQuietly() {
    try {
        close()
    } catch (e: Exception) {
        LoggerFactory.getLogger(javaClass).info("Failed to close $this", e)
    }
}

public val Instant.toDate: Date get() = Date(toEpochMilli())

public fun Iterable.filterNotBlank(): List =
        filterNotNull().filter { it.isNotBlank() }

public infix operator fun Instant.plus(other: Duration): Instant = this.plus(other.toJavaDuration())
public infix operator fun Date.plus(other: Duration): Date = Date(time + other.inWholeMilliseconds)

/**
 * Converts this class to its non-primitive counterpart. For example, converts `int.class` to `Integer.class`.
 * @return converts class of primitive type to appropriate non-primitive class; other classes are simply returned as-is.
 */
@Suppress("UNCHECKED_CAST")
public val  Class.nonPrimitive: Class
    get() = when (this) {
        Integer.TYPE -> Integer::class.java as Class
        java.lang.Long.TYPE -> Long::class.java as Class
        java.lang.Float.TYPE -> Float::class.java as Class
        java.lang.Double.TYPE -> java.lang.Double::class.java as Class
        java.lang.Short.TYPE -> Short::class.java as Class
        java.lang.Byte.TYPE -> Byte::class.java as Class
        else -> this
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy