summaryrefslogtreecommitdiff
path: root/libs/utils
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-07-15 23:52:28 +0200
committerJoel Klinghed <the_jk@spawned.biz>2024-07-16 00:25:07 +0200
commit42564c71cfb70c28831c662a3b6bf4084e079353 (patch)
tree111456fd3e8dce884d0380a81d70950062c7d212 /libs/utils
parent4a8f6807c9d3ee6bcfac25aee832163036b4e6fe (diff)
Break out io code in libs
Preparing for adding more io implementations. Really tried writing the convention plugins in kotlin dsl but could not find the exact right hacks to get it to work.
Diffstat (limited to 'libs/utils')
-rw-r--r--libs/utils/build.gradle.kts12
-rw-r--r--libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt14
-rw-r--r--libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt32
-rw-r--r--libs/utils/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt40
4 files changed, 98 insertions, 0 deletions
diff --git a/libs/utils/build.gradle.kts b/libs/utils/build.gradle.kts
new file mode 100644
index 0000000..b4f0ae5
--- /dev/null
+++ b/libs/utils/build.gradle.kts
@@ -0,0 +1,12 @@
+plugins {
+ alias(libs.plugins.android.library)
+}
+
+android {
+ namespace = "org.the_jk.cleversync.utils"
+}
+
+dependencies {
+ api(libs.androidx.livedata)
+ api(libs.androidx.livedata.ktx)
+}
diff --git a/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt b/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt
new file mode 100644
index 0000000..7f6ab1f
--- /dev/null
+++ b/libs/utils/src/main/java/org/the_jk/cleversync/LiveDataUtils.kt
@@ -0,0 +1,14 @@
+package org.the_jk.cleversync
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.Observer
+
+fun <T> LiveData<T>.safeValue(): T? {
+ if (this.hasActiveObservers())
+ return value
+ var ret: T? = null
+ val observer = Observer<T> { value -> ret = value }
+ this.observeForever(observer)
+ this.removeObserver(observer)
+ return ret
+}
diff --git a/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt b/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt
new file mode 100644
index 0000000..6adea24
--- /dev/null
+++ b/libs/utils/src/main/java/org/the_jk/cleversync/StringUtils.kt
@@ -0,0 +1,32 @@
+package org.the_jk.cleversync
+
+object StringUtils {
+ fun split(input: String, delimiter: Char, keepEmpty: Boolean = true, limit: Int = 0): List<String> {
+ return buildList {
+ var offset = 0
+ var count = 0
+ while (true) {
+ val next = input.indexOf(delimiter, offset)
+ if (next == -1) {
+ if (keepEmpty || offset < input.length) {
+ if (limit > 0 && count == limit) {
+ add("${removeLast()}${delimiter}${input.substring(offset)}")
+ break
+ }
+ add(input.substring(offset))
+ }
+ break
+ }
+ if (keepEmpty || offset < next) {
+ if (limit > 0 && count == limit) {
+ add("${removeLast()}${delimiter}${input.substring(offset)}")
+ break
+ }
+ add(input.substring(offset, next))
+ count++
+ }
+ offset = next + 1
+ }
+ }
+ }
+}
diff --git a/libs/utils/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt b/libs/utils/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt
new file mode 100644
index 0000000..6a36156
--- /dev/null
+++ b/libs/utils/src/test/java/org/the_jk/cleversync/StringUtilsTest.kt
@@ -0,0 +1,40 @@
+package org.the_jk.cleversync
+
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+
+class StringUtilsTest {
+ @Test
+ fun splitEmpty() {
+ assertThat(StringUtils.split("", '.', keepEmpty = true)).containsExactly("")
+ assertThat(StringUtils.split("", '.', keepEmpty = false)).isEmpty()
+ }
+
+ @Test
+ fun splitSanity() {
+ assertThat(StringUtils.split("a.bb.a", '.')).containsExactly("a", "bb", "a").inOrder()
+ assertThat(StringUtils.split(".a.bb.a", '.', keepEmpty = true)).containsExactly("", "a", "bb", "a").inOrder()
+ assertThat(StringUtils.split(".a.bb.a", '.', keepEmpty = false)).containsExactly("a", "bb", "a").inOrder()
+ assertThat(StringUtils.split(".a.bb.a.", '.', keepEmpty = true))
+ .containsExactly("", "a", "bb", "a", "").inOrder()
+ assertThat(StringUtils.split(".a.bb.a.", '.', keepEmpty = false)).containsExactly("a", "bb", "a").inOrder()
+ }
+
+ @Test
+ fun splitDouble() {
+ assertThat(StringUtils.split("foo..bar", '.', keepEmpty = true)).containsExactly("foo", "", "bar").inOrder()
+ assertThat(StringUtils.split("foo..bar", '.', keepEmpty = false)).containsExactly("foo", "bar").inOrder()
+ }
+
+ @Test
+ fun splitLimit() {
+ assertThat(StringUtils.split("a.bb.a", '.', limit = 1)).containsExactly("a.bb.a")
+ assertThat(StringUtils.split("a.bb.a", '.', limit = 2)).containsExactly("a", "bb.a").inOrder()
+ assertThat(StringUtils.split("a.bb.a", '.', limit = 3)).containsExactly("a", "bb", "a").inOrder()
+ assertThat(StringUtils.split("a.bb.a.", '.', limit = 3, keepEmpty = true))
+ .containsExactly("a", "bb", "a.").inOrder()
+ assertThat(StringUtils.split("a.bb.a.", '.', limit = 3, keepEmpty = false))
+ .containsExactly("a", "bb", "a").inOrder()
+ assertThat(StringUtils.split("a.bb.a", '.', limit = 1000)).containsExactly("a", "bb", "a").inOrder()
+ }
+}