mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Implement Entries.getValues
This commit is contained in:
@@ -174,6 +174,26 @@ open class Entries {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values of the entries that fall inside a certain interval of time. The values
|
||||
* are returned in an array containing one integer value for each day of the interval. The
|
||||
* first entry corresponds to the most recent day in the interval. Each subsequent entry
|
||||
* corresponds to one day older than the previous entry. The boundaries of the time interval
|
||||
* are included.
|
||||
*/
|
||||
fun getValues(from: Timestamp, to: Timestamp): IntArray {
|
||||
if (from.isNewerThan(to)) throw IllegalArgumentException()
|
||||
val nDays = from.daysUntil(to) + 1
|
||||
val result = IntArray(nDays) { UNKNOWN }
|
||||
getKnown().filter { entry ->
|
||||
!entry.timestamp.isNewerThan(to) && !entry.timestamp.isOlderThan(from)
|
||||
}.forEach { entry ->
|
||||
val offset = entry.timestamp.daysUntil(to)
|
||||
result[offset] = entry.value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
data class Interval(val begin: Timestamp, val center: Timestamp, val end: Timestamp) {
|
||||
val length: Int
|
||||
get() = begin.daysUntil(end) + 1;
|
||||
|
||||
@@ -64,6 +64,27 @@ class EntriesTest {
|
||||
assertEquals(Entry(today.minus(5), 20), actual[5])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetValues() {
|
||||
val entries = Entries()
|
||||
val today = DateUtils.getToday()
|
||||
|
||||
entries.add(Entry(today.minus(3), YES_MANUAL))
|
||||
entries.add(Entry(today.minus(5), YES_MANUAL))
|
||||
entries.add(Entry(today.minus(6), YES_MANUAL))
|
||||
|
||||
val expected = intArrayOf(
|
||||
UNKNOWN, // 1
|
||||
UNKNOWN, // 2
|
||||
YES_MANUAL, // 3
|
||||
UNKNOWN, // 4
|
||||
YES_MANUAL, // 5
|
||||
YES_MANUAL, // 6
|
||||
UNKNOWN, // 7
|
||||
)
|
||||
assertThat(entries.getValues(today.minus(7), today.minus(1)), equalTo(expected))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testComputeBoolean() {
|
||||
val today = DateUtils.getToday()
|
||||
|
||||
Reference in New Issue
Block a user