55 lines
1.4 KiB
Kotlin
55 lines
1.4 KiB
Kotlin
package audio
|
|
|
|
/**
|
|
* Cache for audio content to avoid reloading from disk
|
|
*/
|
|
@Suppress("unused")
|
|
class ContentCache {
|
|
|
|
private val map: MutableMap<String, AudioFileInfo> = HashMap()
|
|
|
|
/**
|
|
* Clear the cache, but keep essential sounds : chimeup, chimedown, silence1s, silencehalf
|
|
*/
|
|
fun clear(){
|
|
// dont clear chimeup, chimedown, silence1s, silencehalf
|
|
val keysToKeep = setOf("chimeup", "chimedown", "silence1s", "silencehalf")
|
|
map.keys.retainAll(keysToKeep)
|
|
|
|
}
|
|
|
|
/**
|
|
* Add an audio file to the cache
|
|
* @param key The key to identify the audio file
|
|
* @param audioFile The AudioFileInfo object
|
|
*/
|
|
fun addAudioFile(key: String, audioFile: AudioFileInfo) {
|
|
map[key] = audioFile
|
|
}
|
|
|
|
/**
|
|
* Retrieve an audio file from the cache
|
|
* @param key The key to identify the audio file
|
|
* @return The AudioFileInfo object, or null if not found
|
|
*/
|
|
fun getAudioFile(key: String): AudioFileInfo? {
|
|
return map[key]
|
|
}
|
|
|
|
/**
|
|
* Remove an audio file from the cache
|
|
* @param key The key to identify the audio file
|
|
*/
|
|
fun removeAudioFile(key: String) {
|
|
map.remove(key)
|
|
}
|
|
|
|
/**
|
|
* Check if the cache contains the specified key
|
|
* @param key The key to check in the cache
|
|
* @return True if the key exists, false otherwise
|
|
*/
|
|
fun haveKey(key: String): Boolean {
|
|
return map.containsKey(key)
|
|
}
|
|
} |