Skip to content

Commit bded349

Browse files
authored
feat(kotlin): define cache operator overloads (#54)
1 parent c354182 commit bded349

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

kotlin/src/main/kotlin/io/github/xanthic/cache/ktx/CacheApiExtensions.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,27 @@ import io.github.xanthic.cache.core.CacheApiSpec
88
* @see CacheApi.create
99
*/
1010
fun <K, V> createCache(init: CacheApiSpec<K, V>.() -> Unit): Cache<K, V> = CacheApi.create(init)
11+
12+
/**
13+
* @see Cache.get
14+
*/
15+
operator fun <K : Any, V : Any> Cache<K, V>.contains(key: K): Boolean = this.get(key) != null
16+
17+
/**
18+
* @see Cache.remove
19+
*/
20+
operator fun <K : Any, V : Any> Cache<K, V>.minusAssign(key: K) {
21+
this.remove(key)
22+
}
23+
24+
/**
25+
* @see Cache.putAll
26+
*/
27+
operator fun <K : Any, V : Any> Cache<K, V>.plusAssign(map: Map<K, V>) {
28+
this.putAll(map)
29+
}
30+
31+
/**
32+
* @see Cache.put
33+
*/
34+
operator fun <K : Any, V : Any> Cache<K, V>.set(key: K, value: V): V? = this.put(key, value)

kotlin/src/test/kotlin/io/github/xanthic/cache/ktx/KotlinTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package io.github.xanthic.cache.ktx
33
import io.github.xanthic.cache.api.domain.ExpiryType
44
import java.time.Duration
55
import kotlin.test.Test
6+
import kotlin.test.assertEquals
67
import kotlin.test.assertNotNull
8+
import kotlin.test.assertTrue
79

810
internal class KotlinTest {
911

@@ -22,6 +24,16 @@ internal class KotlinTest {
2224
}
2325

2426
assertNotNull(cache)
27+
28+
cache["420"] = 420
29+
assertEquals(420, cache["420"])
30+
31+
cache -= "420"
32+
assertTrue("420" !in cache)
33+
34+
cache += mapOf("1" to 1, "2" to 2)
35+
assertTrue("1" in cache)
36+
assertTrue("2" in cache)
2537
}
2638

2739
}

0 commit comments

Comments
 (0)