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

net.devslash.data.ListDataSupplier.kt Maven / Gradle / Ivy

There is a newer version: 0.26.2
Show newest version
package net.devslash.data

import net.devslash.ListRequestData
import net.devslash.RequestData
import net.devslash.RequestDataSupplier
import java.util.concurrent.atomic.AtomicInteger

class ListDataSupplier(private val list: Lazy>, private val clazz: Class) : RequestDataSupplier {
  private val line = AtomicInteger(0)

  companion object {
    inline operator fun  invoke(list: List): ListDataSupplier {
      return ListDataSupplier(lazy { list }, T::class.java)
    }

    // There used to be a way to send a list of strings, and have it go to a list of list of strings
    @JvmName("stringToList")
    operator fun invoke(list: List): ListDataSupplier> {
      return ListDataSupplier(list.map { listOf(it) }.toList())
    }

    /**
     * Due to supporting `ListDataSupplier(listOf("String"))` returning a supplier that works for
     * List call types that doesn't attempt to resolve to a set of lists
     */
    inline fun  typed(stringList: List): ListDataSupplier {
      return ListDataSupplier(lazy { stringList }, T::class.java)
    }

    @JvmName("lazyStringToList")
    operator fun  invoke(
        lazyList: Lazy>,
        trans: (T) -> List
    ): ListDataSupplier> {
      return ListDataSupplier(transformedLazyList(lazyList, trans))
    }

    @JvmName("lazyListStringToList")
    operator fun invoke(
        ll: Lazy>
    ): ListDataSupplier> {
      return invoke(ll) { listOf(it) }
    }

    private fun  transformedLazyList(
        lazyList: Lazy>,
        transform: (T) -> List
    ): Lazy>> {
      // TODO: Do i really need to do this?? Feels weird
      return lazy {
        object : List> {
          override fun contains(element: List): Boolean = throw UnsupportedOperationException()
          override fun containsAll(elements: Collection>) = throw UnsupportedOperationException()
          override fun isEmpty(): Boolean = lazyList.value.isEmpty()
          override fun indexOf(element: List): Int = throw UnsupportedOperationException()
          override fun iterator(): Iterator> = throw UnsupportedOperationException()
          override fun lastIndexOf(element: List): Int = throw UnsupportedOperationException()
          override fun listIterator(): ListIterator> = throw UnsupportedOperationException()
          override fun listIterator(index: Int): ListIterator> = throw UnsupportedOperationException()
          override fun subList(fromIndex: Int, toIndex: Int): List> = throw UnsupportedOperationException()

          override val size: Int
            get() = lazyList.value.size

          override fun get(index: Int): List = transform(lazyList.value[index])
        }
      }
    }

    @Deprecated(
        "Instead of transforming, send type directly",
        ReplaceWith("ListDataSupplier(list)", "net.devslash.data.ListDataSupplier")
    )
    inline operator fun  invoke(list: List, transform: (Q) -> T): ListDataSupplier {
      return ListDataSupplier(list.map { transform(it) }.toList())
    }

    inline operator fun  invoke(list: Lazy>): ListDataSupplier {
      return ListDataSupplier(list, T::class.java)
    }

    @Suppress("unused")
    inline fun  single(item: T): ListDataSupplier {
      return ListDataSupplier(lazy { listOf(item) }, T::class.java)
    }
  }

  override suspend fun getDataForRequest(): RequestData? {
    val index = line.getAndIncrement()
    val obj = list.value.getOrNull(index) ?: return null
    return ListRequestData(obj, clazz)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy