aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal002@users.noreply.github.com>2024-05-29 07:43:22 +0200
committerGitHub <noreply@github.com>2024-05-29 07:43:22 +0200
commit2250aa8b2a64ea1f1850b74c5f4f34ffa617609e (patch)
tree1c01b2fd03da182f851bf176ec4c6b050c42d1b0 /src/main/java/at/hannibal2/skyhanni/utils
parenta1801699f7191de07523c8d54359234645c20520 (diff)
downloadskyhanni-2250aa8b2a64ea1f1850b74c5f4f34ffa617609e.tar.gz
skyhanni-2250aa8b2a64ea1f1850b74c5f4f34ffa617609e.tar.bz2
skyhanni-2250aa8b2a64ea1f1850b74c5f4f34ffa617609e.zip
Feature: Trophy Fish Display (#1754)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt4
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt11
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt6
3 files changed, 17 insertions, 4 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
index 915677bb6..2e1237fe8 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
@@ -156,6 +156,10 @@ object CollectionUtils {
add(Collections.singletonList(text))
}
+ fun MutableList<List<Renderable>>.addSingleString(text: String) {
+ add(Collections.singletonList(Renderable.string(text)))
+ }
+
fun <K, V : Comparable<V>> List<Pair<K, V>>.sorted(): List<Pair<K, V>> {
return sortedBy { (_, value) -> value }
}
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt
index 182b1381b..caf5b7734 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt
@@ -14,11 +14,14 @@ class TimeLimitedCache<K: Any, V: Any>(
.removalListener { removalListener(it.key, it.value) }
.build<K, V>()
- fun put(key: K, value: V) = cache.put(key, value)
+ // TODO IntelliJ cant replace this, find another way?
+// @Deprecated("outdated", ReplaceWith("[key] = value"))
+ @Deprecated("outdated", ReplaceWith("set(key, value)"))
+ fun put(key: K, value: V) = set(key, value)
fun getOrNull(key: K): V? = cache.getIfPresent(key)
- fun getOrPut(key: K, defaultValue: () -> V) = getOrNull(key) ?: defaultValue().also { put(key, it) }
+ fun getOrPut(key: K, defaultValue: () -> V) = getOrNull(key) ?: defaultValue().also { set(key, it) }
fun clear() = cache.invalidateAll()
@@ -31,4 +34,8 @@ class TimeLimitedCache<K: Any, V: Any>(
fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null
override fun iterator(): Iterator<Map.Entry<K, V>> = entries().iterator()
+
+ operator fun set(key: K, value: V) {
+ cache.put(key, value)
+ }
}
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
index fae86fac5..aab5595da 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
@@ -2,14 +2,16 @@ package at.hannibal2.skyhanni.utils
import kotlin.time.Duration
-class TimeLimitedSet<T: Any>(
+class TimeLimitedSet<T : Any>(
expireAfterWrite: Duration,
private val removalListener: (T) -> Unit = {},
) {
private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite) { key, _ -> key?.let { removalListener(it) } }
- fun add(element: T) = cache.put(element, Unit)
+ fun add(element: T) {
+ cache[element] = Unit
+ }
operator fun contains(element: T): Boolean = cache.containsKey(element)