From 109de2b4144d37023cec304a615118c98a6d63c6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 3 Aug 2023 19:50:19 +0700 Subject: [PATCH] Add streamy iterator docs --- .../mc/otm/core/collect/StreamyIterator.kt | 66 +++++++++++++++++-- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt index ad729b6d7..1ca57f6a1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterator.kt @@ -14,6 +14,11 @@ import java.util.stream.Collector // Aside parallel work, unimplemented Stream API elements can be easily implemented when required +/** + * Filters elements of [parent] iterator + * + * Resulting [Iterator] is [MutableIterator] if [parent] is + */ class FilteredIterator(private val parent: Iterator, private val predicate: Predicate) : MutableIterator { private var foundValue: Any? = Companion @@ -66,13 +71,18 @@ class FilteredIterator(private val parent: Iterator, private val predicate private companion object } -class MappingIterator(private val parent: Iterator, private val transform: (T) -> R) : MutableIterator { +/** + * Maps elements of [parent] iterator from values of [T] to [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [parent] is + */ +class MappingIterator(private val parent: Iterator, private val mapper: (T) -> R) : MutableIterator { override fun hasNext(): Boolean { return parent.hasNext() } override fun next(): R { - return transform.invoke(parent.next()) + return mapper.invoke(parent.next()) } override fun remove() { @@ -80,13 +90,18 @@ class MappingIterator(private val parent: Iterator, private val transfo } } -class FlatMappingIterator(private val parent: Iterator, private val transform: (T) -> Iterator) : MutableIterator { +/** + * Maps elements of [parent] iterator from type [T] to other iterators of type [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [parent] is + */ +class FlatMappingIterator(private val parent: Iterator, private val mapper: (T) -> Iterator) : MutableIterator { private var current: Iterator? = null private var last: Iterator? = null override fun hasNext(): Boolean { while (current?.hasNext() != true && parent.hasNext()) { - current = transform.invoke(parent.next()) + current = mapper.invoke(parent.next()) } return current?.hasNext() == true @@ -107,7 +122,12 @@ class FlatMappingIterator(private val parent: Iterator, private val tra } } -class LimitingIterator(val parent: Iterator, val limit: Long) : Iterator { +/** + * Limits amount of values returned by [parent] iterator to return at most [limit] values + * + * Resulting [Iterator] is [MutableIterator] if [parent] is + */ +class LimitingIterator(private val parent: Iterator, private val limit: Long) : Iterator { init { require(limit > 0) { "Invalid limit $limit" } } @@ -126,7 +146,12 @@ class LimitingIterator(val parent: Iterator, val limit: Long) : Iterator(val parent: Iterator, skip: Long) : MutableIterator { +/** + * Skips (discards) up to [skip] values returned by [parent] iterator + * + * Resulting [Iterator] is [MutableIterator] if [parent] is + */ +class SkippingIterator(private val parent: Iterator, skip: Long) : MutableIterator { init { require(skip >= 0) { "Invalid skip amount $skip" } } @@ -162,10 +187,39 @@ class SkippingIterator(val parent: Iterator, skip: Long) : MutableIterator } } +/** + * Filters elements of [this] iterator + * + * Resulting [Iterator] is [MutableIterator] if [this] is + */ fun Iterator.filter(condition: Predicate) = FilteredIterator(this, condition) + +/** + * Maps elements of [this] iterator from values of [T] to [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [this] is + */ fun Iterator.map(mapper: (T) -> R) = MappingIterator(this, mapper) + +/** + * Maps elements of [this] iterator from values of [T] to [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [this] is + */ fun Iterator.map(mapper: java.util.function.Function) = MappingIterator(this, mapper::apply) + +/** + * Maps elements of [this] iterator from type [T] to other iterators of type [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [this] is + */ fun Iterator.flatMap(mapper: (T) -> Iterator) = FlatMappingIterator(this, mapper) + +/** + * Maps elements of [this] iterator from type [T] to other iterators of type [R] using function [mapper] + * + * Resulting [Iterator] is [MutableIterator] if [this] is + */ fun Iterator.flatMap(mapper: java.util.function.Function>) = FlatMappingIterator(this, mapper::apply) fun interface O2DFunction {