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

commonMain.io.github.optimumcode.json.schema.AnnotationKey.kt Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package io.github.optimumcode.json.schema

import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

/**
 * Class represents a key with certain type that can be used
 * to annotate a [JSON element][kotlinx.serialization.json.JsonElement].
 * Only **one instance** of a key should be created and used to annotate or retrieve annotations.
 *
 * ```kotlin
 *object Factory : AbstractAssertionFactory(/* .. */) {
 *  val ANNOTATION: AnnotationKey = AnnotationKey.simple(/*...*/)
 *}
 * ```
 */
public sealed class AnnotationKey private constructor(
  private val name: String,
  internal val type: KClass,
) {
  override fun equals(other: Any?): Boolean = this === other

  override fun hashCode(): Int {
    var result = name.hashCode()
    result = 31 * result + type.hashCode()
    return result
  }

  override fun toString(): String = "${this::class.simpleName}($name(${type.simpleName}))"

  internal class SimpleAnnotationKey private constructor(
    name: String,
    type: KClass,
  ) : AnnotationKey(name, type) {
    internal companion object {
      @JvmStatic
      fun  create(
        name: String,
        type: KClass,
      ): AnnotationKey = SimpleAnnotationKey(name, type)
    }
  }

  internal class AggregatableAnnotationKey private constructor(
    name: String,
    type: KClass,
    internal val aggregator: Aggregator,
  ) : AnnotationKey(name, type) {
    internal companion object {
      @JvmStatic
      fun  create(
        name: String,
        type: KClass,
        aggregator: (T, T) -> T,
      ): AnnotationKey = AggregatableAnnotationKey(name, type, aggregator)
    }
  }

  public companion object {
    @JvmStatic
    public inline fun  simple(name: String): AnnotationKey = simple(name, T::class)

    @JvmStatic
    public fun  simple(
      name: String,
      type: KClass,
    ): AnnotationKey = SimpleAnnotationKey.create(name, type)
  }
}

internal fun interface Aggregator {
  fun aggregate(
    a: T,
    b: T,
  ): T?
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy