Skip to content

Commit

Permalink
refactor: use CopyOnWriteArrayList (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Dec 7, 2024
1 parent be9d821 commit 56e20c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.zyrouge.symphony.services.radio

import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.utils.ConcurrentList
import io.github.zyrouge.symphony.utils.concurrentListOf

class RadioQueue(private val symphony: Symphony) {
enum class LoopMode {
Expand All @@ -14,10 +14,8 @@ class RadioQueue(private val symphony: Symphony) {
}
}

val originalQueue = ConcurrentList<String>()
private set
val currentQueue = ConcurrentList<String>()
private set
val originalQueue = concurrentListOf<String>()
val currentQueue = concurrentListOf<String>()

var currentSongIndex = -1
private set(value) {
Expand Down
40 changes: 2 additions & 38 deletions app/src/main/java/io/github/zyrouge/symphony/utils/List.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.zyrouge.symphony.utils

import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.math.max
import kotlin.math.min
import kotlin.random.Random
Expand All @@ -27,38 +25,4 @@ fun <T> List<T>.mutate(fn: MutableList<T>.() -> Unit): List<T> {
return out
}

class ConcurrentList<T> : MutableList<T> {
private val list = mutableListOf<T>()
private val lock = ReentrantReadWriteLock()

override val size: Int get() = lock.read { list.size }

override operator fun set(index: Int, element: T) = lock.write { list.set(index, element) }
override operator fun get(index: Int) = lock.read { list[index] }

override fun contains(element: T) = lock.read { list.contains(element) }
override fun containsAll(elements: Collection<T>) = lock.read { list.containsAll(elements) }
override fun indexOf(element: T) = lock.read { list.indexOf(element) }
override fun lastIndexOf(element: T) = lock.read { list.lastIndexOf(element) }
override fun isEmpty() = lock.read { list.isEmpty() }
override fun subList(fromIndex: Int, toIndex: Int) =
lock.read { list.subList(fromIndex, toIndex) }

override fun add(element: T) = lock.write { list.add(element) }
override fun add(index: Int, element: T) = lock.write { list.add(index, element) }
override fun addAll(elements: Collection<T>) = lock.write { list.addAll(elements) }
override fun addAll(index: Int, elements: Collection<T>) =
lock.write { list.addAll(index, elements) }

override fun clear() = lock.write { list.clear() }
override fun remove(element: T) = lock.write { list.remove(element) }
override fun removeAll(elements: Collection<T>) = lock.write { list.removeAll(elements) }
override fun removeAt(index: Int) = lock.write { list.removeAt(index) }
override fun retainAll(elements: Collection<T>) = lock.write { list.retainAll(elements) }

// NOTE: `write` lock since it returns `MutableIterator`s
override fun iterator() = lock.write { list.iterator() }
override fun listIterator() = lock.write { list.listIterator() }
override fun listIterator(index: Int) = lock.write { list.listIterator(index) }
}

fun <T> concurrentListOf(): MutableList<T> = CopyOnWriteArrayList(mutableListOf<T>())

0 comments on commit 56e20c9

Please sign in to comment.