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

model.WithChildren.kt Maven / Gradle / Ivy

Go to download

Dokka is an API documentation engine for Kotlin and Java, performing the same function as Javadoc for Java

There is a newer version: 2.0.0
Show newest version
package org.jetbrains.dokka.model

interface WithChildren {
    val children: List
}

inline fun  WithChildren<*>.firstChildOfTypeOrNull(): T? =
    children.filterIsInstance().firstOrNull()

inline fun  WithChildren<*>.firstChildOfTypeOrNull(predicate: (T) -> Boolean): T? =
    children.filterIsInstance().firstOrNull(predicate)

inline fun  WithChildren<*>.firstChildOfType(): T =
    children.filterIsInstance().first()

inline fun  WithChildren<*>.childrenOfType(): List =
    children.filterIsInstance()

inline fun  WithChildren<*>.firstChildOfType(predicate: (T) -> Boolean): T =
    children.filterIsInstance().first(predicate)

inline fun  WithChildren>.firstMemberOfType(): T where T : WithChildren<*> {
    return withDescendants().filterIsInstance().first()
}

inline fun  WithChildren>.firstMemberOfType(
    predicate: (T) -> Boolean
): T where T : WithChildren<*> = withDescendants().filterIsInstance().first(predicate)


inline fun  WithChildren>.firstMemberOfTypeOrNull(): T? where T : WithChildren<*> {
    return withDescendants().filterIsInstance().firstOrNull()
}

fun  T.withDescendants(): Sequence where T : WithChildren {
    return sequence {
        yield(this@withDescendants)
        children.forEach { child ->
            yieldAll(child.withDescendants())
        }
    }
}

@JvmName("withDescendantsProjection")
fun WithChildren<*>.withDescendants(): Sequence {
    return sequence {
        yield(this@withDescendants)
        children.forEach { child ->
            if (child is WithChildren<*>) {
                yieldAll(child.withDescendants())
            }
        }
    }
}

@JvmName("withDescendantsAny")
fun WithChildren.withDescendants(): Sequence {
    return sequence {
        yield(this@withDescendants)
        children.forEach { child ->
            if (child is WithChildren<*>) {
                yieldAll(child.withDescendants().filterNotNull())
            }
        }
    }
}

fun  T.dfs(predicate: (T) -> Boolean): T? where T : WithChildren = if (predicate(this)) {
    this
} else {
    children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull()
}

fun > T.asPrintableTree(
    nodeNameBuilder: Appendable.(T) -> Unit = { append(it.toString()) }
): String {
    fun Appendable.append(element: T, ownPrefix: String, childPrefix: String) {
        append(ownPrefix)
        nodeNameBuilder(element)
        appendLine()
        element.children.takeIf(Collection<*>::isNotEmpty)?.also { children ->
            val newOwnPrefix = childPrefix + '\u251c' + '\u2500' + ' '
            val lastOwnPrefix = childPrefix + '\u2514' + '\u2500' + ' '
            val newChildPrefix = childPrefix + '\u2502' + ' ' + ' '
            val lastChildPrefix = childPrefix + ' ' + ' ' + ' '
            children.forEachIndexed { n, e ->
                if (n != children.lastIndex) append(e, newOwnPrefix, newChildPrefix)
                else append(e, lastOwnPrefix, lastChildPrefix)
            }
        }
    }

    return buildString { append(this@asPrintableTree, "", "") }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy