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

spi.strategy.KotlinCodeGenerationStrategyList.kt Maven / Gradle / Ivy

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

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationContext
import io.toolisticon.kotlin.generation.spi.KotlinCodeGenerationStrategy
import io.toolisticon.kotlin.generation.spi.UnboundKotlinCodeGenerationStrategy
import kotlin.reflect.KClass

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

  constructor(vararg strategy: UnboundKotlinCodeGenerationStrategy) : this(strategy.toList())

  /**
   * Filter the current list and return instances of given type.
   *
   * @param strategyType defining which concrete implementations to use
   * @return list containing only instances of given [strategyType]
   */
  fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, SPEC : Any> filter(
    strategyType: KClass
  ): List {
    return list.filterIsInstance(strategyType.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(STRATEGY::class)

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

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

@ExperimentalKotlinPoetApi
fun , CONTEXT : KotlinCodeGenerationContext, INPUT : Any, SPEC : Any> List.executeAll(
  context: CONTEXT,
  input: INPUT
): List = mapNotNull { it.execute(context, input) }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy