diff --git a/multitask/src/main/java/dev/aether/collaborative_multitasking/Task.kt b/multitask/src/main/java/dev/aether/collaborative_multitasking/Task.kt index 50f4ecc132d7..cd603ff97677 100644 --- a/multitask/src/main/java/dev/aether/collaborative_multitasking/Task.kt +++ b/multitask/src/main/java/dev/aether/collaborative_multitasking/Task.kt @@ -38,9 +38,15 @@ state Task { typealias TaskQuery2 = (task: Task, scheduler: Scheduler) -> T typealias TaskQuery1 = (task: Task) -> T typealias Producer = () -> T +typealias JTaskQuery2 = java.util.function.BiFunction +typealias JTaskQuery1 = java.util.function.Function +typealias JProducer = java.util.function.Supplier typealias TaskAction2 = TaskQuery2 typealias TaskAction1 = TaskQuery1 typealias Runnable = () -> Unit +typealias JTaskAction2 = java.util.function.BiConsumer +typealias JTaskAction1 = java.util.function.Consumer +typealias JRunnable = java.lang.Runnable class Task constructor( internal var scheduler: Scheduler, @@ -98,6 +104,18 @@ class Task constructor( canStart = { _: Task, _: Scheduler -> block() } } + fun canStart(block: JTaskQuery2) { + canStart = { that: Task, scheduler2: Scheduler -> block.apply(that, scheduler2) } + } + + fun canStart(block: JTaskQuery1) { + canStart = { that: Task, _: Scheduler -> block.apply(that) } + } + + fun canStart(block: JProducer) { + canStart = { _: Task, _: Scheduler -> block.get() } + } + fun invokeCanStart(): Boolean { return canStart(this, scheduler) } @@ -114,6 +132,18 @@ class Task constructor( onStart = { _: Task, _: Scheduler -> block() } } + fun onStart(block: JTaskAction2) { + onStart = { that: Task, scheduler2: Scheduler -> block.accept(that, scheduler2) } + } + + fun onStart(block: JTaskAction1) { + onStart = { that: Task, _: Scheduler -> block.accept(that) } + } + + fun onStart(block: JRunnable) { + onStart = { _: Task, _: Scheduler -> block.run() } + } + fun invokeOnStart() { onStart(this, scheduler) } @@ -130,6 +160,18 @@ class Task constructor( onTick = { _: Task, _: Scheduler -> block() } } + fun onTick(block: JTaskAction2) { + onTick = { that: Task, scheduler2: Scheduler -> block.accept(that, scheduler2) } + } + + fun onTick(block: JTaskAction1) { + onTick = { that: Task, _: Scheduler -> block.accept(that) } + } + + fun onTick(block: JRunnable) { + onTick = { _: Task, _: Scheduler -> block.run() } + } + fun invokeOnTick() { onTick(this, scheduler) } @@ -146,6 +188,18 @@ class Task constructor( isCompleted = { _: Task, _: Scheduler -> block() } } + fun isCompleted(block: JTaskQuery2) { + isCompleted = { that: Task, scheduler2: Scheduler -> block.apply(that, scheduler2) } + } + + fun isCompleted(block: JTaskQuery1) { + isCompleted = { that: Task, _: Scheduler -> block.apply(that) } + } + + fun isCompleted(block: JProducer) { + isCompleted = { _: Task, _: Scheduler -> block.get() } + } + fun invokeIsCompleted(): Boolean { return isCompleted(this, scheduler) } @@ -162,6 +216,18 @@ class Task constructor( onFinish = { _: Task, _: Scheduler -> block() } } + fun onFinish(block: JTaskAction2) { + onFinish = { that: Task, scheduler2: Scheduler -> block.accept(that, scheduler2) } + } + + fun onFinish(block: JTaskAction1) { + onFinish = { that: Task, _: Scheduler -> block.accept(that) } + } + + fun onFinish(block: JRunnable) { + onFinish = { _: Task, _: Scheduler -> block.run() } + } + fun invokeOnFinish() { onFinish(this, scheduler) }