-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sett opp caching av opplysninger og mulige regler
- Loading branch information
Showing
9 changed files
with
117 additions
and
113 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
39 changes: 39 additions & 0 deletions
39
opplysninger/src/main/kotlin/no/nav/dagpenger/opplysning/CachedList.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,39 @@ | ||
package no.nav.dagpenger.opplysning | ||
|
||
class CachedList<T>( | ||
val compute: () -> List<T>, | ||
) : List<T> { | ||
private var cachedValue: List<T>? = null | ||
private val value: List<T> | ||
get() = cachedValue ?: refresh() | ||
|
||
fun refresh(): List<T> { | ||
cachedValue = compute() | ||
return cachedValue!! | ||
} | ||
|
||
override val size get() = value.size | ||
|
||
override fun contains(element: T) = value.contains(element) | ||
|
||
override fun containsAll(elements: Collection<T>) = value.containsAll(elements) | ||
|
||
override fun get(index: Int) = value[index] | ||
|
||
override fun indexOf(element: T) = value.indexOf(element) | ||
|
||
override fun isEmpty() = value.isEmpty() | ||
|
||
override fun iterator() = value.iterator() | ||
|
||
override fun lastIndexOf(element: T) = value.lastIndexOf(element) | ||
|
||
override fun listIterator() = value.listIterator() | ||
|
||
override fun listIterator(index: Int) = value.listIterator(index) | ||
|
||
override fun subList( | ||
fromIndex: Int, | ||
toIndex: Int, | ||
) = value.subList(fromIndex, toIndex) | ||
} |
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
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
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
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
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
21 changes: 21 additions & 0 deletions
21
opplysninger/src/test/kotlin/no/nav/dagpenger/opplysning/CachedListTest.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,21 @@ | ||
package no.nav.dagpenger.opplysning | ||
|
||
import io.kotest.matchers.collections.shouldContainExactly | ||
import org.junit.jupiter.api.Test | ||
|
||
class CachedListTest { | ||
@Test | ||
fun `cacher lister som er mutable på utsiden`() { | ||
var basertPå = mutableListOf(1) | ||
var externalList = mutableListOf(2, 3) | ||
val cachedList = CachedList { basertPå + externalList.filter { it != 5 } } | ||
|
||
cachedList.shouldContainExactly(1, 2, 3) | ||
|
||
externalList.add(4) | ||
cachedList.shouldContainExactly(1, 2, 3) | ||
|
||
cachedList.refresh() | ||
cachedList.shouldContainExactly(1, 2, 3, 4) | ||
} | ||
} |
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