Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# .editorconfig
root = true

[*.{kt,kts}]
end_of_line = lf
ij_kotlin_packages_to_use_import_on_demand = true
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_imports_layout = *
ij_kotlin_indent_before_arrow_on_new_line = false
ij_kotlin_line_break_after_multiline_when_entry = true
indent_size = 4
indent_style = space
insert_final_newline = true
ktlint_argument_list_wrapping_ignore_when_parameter_count_greater_or_equal_than = 8
ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than = 4
ktlint_code_style = android_studio
ktlint_enum_entry_name_casing = upper_or_camel_cases
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_function_signature_body_expression_wrapping = default
ktlint_ignore_back_ticked_identifier = false
max_line_length = 140
parameter-list-wrapping = true
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
- name: Cache gradle
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache konan
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.konan
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Run Gradle Build
run: ./gradlew build --no-daemon --stacktrace --no-build-cache
run: ./gradlew ktlintCheck build --no-daemon --stacktrace --no-build-cache
env:
ORG_GRADLE_PROJECT_SONATYPE_NEXUS_USERNAME: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
Expand All @@ -47,7 +47,7 @@ jobs:

- name: Upload Stately Collections Build Results
if: always()
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: deprecated-test-results
path: deprecated/**/build/reports/tests/
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
- name: Cache gradle
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache konan
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.konan
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
- name: Cache gradle
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Cache konan
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.konan
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
Expand Down
2 changes: 0 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
alias(libs.plugins.dokka) apply false
alias(libs.plugins.touchlab.docusaurusosstemplate)
alias(libs.plugins.mavenPublish) apply false
id("org.jlleitschuh.gradle.ktlint") version "12.2.0" apply false
}

