com.kamelia.sprinkler.util.ExtendedCollectors.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
Sprinkler@utils | Black Kamelia
package com.kamelia.sprinkler.util
import java.util.stream.Collector
import java.util.stream.Collectors
/**
* Class containing additional [collectors][Collectors] for [streams][java.util.stream.Stream].
*
* @see Collector
* @see Collectors
*/
object ExtendedCollectors {
/**
* Returns a [collector][Collector] that collects elements to a [map][Map] from [pairs][Pair] of keys and values.
*
* @param K the type of keys
* @param V the type of values
* @return a [collector][Collector] that creates a [map][Map] from [pairs][Pair] of keys and values
*/
@JvmStatic
fun toMap(): Collector, *, Map> = Collectors.toMap(Pair::first, Pair::second)
/**
* Returns a [collector][Collector] that collects elements to an [array][Array].
*
* @param T the type of elements
* @param factory a function that creates an array of the desired type and the given size
* @return a [collector][Collector] that creates an [array][Array] from elements
*/
@JvmStatic
fun toArray(factory: (Int) -> Array): Collector> = Collector.of, Array?>(
::ArrayList,
{ list, t -> list.add(t) },
{ list1, list2 -> list1.apply { addAll(list2) } },
{ list ->
val array = factory(list.size).unsafeCast>()
list.forEachIndexed { index, t -> array[index] = t }
array
}
)
/**
* Returns a [collector][Collector] that collects elements to an [int array][IntArray].
*
* @return a [collector][Collector] that creates an [int array][IntArray] from elements
*/
@JvmStatic
fun toIntArray(): Collector = toPrimitiveArray(::IntArray, IntArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [long array][LongArray].
*
* @return a [collector][Collector] that creates a [long array][LongArray] from elements
*/
@JvmStatic
fun toLongArray(): Collector = toPrimitiveArray(::LongArray, LongArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [double array][DoubleArray].
*
* @return a [collector][Collector] that creates a [double array][DoubleArray] from elements
*/
@JvmStatic
fun toDoubleArray(): Collector = toPrimitiveArray(::DoubleArray, DoubleArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [float array][FloatArray].
*
* @return a [collector][Collector] that creates a [float array][FloatArray] from elements
*/
@JvmStatic
fun toFloatArray(): Collector = toPrimitiveArray(::FloatArray, FloatArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [short array][ShortArray].
*
* @return a [collector][Collector] that creates a [short array][ShortArray] from elements
*/
@JvmStatic
fun toShortArray(): Collector = toPrimitiveArray(::ShortArray, ShortArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [byte array][ByteArray].
*
* @return a [collector][Collector] that creates a [byte array][ByteArray] from elements
*/
@JvmStatic
fun toByteArray(): Collector = toPrimitiveArray(::ByteArray, ByteArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [char array][CharArray].
*
* @return a [collector][Collector] that creates a [char array][CharArray] from elements
*/
@JvmStatic
fun toCharArray(): Collector = toPrimitiveArray(::CharArray, CharArray::set)
/**
* Returns a [collector][Collector] that collects elements to a [boolean array][BooleanArray].
*
* @return a [collector][Collector] that creates a [boolean array][BooleanArray] from elements
*/
@JvmStatic
fun toBooleanArray(): Collector = toPrimitiveArray(::BooleanArray, BooleanArray::set)
private fun toPrimitiveArray(
arrayFactory: (Int) -> R,
arrayAccumulator: R.(Int, T) -> Unit
): Collector = Collector.of(
::ArrayList,
ArrayList::add,
{ list1, list2 -> list1.apply { addAll(list2) } },
{ list ->
val r = arrayFactory(list.size)
list.forEachIndexed { index, t -> r.arrayAccumulator(index, t) }
r
}
)
}