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

com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.KotlinClass.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin

import com.ancientlightstudios.quarkus.kotlin.openapi.emitter.CodeWriter
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.expression.ExtendExpression
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.ClassName

class KotlinClass(
    private val name: ClassName,
    private val constructorAccessModifier: KotlinAccessModifier? = null,
    private val asDataClass: Boolean = false,
    private val sealed: Boolean = false,
    private val extends: List = emptyList()
) : KotlinRenderable, AnnotationAware, MethodAware, MemberAware, CompanionAware,
    CommentAware, ClassAware, ConstructorAware {

    private val annotations = KotlinAnnotationContainer()
    private val items = KotlinRenderableBlockContainer()

    private val constructorMembers = KotlinRenderableWrapContainer()
    private val otherMembers = KotlinRenderableBlockContainer(separateItemsWithNewLine = false)
    private var companion: KotlinCompanion? = null
    private var comment: KotlinComment? = null
    private val additionalConstructors = KotlinRenderableBlockContainer()

    override fun addAnnotation(annotation: KotlinAnnotation) {
        annotations.addAnnotation(annotation)
    }

    override fun addMethod(method: KotlinMethod) {
        items.addItem(method)
    }

    override fun addClass(clazz: KotlinClass) {
        items.addItem(clazz)
    }

    override fun addMember(member: KotlinMember) {
        when(member.initializedInConstructor) {
            true -> constructorMembers.addItem(member)
            false -> otherMembers.addItem(member)
        }
    }

    override fun setCompanion(companion: KotlinCompanion) {
        this.companion = companion
    }

    override fun setComment(comment: KotlinComment) {
        this.comment = comment
    }

    override fun addConstructor(constructor: KotlinConstructor) {
        additionalConstructors.addItem(constructor)
    }

    override fun render(writer: CodeWriter) = with(writer) {
        comment?.let {
            it.render(this)
            writeln(forceNewLine = false)
        }

        annotations.render(this)
        if (sealed) {
            write("sealed ")
        }

        if (asDataClass) {
            write("data ")
        }

        write("class ${name.render()}")

        constructorAccessModifier?.let {write(" ${it.value} constructor") }

        if (constructorMembers.isNotEmpty || constructorAccessModifier != null) {
            write("(")
            constructorMembers.render(this)
            write(")")
        }

        if (extends.isNotEmpty()) {
            write(extends.joinToString(prefix = " : ") { it.evaluate() })
        }

        if (otherMembers.isNotEmpty || items.isNotEmpty || companion != null || additionalConstructors.isNotEmpty) {
            writeln(" {")
            indent {

                if (otherMembers.isNotEmpty) {
                    writeln()
                    otherMembers.render(this)
                    writeln(forceNewLine = false) // in case the item already rendered a line break
                }

                if (additionalConstructors.isNotEmpty) {
                    writeln()
                    additionalConstructors.render(this)
                    writeln(forceNewLine = false) // in case the item already rendered a line break
                }

                if (items.isNotEmpty) {
                    writeln()
                    items.render(this)
                    writeln(forceNewLine = false) // in case the item already rendered a line break
                }

                companion?.let {
                    writeln()
                    it.render(this)
                    writeln(forceNewLine = false) // in case the item already rendered a line break
                }

                writeln()
            }
            write("}")
        }
    }

}

interface ClassAware {

    fun addClass(clazz: KotlinClass)

}

fun ClassAware.kotlinClass(
    name: ClassName,
    constructorAccessModifier: KotlinAccessModifier? = null,
    asDataClass: Boolean = false,
    sealed: Boolean = false,
    extends: List = emptyList(),
    block: KotlinClass.() -> Unit
) {
    val content = KotlinClass(name, constructorAccessModifier, asDataClass, sealed, extends).apply(block)
    addClass(content)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy