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

commonMain.io.islandtime.ranges.TimePointIterators.kt Maven / Gradle / Ivy

The newest version!
package io.islandtime.ranges

import io.islandtime.base.TimePoint
import io.islandtime.measures.*

internal class TimePointSecondProgressionIterator>(
    first: T,
    last: T,
    private val step: LongSeconds
) : Iterator {

    private val finalElement = last
    private var hasNext = if (step.value > 0) last > first else last < first
    private var next = if (hasNext) first else finalElement

    override fun hasNext() = hasNext

    override fun next(): T {
        val value = next

        if (value.isSameInstantAs(finalElement)) {
            if (!hasNext) {
                throw NoSuchElementException()
            }

            hasNext = false
        } else {
            next += step
        }

        return value
    }
}

internal class TimePointNanosecondProgressionIterator>(
    first: T,
    last: T,
    private val step: LongNanoseconds
) : Iterator {

    private val finalElement = last
    private var hasNext = if (step.value > 0) last > first else last < first
    private var next = if (hasNext) first else finalElement

    override fun hasNext() = hasNext

    override fun next(): T {
        val value = next

        if (value.isSameInstantAs(finalElement)) {
            if (!hasNext) {
                throw NoSuchElementException()
            }

            hasNext = false
        } else {
            next += step
        }

        return value
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy