Add mutating operations to streamy iterators
This commit is contained in:
parent
5e7c8c08f5
commit
9ae099ec23
@ -20,6 +20,8 @@ import ru.dbotthepony.mc.otm.container.MatteryContainer
|
|||||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||||
import ru.dbotthepony.mc.otm.container.UpgradeContainer
|
import ru.dbotthepony.mc.otm.container.UpgradeContainer
|
||||||
import ru.dbotthepony.mc.otm.container.balance
|
import ru.dbotthepony.mc.otm.container.balance
|
||||||
|
import ru.dbotthepony.mc.otm.core.collect.filter
|
||||||
|
import ru.dbotthepony.mc.otm.core.collect.findAny
|
||||||
import ru.dbotthepony.mc.otm.menu.tech.PlatePressMenu
|
import ru.dbotthepony.mc.otm.menu.tech.PlatePressMenu
|
||||||
import ru.dbotthepony.mc.otm.menu.tech.TwinPlatePressMenu
|
import ru.dbotthepony.mc.otm.menu.tech.TwinPlatePressMenu
|
||||||
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
||||||
@ -88,10 +90,10 @@ class PlatePressBlockEntity(
|
|||||||
val recipe = level.recipeManager
|
val recipe = level.recipeManager
|
||||||
.byType(MRecipes.PLATE_PRESS)
|
.byType(MRecipes.PLATE_PRESS)
|
||||||
.values
|
.values
|
||||||
.stream()
|
.iterator()
|
||||||
.filter { it.matches(inputContainer, id) }
|
.filter { it.matches(inputContainer, id) }
|
||||||
.findAny().
|
.findAny()
|
||||||
orElse(null) ?: return JobContainer.noItem()
|
.orElse(null) ?: return JobContainer.noItem()
|
||||||
|
|
||||||
val toProcess = inputContainer[id].count.coerceAtMost(1 + upgrades.processingItems)
|
val toProcess = inputContainer[id].count.coerceAtMost(1 + upgrades.processingItems)
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import java.util.stream.Collector
|
|||||||
|
|
||||||
// Aside parallel work, unimplemented Stream API elements can be easily implemented when required
|
// Aside parallel work, unimplemented Stream API elements can be easily implemented when required
|
||||||
|
|
||||||
class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>) : Iterator<T> {
|
class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>) : MutableIterator<T> {
|
||||||
private var foundValue: Any? = Companion
|
private var foundValue: Any? = Companion
|
||||||
|
|
||||||
override fun hasNext(): Boolean {
|
override fun hasNext(): Boolean {
|
||||||
@ -54,10 +54,19 @@ class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate
|
|||||||
return foundValue as T
|
return foundValue as T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
if (foundValue === Companion) {
|
||||||
|
throw NoSuchElementException()
|
||||||
|
}
|
||||||
|
|
||||||
|
(parent as MutableIterator<T>).remove()
|
||||||
|
foundValue = Companion
|
||||||
|
}
|
||||||
|
|
||||||
private companion object
|
private companion object
|
||||||
}
|
}
|
||||||
|
|
||||||
class MappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> R) : Iterator<R> {
|
class MappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> R) : MutableIterator<R> {
|
||||||
override fun hasNext(): Boolean {
|
override fun hasNext(): Boolean {
|
||||||
return parent.hasNext()
|
return parent.hasNext()
|
||||||
}
|
}
|
||||||
@ -65,10 +74,15 @@ class MappingIterator<T, R>(private val parent: Iterator<T>, private val transfo
|
|||||||
override fun next(): R {
|
override fun next(): R {
|
||||||
return transform.invoke(parent.next())
|
return transform.invoke(parent.next())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
(parent as MutableIterator<T>).remove()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> Iterator<R>) : Iterator<R> {
|
class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> Iterator<R>) : MutableIterator<R> {
|
||||||
private var current: Iterator<R>? = null
|
private var current: Iterator<R>? = null
|
||||||
|
private var last: Iterator<R>? = null
|
||||||
|
|
||||||
override fun hasNext(): Boolean {
|
override fun hasNext(): Boolean {
|
||||||
while (current?.hasNext() != true && parent.hasNext()) {
|
while (current?.hasNext() != true && parent.hasNext()) {
|
||||||
@ -82,7 +96,14 @@ class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val tra
|
|||||||
if (!hasNext())
|
if (!hasNext())
|
||||||
throw NoSuchElementException()
|
throw NoSuchElementException()
|
||||||
|
|
||||||
return current!!.next()
|
val v = current!!.next()
|
||||||
|
last = current
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
(last as MutableIterator<R>? ?: throw NoSuchElementException()).remove()
|
||||||
|
last = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,12 +126,13 @@ class LimitingIterator<T>(val parent: Iterator<T>, val limit: Long) : Iterator<T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : Iterator<T> {
|
class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : MutableIterator<T> {
|
||||||
init {
|
init {
|
||||||
require(skip >= 0) { "Invalid skip amount $skip" }
|
require(skip >= 0) { "Invalid skip amount $skip" }
|
||||||
}
|
}
|
||||||
|
|
||||||
private var found = skip
|
private var found = skip
|
||||||
|
private var returned = false
|
||||||
|
|
||||||
override fun hasNext(): Boolean {
|
override fun hasNext(): Boolean {
|
||||||
while (parent.hasNext() && found > 0L) {
|
while (parent.hasNext() && found > 0L) {
|
||||||
@ -125,15 +147,26 @@ class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : Iterator<T> {
|
|||||||
if (!hasNext())
|
if (!hasNext())
|
||||||
throw NoSuchElementException()
|
throw NoSuchElementException()
|
||||||
|
|
||||||
return parent.next()
|
val v = parent.next()
|
||||||
|
returned = true
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
if (!returned) {
|
||||||
|
throw NoSuchElementException()
|
||||||
|
}
|
||||||
|
|
||||||
|
returned = false
|
||||||
|
(parent as MutableIterator<T>).remove()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Iterator<T>.filter(condition: Predicate<in T>): Iterator<T> = FilteredIterator(this, condition)
|
fun <T> Iterator<T>.filter(condition: Predicate<in T>) = FilteredIterator(this, condition)
|
||||||
fun <T, R> Iterator<T>.map(mapper: (T) -> R): Iterator<R> = MappingIterator(this, mapper)
|
fun <T, R> Iterator<T>.map(mapper: (T) -> R) = MappingIterator(this, mapper)
|
||||||
fun <T, R> Iterator<T>.map(mapper: java.util.function.Function<T, R>): Iterator<R> = MappingIterator(this, mapper::apply)
|
fun <T, R> Iterator<T>.map(mapper: java.util.function.Function<T, R>) = MappingIterator(this, mapper::apply)
|
||||||
fun <T, R> Iterator<T>.flatMap(mapper: (T) -> Iterator<R>): Iterator<R> = FlatMappingIterator(this, mapper)
|
fun <T, R> Iterator<T>.flatMap(mapper: (T) -> Iterator<R>) = FlatMappingIterator(this, mapper)
|
||||||
fun <T, R> Iterator<T>.flatMap(mapper: java.util.function.Function<T, Iterator<R>>): Iterator<R> = FlatMappingIterator(this, mapper::apply)
|
fun <T, R> Iterator<T>.flatMap(mapper: java.util.function.Function<T, Iterator<R>>) = FlatMappingIterator(this, mapper::apply)
|
||||||
|
|
||||||
fun interface O2DFunction<T> {
|
fun interface O2DFunction<T> {
|
||||||
fun apply(value: T): Double
|
fun apply(value: T): Double
|
||||||
|
Loading…
Reference in New Issue
Block a user