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

commonMain.dev.inmo.micro_utils.common.Mapper.kt Maven / Gradle / Ivy

The newest version!
package dev.inmo.micro_utils.common

import kotlin.jvm.JvmName

interface SimpleMapper {
    fun convertToT1(from: T2): T1
    fun convertToT2(from: T1): T2
}

@JvmName("convertFromT2")
fun  SimpleMapper.convert(from: T2) = convertToT1(from)
@JvmName("convertFromT1")
fun  SimpleMapper.convert(from: T1) = convertToT2(from)

class SimpleMapperImpl(
    private val t1: (T2) -> T1,
    private val t2: (T1) -> T2,
) : SimpleMapper {
    override fun convertToT1(from: T2): T1 = t1.invoke(from)

    override fun convertToT2(from: T1): T2 = t2.invoke(from)
}

@Suppress("NOTHING_TO_INLINE")
inline fun  simpleMapper(
    noinline t1: (T2) -> T1,
    noinline t2: (T1) -> T2,
) = SimpleMapperImpl(t1, t2)

interface SimpleSuspendableMapper {
    suspend fun convertToT1(from: T2): T1
    suspend fun convertToT2(from: T1): T2
}

@JvmName("convertFromT2")
suspend fun  SimpleSuspendableMapper.convert(from: T2) = convertToT1(from)
@JvmName("convertFromT1")
suspend fun  SimpleSuspendableMapper.convert(from: T1) = convertToT2(from)

class SimpleSuspendableMapperImpl(
    private val t1: suspend (T2) -> T1,
    private val t2: suspend (T1) -> T2,
) : SimpleSuspendableMapper {
    override suspend fun convertToT1(from: T2): T1 = t1.invoke(from)

    override suspend fun convertToT2(from: T1): T2 = t2.invoke(from)
}

@Suppress("NOTHING_TO_INLINE")
inline fun  simpleSuspendableMapper(
    noinline t1: suspend (T2) -> T1,
    noinline t2: suspend (T1) -> T2,
) = SimpleSuspendableMapperImpl(t1, t2)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy