官术网_书友最值得收藏!

How to do it...

  1. Let's start with implementing the Iterator interface for the LocalDate type. We are going to create a custom LocalDateIterator class, which implements the Iterator<LocalDate> interface:
class DateIterator(startDate: LocalDate,
val endDateInclusive: LocalDate,
val stepDays: Long) : Iterator<LocalDate> {
private var currentDate = startDate
override fun hasNext() = currentDate <= endDateInclusive
override fun next(): LocalDate {
val next = currentDate
currentDate = currentDate.plusDays(stepDays)
return next
}
}
  1. Now, we can implement the progression for the LocalDate type. Let's create a new class called DateProgression, which is going to implement the Iterable<LocalDate> and ClosedRange<LocalDate> interfaces:
class DateProgression(override val start: LocalDate,
override val endInclusive: LocalDate,
val stepDays: Long = 1) :
Iterable<LocalDate>,
ClosedRange<LocalDate> {
override fun iterator(): Iterator<LocalDate> {
return DateIterator(start, endInclusive, stepDays)
}

infix fun step(days: Long) = DateProgression(start, endInclusive, days)
}
  1. Finally, declare a custom rangeTo operator for the LocalDate class:
operator fun LocalDate.rangeTo(other: LocalDate) = DateProgression(this, other)

主站蜘蛛池模板: 吴川市| 石柱| 苍山县| 洞口县| 英超| 济源市| 锦屏县| 施秉县| 威信县| 高邑县| 根河市| 瑞丽市| 衡水市| 神池县| 化州市| 得荣县| 社旗县| 资阳市| 永登县| 佳木斯市| 贵溪市| 信阳市| 荥经县| 赣榆县| 南投市| 驻马店市| 凭祥市| 肇州县| 成都市| 鄂伦春自治旗| 仙居县| 海淀区| 潜江市| 海兴县| 封丘县| 呼伦贝尔市| 乌鲁木齐县| 宝兴县| 潞城市| 错那县| 五寨县|