You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.0 KiB
Kotlin
31 lines
1.0 KiB
Kotlin
package me.eater.hefbrug.platform_utils
|
|
|
|
import me.eater.hefbrug.access.AccessSkeleton
|
|
import me.eater.hefbrug.executor.ExecutionContext
|
|
import me.eater.hefbrug.logging.Logging
|
|
|
|
interface PlatformUtil<out M> {
|
|
suspend fun isSupported(access: AccessSkeleton): Boolean
|
|
suspend fun getManager(context: ExecutionContext): M
|
|
|
|
abstract class Provider<M: Any>(val name: String, vararg manager: PlatformUtil<M>) : Logging {
|
|
private val managers: MutableSet<PlatformUtil<M>> = mutableSetOf(*manager)
|
|
|
|
suspend fun getManager(context: ExecutionContext): M {
|
|
val manager = managers.find {
|
|
debug("Trying $name ${it.javaClass.name}")
|
|
it.isSupported(context.roAccess)
|
|
}?.getManager(context)
|
|
?: throw RuntimeException("Couldn't find suitable $name for this system")
|
|
|
|
debug("Selected $name ${manager.javaClass.name}")
|
|
|
|
return manager
|
|
}
|
|
|
|
|
|
fun register(packageManager: PlatformUtil<M>) {
|
|
managers.add(packageManager)
|
|
}
|
|
}
|
|
} |