-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rangeTo support for java.util.Date
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package me.moallemi.tools.daterange.date | ||
|
||
import java.util.Date | ||
|
||
operator fun Date.rangeTo(other: Date) = DateRange(this, other) |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/me/moallemi/tools/daterange/date/DateIterator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.moallemi.tools.daterange.date | ||
|
||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
class DateIterator( | ||
startDate: Date, | ||
private val endDate: Date, | ||
private val stepDays: Int | ||
) : Iterator<Date> { | ||
|
||
private val calendar = Calendar.getInstance(Locale.getDefault()) | ||
private var currentDate = startDate | ||
|
||
override fun hasNext() = currentDate <= endDate | ||
|
||
override fun next(): Date { | ||
val next = currentDate | ||
|
||
calendar.time = currentDate | ||
calendar.add(Calendar.DATE, stepDays) | ||
currentDate = calendar.time | ||
|
||
return next | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/me/moallemi/tools/daterange/date/DateRange.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package me.moallemi.tools.daterange.date | ||
|
||
import java.util.Date | ||
|
||
class DateRange( | ||
override val start: Date, | ||
override val endInclusive: Date, | ||
private val stepDays: Int = 1 | ||
) : Iterable<Date>, ClosedRange<Date> { | ||
|
||
override fun iterator(): Iterator<Date> = | ||
DateIterator(start, endInclusive, stepDays) | ||
|
||
infix fun step(days: Int) = DateRange(start, endInclusive, days) | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/kotlin/me/moallemi/tools/daterange/date/DateRangeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package me.moallemi.tools.daterange.date | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Calendar | ||
import java.util.Locale | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
|
||
class DateRangeTest { | ||
|
||
private val calendar = Calendar.getInstance(Locale.getDefault()) | ||
private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) | ||
|
||
@Test | ||
fun testRange() { | ||
val expected = listOf( | ||
"2020-01-01", | ||
"2020-01-02", | ||
"2020-01-03", | ||
"2020-01-04", | ||
"2020-01-05" | ||
) | ||
calendar.set(2020, 0, 1) | ||
val startDate = calendar.time | ||
calendar.set(2020, 0, 5) | ||
val endDate = calendar.time | ||
|
||
val actual = (startDate..endDate).map { | ||
simpleDateFormat.format(it) | ||
} | ||
|
||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun testStep() { | ||
val expected = listOf( | ||
"2020-01-01", | ||
"2020-01-03", | ||
"2020-01-05" | ||
) | ||
calendar.set(2020, 0, 1) | ||
val startDate = calendar.time | ||
calendar.set(2020, 0, 5) | ||
val endDate = calendar.time | ||
|
||
val actual = (startDate..endDate step 2).map { | ||
simpleDateFormat.format(it) | ||
} | ||
|
||
assertEquals(expected, actual) | ||
} | ||
} |