arrow.meta.internal.Utils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of arrow-meta Show documentation
Show all versions of arrow-meta Show documentation
Functional companion to Kotlin's Standard Library
package arrow.meta.internal
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getSuperNames
// copied from stdlib without A : Any
fun Iterable.filterNotNull(): List = filterNotNullTo(ArrayList())
fun , T> Iterable.filterNotNullTo(destination: C): C {
for (element in this) if (element != null) destination.add(element)
return destination
}
inline fun > Iterable.mapNotNullTo(
destination: C,
transform: (T) -> R?
): C {
forEach { element -> transform(element)?.let { destination.add(it) } }
return destination
}
inline fun Iterable.mapNotNull(transform: (T) -> R?): List {
return mapNotNullTo(ArrayList(), transform)
}
inline fun > Map.mapNotNullTo(
destination: C,
transform: (Map.Entry) -> R?
): C {
forEach { element -> transform(element)?.let { destination.add(it) } }
return destination
}
inline fun Map.mapNotNull(transform: (Map.Entry) -> R?): List {
return mapNotNullTo(ArrayList(), transform)
}
data class SealedSubclass(
val simpleName: Name,
val fqName: FqName?,
val typeVariables: List
)
fun KtClass.sealedSubclasses(): List =
innerSealedSubclasses() + outerSealedSubclasses()
fun List.sealedVariants(superKt: KtClass): List =
filter {
(it is KtClassOrObject) && it.getSuperNames().contains(superKt.nameAsSafeName.identifier)
}
.map { it as KtClassOrObject }
.map {
SealedSubclass(
simpleName = it.nameAsSafeName,
fqName = it.fqName,
typeVariables = if (it is KtClass) it.renderTypeParameters else emptyList()
)
}
fun KtClass.innerSealedSubclasses(): List = declarations.sealedVariants(this)
fun KtClass.outerSealedSubclasses(): List =
containingKtFile.declarations.sealedVariants(this)
val KtClass.renderTypeParameters: List
get() =
typeParameters.map { it.nameAsSafeName.identifier }.map {
it.replace("out ", "").replace("in ", "")
}
/**
* From Eugenio's https://github.com/Takhion/kotlin-metadata If this [isNotBlank] then it adds the
* optional [prefix] and [postfix].
*/
fun String.plusIfNotBlank(prefix: String = "", postfix: String = "") =
if (isNotBlank()) "$prefix${this}$postfix" else this
© 2015 - 2025 Weber Informatics LLC | Privacy Policy