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

org.babyfish.jimmer.sql.ast.tuple.Tuple2.kt Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.sql.ast.tuple

import org.babyfish.jimmer.sql.ast.impl.TupleImplementor
import java.util.function.BiFunction
import java.util.function.Function

data class Tuple2(
    val _1: T1,
    val _2: T2
) : TupleImplementor {

    override fun size(): Int = 2

    override operator fun get(index: Int): Any? =
        when (index) {
            0 -> _1
            1 -> _2
            else -> throw IllegalArgumentException("Index must between 0 and ${size() - 1}")
        }

    override fun convert(block: BiFunction): TupleImplementor =
        Tuple2(
            block.apply(_1, 0),
            block.apply(_2, 1),
        )

    companion object {

        /**
         * Shortcut method only for java, java8 stream API is too complex
         */
        @JvmStatic
        fun  toMap(tuples: Collection>): Map =
            tuples.associateBy({
                it._1
            }) {
                it._2
            }

        /**
         * Shortcut method only for java, java8 stream API is too complex
         */
        @JvmStatic
        fun  toMap(
            tuples: Collection>,
            valueMapper: Function
        ): Map =
            tuples.associateBy({
                it._1
            }) {
                valueMapper.apply(it._2)
            }

        /**
         * Shortcut method only for java, java8 stream API is too complex
         */
        @JvmStatic
        fun  toMultiMap(
            tuples: Collection>
        ): Map> =
            tuples.groupBy({
                it._1
            }) {
                it._2
            }

        /**
         * Shortcut method only for java, java8 stream API is too complex
         */
        @JvmStatic
        fun  toMultiMap(
            tuples: Collection>,
            valueMapper: Function
        ): Map> =
            tuples.groupBy({
                it._1
            }) {
                valueMapper.apply(it._2)
            }

        @JvmStatic
        @Suppress("UNCHECKED_CAST")
        fun  projection1(tuples: Collection>) : Collection =
            TupleImplementor.projection(tuples, 0) as Collection

        @JvmStatic
        @Suppress("UNCHECKED_CAST")
        fun  projection2(tuples: Collection>) : Collection =
            TupleImplementor.projection(tuples, 1) as Collection
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy