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

spi.processor.KotlinCodeGenerationProcessorList.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package io.toolisticon.kotlin.generation.spi.processor

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContext
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationProcessor
import io.toolisticon.kotlin.generation.spi.UnboundKotlinCodeGenerationProcessor
import kotlin.reflect.KClass

/**
 * Wraps list of [KotlinCodeGenerationProcessor] instances. Used to provide additional functionalities.
 */
@ExperimentalKotlinPoetApi
@JvmInline
value class KotlinCodeGenerationProcessorList(
  @PublishedApi
  internal val list: List
) : List by list {

  constructor(vararg processors: UnboundKotlinCodeGenerationProcessor) : this(processors.toList())

  /**
   * Filter the current list and return instances of given type.
   *
   * @param processorType defining which concrete implementations to use
   * @return list containing only instances of given [processorType]
   */
  fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, BUILDER : Any> filter(
    processorType: KClass
  ): List {
    return list.filterIsInstance(processorType.java)
  }

  /**
   * Filter the current list and return instances of given type.
   * @return list containing only instances of reified type.
   */
  inline fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, SPEC : Any> filter() = filter(PROCESSOR::class)


  override fun toString(): String = "KotlinCodeGenerationProcessorList(processors=${list.map { it.name }})"
}


@ExperimentalKotlinPoetApi
fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, BUILDER : Any> List.executeSingle(
  context: CONTEXT,
  input: INPUT,
  builder: BUILDER
): BUILDER = single().execute(context, input, builder)

@ExperimentalKotlinPoetApi
fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, BUILDER : Any> List.executeAll(
  context: CONTEXT,
  input: INPUT,
  builder: BUILDER
): BUILDER = fold(builder) { acc, cur -> cur.execute(context, input, acc) }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy