Widen forValidRefs receiver, chainOptionalFutures

This commit is contained in:
DBotThePony 2024-02-03 23:00:08 +07:00
parent 79f050e662
commit 8a9da77006
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 22 additions and 5 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=1.7.4
projectVersion=1.7.5
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -1,7 +1,9 @@
package ru.dbotthepony.kommons.collect
import ru.dbotthepony.kommons.core.KOptional
import java.lang.ref.Reference
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.stream.Stream
import java.util.stream.StreamSupport
@ -17,8 +19,8 @@ inline fun <reified T> Stream<*>.filterIsInstance(): Stream<T> {
return filter { it is T } as Stream<T>
}
inline fun <T> MutableList<out Reference<out T>>.forValidRefs(fn: (T) -> Unit) {
val iterator = listIterator()
inline fun <T> MutableIterable<Reference<out T>>.forValidRefs(fn: (T) -> Unit) {
val iterator = iterator()
for (value in iterator) {
val get = value.get()
@ -31,8 +33,8 @@ inline fun <T> MutableList<out Reference<out T>>.forValidRefs(fn: (T) -> Unit) {
}
}
inline fun <T> MutableList<out Reference<out T>>.forValidRefsBreak(fn: (T) -> Boolean) {
val iterator = listIterator()
inline fun <T> MutableIterable<Reference<out T>>.forValidRefsBreak(fn: (T) -> Boolean) {
val iterator = iterator()
for (value in iterator) {
val get = value.get()
@ -47,6 +49,21 @@ inline fun <T> MutableList<out Reference<out T>>.forValidRefsBreak(fn: (T) -> Bo
}
}
fun <S, T> chainOptionalFutures(source: Iterable<S>, mapper: (S) -> CompletableFuture<KOptional<T>>): CompletableFuture<KOptional<T>> {
val itr = source.iterator()
if (itr.hasNext()) {
var future = mapper.invoke(itr.next())
for (v in itr)
future = future.thenCompose { if (it.isPresent) CompletableFuture.completedFuture(it) else mapper.invoke(v) }
return future
}
return CompletableFuture.completedFuture(KOptional.empty())
}
fun <T> Stream<T>.asIterable(): Iterable<T> {
return object : Iterable<T> {
override fun iterator(): Iterator<T> {