apiValidation {
Expand All @@ -26,4 +27,23 @@ val VERSION_NAME: String by project
allprojects {
group = GROUP
version = VERSION_NAME
}

subprojects {
apply(plugin = "org.jlleitschuh.gradle.ktlint")

configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
version.set("1.4.0")
enableExperimentalRules.set(true)
verbose.set(true)
filter {
exclude { it.file.path.contains("build/") }
}
}

afterEvaluate {
tasks.named("check") {
dependsOn(tasks.getByName("ktlintCheck"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ expect fun <T> frozenCopyOnWriteList(collection: Collection<T>? = null): Mutable
message = "Replacing Atomic collections with isolated state collections (modele `stately-iso-collections`)",
replaceWith = ReplaceWith(
"sharedMutableListOf()",
"co.touchlab.stately.collections.sharedMutableListOf"
)
"co.touchlab.stately.collections.sharedMutableListOf",
),
)
fun <T> frozenLinkedList(stableIterator: Boolean = false): MutableList<T> = if (stableIterator) {
CopyOnIterateLinkedList()
Expand All @@ -40,8 +40,8 @@ fun <T> frozenLinkedList(stableIterator: Boolean = false): MutableList<T> = if (
message = "Replacing Atomic collections with isolated state collections (modele `stately-iso-collections`)",
replaceWith = ReplaceWith(
"sharedMutableMapOf()",
"co.touchlab.stately.collections.sharedMutableMapOf"
)
"co.touchlab.stately.collections.sharedMutableMapOf",
),
)
fun <K, V> frozenHashMap(initialCapacity: Int = 16, loadFactor: Float = 0.75.toFloat()): MutableMap<K, V> =
SharedHashMap(initialCapacity, loadFactor)
Expand All @@ -50,8 +50,8 @@ fun <K, V> frozenHashMap(initialCapacity: Int = 16, loadFactor: Float = 0.75.toF
message = "Replacing Atomic collections with isolated state collections (modele `stately-iso-collections`)",
replaceWith = ReplaceWith(
"sharedMutableSetOf()",
"co.touchlab.stately.collections.sharedMutableSetOf"
)
"co.touchlab.stately.collections.sharedMutableSetOf",
),
)
fun <T> frozenHashSet(): MutableSet<T> = SharedSet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import co.touchlab.stately.concurrency.value
import co.touchlab.stately.concurrency.withLock
import co.touchlab.stately.freeze

class ObjectPool<T>(
private val maxSize: Int,
private val createBlock: () -> T,
private val cleanupBlock: ((t: T) -> Unit)? = null
) {
class ObjectPool<T>(private val maxSize: Int, private val createBlock: () -> T, private val cleanupBlock: ((t: T) -> Unit)? = null) {
init {
if (maxSize < 0) {
throw IllegalArgumentException("maxSize cannot be negative")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
override val value: V
get() = v

override fun setValue(newValue: V): V {
throw UnsupportedOperationException()
}
override fun setValue(newValue: V): V = throw UnsupportedOperationException()
}

private var lock: Lock = Lock()
Expand All @@ -56,22 +54,21 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7

init {
var capacity = 1
while (capacity < initialCapacity)
while (capacity < initialCapacity) {
capacity = capacity shl 1
}

threshold = AtomicInt((capacity.toFloat() * loadFactor).toInt())
buckets = AtomicReference(makeBuckets(capacity))

freeze()
}

private fun makeBuckets(capacity: Int): Array<AtomicReference<SharedLinkedList<Entry<K, V>>>> {
return (
Array(capacity) {
AtomicReference(SharedLinkedList<Entry<K, V>>(1).freeze())
}
).freeze()
}
private fun makeBuckets(capacity: Int): Array<AtomicReference<SharedLinkedList<Entry<K, V>>>> = (
Array(capacity) {
AtomicReference(SharedLinkedList<Entry<K, V>>(1).freeze())
}
).freeze()

private inline fun iterInternal(proc: (Entry<K, V>) -> Unit) {
buckets.value.forEach {
Expand Down Expand Up @@ -115,31 +112,19 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
}

private class NotReallyMutableSet<T>(private val delegate: MutableCollection<T>) : MutableSet<T> {
override fun add(element: T): Boolean {
throw UnsupportedOperationException()
}
override fun add(element: T): Boolean = throw UnsupportedOperationException()

override fun addAll(elements: Collection<T>): Boolean {
throw UnsupportedOperationException()
}
override fun addAll(elements: Collection<T>): Boolean = throw UnsupportedOperationException()

override fun clear() {
throw UnsupportedOperationException()
}
override fun clear(): Unit = throw UnsupportedOperationException()

override fun iterator(): MutableIterator<T> = delegate.iterator()

override fun remove(element: T): Boolean {
throw UnsupportedOperationException()
}
override fun remove(element: T): Boolean = throw UnsupportedOperationException()

override fun removeAll(elements: Collection<T>): Boolean {
throw UnsupportedOperationException()
}
override fun removeAll(elements: Collection<T>): Boolean = throw UnsupportedOperationException()

override fun retainAll(elements: Collection<T>): Boolean {
throw UnsupportedOperationException()
}
override fun retainAll(elements: Collection<T>): Boolean = throw UnsupportedOperationException()

override val size: Int
get() = delegate.size
Expand Down Expand Up @@ -201,10 +186,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
return result
}

private fun internalRemoveByKey(
entryList: SharedLinkedList<Entry<K, V>>,
key: K
): V? {
private fun internalRemoveByKey(entryList: SharedLinkedList<Entry<K, V>>, key: K): V? {
var result: AbstractSharedLinkedList.Node<Entry<K, V>>? = null
val iter = entryList.nodeIterator()
while (iter.hasNext()) {
Expand Down Expand Up @@ -250,7 +232,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7

private fun transfer(
newTable: Array<AtomicReference<SharedLinkedList<Entry<K, V>>>>,
oldTable: Array<AtomicReference<SharedLinkedList<Entry<K, V>>>>
oldTable: Array<AtomicReference<SharedLinkedList<Entry<K, V>>>>,
) {
oldTable.forEach {
it.value.iterator().forEach {
Expand All @@ -262,9 +244,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7

internal fun currentBucketSize(): Int = buckets.value.size

private fun indexFor(h: Int, length: Int): Int {
return h and length - 1
}
private fun indexFor(h: Int, length: Int): Int = h and length - 1

internal fun rehash(initHash: Int): Int {
var h = initHash
Expand All @@ -275,10 +255,7 @@ class SharedHashMap<K, V>(initialCapacity: Int = 16, val loadFactor: Float = 0.7
return h xor h.ushr(7) xor h.ushr(4)
}

private fun findEntryList(
bucketArray: Array<AtomicReference<SharedLinkedList<Entry<K, V>>>>,
key: K
): SharedLinkedList<Entry<K, V>> {
private fun findEntryList(bucketArray: Array<AtomicReference<SharedLinkedList<Entry<K, V>>>>, key: K): SharedLinkedList<Entry<K, V>> {
val hash = rehash(key.hashCode())
val entryList = bucketArray.get(indexFor(hash, bucketArray.size)).value
return entryList
Expand Down
Loading
Loading