com.github.bjoernpetersen.jmusicbot.playback.AbstractPlayback.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of musicbot Show documentation
Show all versions of musicbot Show documentation
Core library of MusicBot, which plays music from various providers.
package com.github.bjoernpetersen.jmusicbot.playback
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
/**
* Abstract Playback implementation providing a [lock] and an associated [done] condition, as well as
* an implementation for the [waitForFinish] method and a [markDone] method.
*/
abstract class AbstractPlayback private constructor(
/**
* A lock which will be used for critical code.
*/
protected val lock: Lock,
/**
* A condition which will come true when this Playback finishes.
*/
protected val done: Condition,
private val _isDone: AtomicBoolean) : Playback {
protected constructor() : this(ReentrantLock())
private constructor(lock: Lock) : this(lock, lock.newCondition(), AtomicBoolean())
private var playbackListener: PlaybackStateListener? = null
override fun setPlaybackStateListener(listener: PlaybackStateListener) {
playbackListener = listener
}
protected fun getPlaybackStateListener(): Optional =
Optional.ofNullable(playbackListener)
protected fun isDone(): Boolean = _isDone.get()
/**
* Waits for the [done] condition.
* @throws InterruptedException if the thread is interrupted while waiting
*/
@Throws(InterruptedException::class)
override fun waitForFinish() {
lock.withLock {
while (!isDone()) {
done.await()
}
}
}
/**
* Signals all threads waiting for the [done] condition.
*/
protected fun markDone() {
lock.withLock {
_isDone.set(true)
done.signalAll()
}
}
@Throws(Exception::class)
override fun close() {
markDone()
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy