commonMain.io.islandtime.ranges.DateIterators.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-metadata Show documentation
Show all versions of core-metadata Show documentation
A multiplatform library for working with dates and times
The newest version!
package io.islandtime.ranges
import io.islandtime.Date
import io.islandtime.measures.IntDays
import io.islandtime.measures.IntMonths
internal class DateDayProgressionIterator(
first: Date,
last: Date,
private val step: IntDays
) : Iterator {
private val finalElement = last
private var hasNext = if (step.value > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
override fun hasNext() = hasNext
override fun next(): Date {
val value = next
if (value == finalElement) {
if (!hasNext) {
throw NoSuchElementException()
}
hasNext = false
} else {
next += step
}
return value
}
}
internal class DateMonthProgressionIterator(
first: Date,
last: Date,
private val step: IntMonths
) : Iterator {
private val finalElement = last
private var hasNext = if (step.value > 0) first <= last else first >= last
private var next = if (hasNext) first else finalElement
override fun hasNext() = hasNext
override fun next(): Date {
val value = next
if (value == finalElement) {
if (!hasNext) {
throw NoSuchElementException()
}
hasNext = false
} else {
next += step
}
return value
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy