Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
v0.3.0 release
  • Loading branch information
ShreckYe committed May 15, 2023
2 parents b886763 + ae8241d commit 5f4a007
Show file tree
Hide file tree
Showing 36 changed files with 1,199 additions and 149 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Huanshankeji Kotlin Common

[![Maven Central](https://img.shields.io/maven-central/v/com.huanshankeji/kotlin-common-core)](https://search.maven.org/search?q=g:com.huanshankeji%20a:kotlin-common-*)
![Kotlin version](https://kotlin-version.aws.icerock.dev/kotlin-version?group=com.huanshankeji&name=kotlin-common-core)

Huanshankeji's common code libraries in Kotlin

Expand Down
6 changes: 3 additions & 3 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
}

dependencies {
implementation(kotlin("gradle-plugin", "1.7.10"))
implementation("com.huanshankeji:common-gradle-dependencies:0.3.2-20220728")
implementation("com.huanshankeji.team:gradle-plugins:0.3.2")
implementation(kotlin("gradle-plugin", "1.8.10"))
implementation("com.huanshankeji:common-gradle-dependencies:0.5.0-20230310")
implementation("com.huanshankeji.team:gradle-plugins:0.4.0")
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/VersionsAndDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.huanshankeji.CommonDependencies
import com.huanshankeji.CommonGradleClasspathDependencies
import com.huanshankeji.CommonVersions

val projectVersion = "0.2.4"
val projectVersion = "0.3.0"

val commonVersions = CommonVersions()
val commonDependencies = CommonDependencies(commonVersions)
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/jvm-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ plugins {
id("com.huanshankeji.kotlin-jvm-library-sonatype-ossrh-publish-conventions")
id("common-conventions-after")
}

kotlin.jvmToolchain(8)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
plugins {
id("common-conventions-before")
id("com.huanshankeji.kotlin-multiplatform-jvm-and-js-browser-sonatype-ossrh-publish-conventions")
id("com.huanshankeji.kotlin-multiplatform-jvm-and-js-browser-conventions")
id("com.huanshankeji.kotlin-multiplatform-sonatype-ossrh-publish-conventions")
id("common-conventions-after")
}

kotlin.jvmToolchain(8)
20 changes: 20 additions & 0 deletions core/src/commonMain/kotlin/com/huanshankeji/Untested.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.huanshankeji

import kotlin.annotation.AnnotationTarget.*

@RequiresOptIn("This API hasn't been tested yet. Use at your own risk.", RequiresOptIn.Level.WARNING)
@Retention(AnnotationRetention.BINARY)
// The ones commented out are what I think may be used in very few use cases.
@Target(
CLASS,
//ANNOTATION_CLASS,
PROPERTY,
//FIELD,
//VALUE_PARAMETER,
CONSTRUCTOR,
FUNCTION,
PROPERTY_GETTER,
PROPERTY_SETTER,
//TYPEALIAS
)
annotation class Untested
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.huanshankeji.collections

fun List<*>.areElementsDistinct() =
// There is a more efficient implementation.
distinct().size == size

fun <T> List<T>.eachCount() =
groupBy { it }.mapValues { it.value.size }
// This is tested to be faster than the implementation below in some scenarios.
//groupingBy { it }.eachCount()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.huanshankeji.collections

class LexicographicOrderListComparable<E : Comparable<E>> internal constructor(val list: List<E>) :
Comparable<LexicographicOrderListComparable<E>> {
override fun compareTo(other: LexicographicOrderListComparable<E>): Int {
val otherList = other.list
tailrec fun helper(index: Int): Int {
val a = list.getOrNull(index)
val b = otherList.getOrNull(index)
return if (a === null)
if (b === null) 0
else -1
else
if (b === null) 1
else {
val elementCompareResult = a.compareTo(b)
if (elementCompareResult != 0) elementCompareResult
else helper(index + 1)
}
}

return helper(0)
}
}

fun <E : Comparable<E>> List<E>.lexicographicOrderComparable() =
LexicographicOrderListComparable(this)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.huanshankeji.collections

import io.kotest.property.Arb
import io.kotest.property.arbitrary.pair
import io.kotest.property.arbitrary.string
import io.kotest.property.checkAll
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlin.math.sign
import kotlin.test.Test
import kotlin.test.assertEquals

class LexicographicOrderListComparableTest {
@Test
@OptIn(ExperimentalCoroutinesApi::class)
fun testLexicographicOrderListComparable() = runTest {
val stringArb = Arb.string()
checkAll(Arb.pair(stringArb, stringArb)) { (a, b) ->
assertEquals(
(a compareTo b).sign,
(a.toCharArray().asList().lexicographicOrderComparable() compareTo
b.toCharArray().asList().lexicographicOrderComparable()).sign
)
}
}
}

This file was deleted.

12 changes: 10 additions & 2 deletions coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import com.huanshankeji.cpnProject

plugins {
id("multiplatform-conventions")
}

kotlin {
sourceSets {
val commonMain by getting {
commonMain {
dependencies {
implementation(commonDependencies.kotlinx.coroutines.core())
implementation(cpnProject(project, ":core"))
}
}
commonTest {
dependencies {
implementation(kotlin("test"))
implementation(commonDependencies.kotlinx.coroutines.test())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.huanshankeji.kotlinx.coroutine

import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.selects.select

suspend fun <T> awaitAny(vararg deferreds: Deferred<T>): T {
require(deferreds.isNotEmpty())
return select { deferreds.forEach { it.onAwait { it } } }
}

suspend fun <T> Collection<Deferred<T>>.awaitAny(): T {
require(isNotEmpty())
return select { forEach { it.onAwait { it } } }
}

suspend fun <T> Collection<Deferred<T>>.awaitAnyAndCancelOthers(): T {
require(isNotEmpty())
val firstAwaited = select { forEachIndexed { index, deferred -> deferred.onAwait { IndexedValue(index, it) } } }
val firstAwaitedIndex = firstAwaited.index
forEachIndexed { index, deferred -> if (index != firstAwaitedIndex) deferred.cancel() }
return firstAwaited.value
}

suspend fun joinAny(vararg jobs: Job) {
require(jobs.isNotEmpty())
select { jobs.forEach { it.onJoin { } } }
}

suspend fun Collection<Job>.joinAny() {
require(isNotEmpty())
select { forEach { it.onJoin { } } }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.huanshankeji.kotlinx.coroutine

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.test.runTest
import kotlin.test.*

@OptIn(ExperimentalCoroutinesApi::class)
class AwaitTest {
@Test
fun simplyTestAwaitAnyAndJoinAny() = runTest {
val mutex = Mutex()
mutex.withLock {
fun asyncOf(value: Int) = async { value }
fun asyncSuspendUtilEnd() = async { mutex.withLock { } }

fun deferredArray0() = arrayOf(asyncOf(0), asyncSuspendUtilEnd())
fun deferredArray1() = arrayOf(asyncSuspendUtilEnd(), asyncOf(1))
assertEquals(0, awaitAny(*deferredArray0()))
assertEquals(1, awaitAny(*deferredArray1()))
assertEquals(0, deferredArray0().asList().awaitAny())
assertEquals(1, deferredArray1().asList().awaitAny())


val deffereds = deferredArray0()
assertFalse(deffereds[1].isCancelled)
assertEquals(0, deffereds.asList().awaitAnyAndCancelOthers())
assertTrue(deffereds[1].isCancelled)


var value: Int?

fun launchSetOf(newValue: Int) = launch { value = newValue }
fun launchSuspendUtilEndAndSetOf(newValue: Int) = launch {
mutex.withLock { }
value = newValue
}

fun jobArray0() = arrayOf(launchSetOf(0), launchSuspendUtilEndAndSetOf(1))
fun jobArray1() = arrayOf(launchSuspendUtilEndAndSetOf(0), launchSetOf(1))

value = null
joinAny(*jobArray0())
assertEquals(0, value)

value = null
joinAny(*jobArray1())
assertEquals(1, value)

value = null
jobArray0().asList().joinAny()
assertEquals(0, value)

value = null
jobArray1().asList().joinAny()
assertEquals(1, value)
}
}

private inline fun assertFailsWithIae(block: () -> Unit) =
assertFailsWith<IllegalArgumentException>(block = block)

@Test
fun simplyTestAwaitAnyAndJoinAnyWithNoTarget() = runTest {
val emptyList = emptyList<Nothing>()
assertFailsWithIae { awaitAny() }
assertFailsWithIae { emptyList.awaitAny() }
assertFailsWithIae { emptyList.awaitAnyAndCancelOthers() }
assertFailsWithIae { joinAny() }
assertFailsWithIae { emptyList.joinAny() }
}
}
4 changes: 4 additions & 0 deletions exposed/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import com.huanshankeji.cpnProject

plugins {
id("jvm-conventions")
}

dependencies {
implementation(commonDependencies.exposed.core())

implementation(cpnProject(project, ":core"))
}
93 changes: 0 additions & 93 deletions exposed/src/main/kotlin/com/huanshankeji/exposed/SimpleOrm.kt

This file was deleted.

Loading

0 comments on commit 5f4a007

Please sign in to comment.