-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ActionBuilder v2
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
ftc/src/main/kotlin/page/j5155/expressway/ftc/actions/ActionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package page.j5155.expressway.ftc.actions | ||
|
||
import com.acmerobotics.dashboard.telemetry.TelemetryPacket | ||
import page.j5155.expressway.core.actions.Condition | ||
import page.j5155.expressway.core.actions.InitLoopCondAction | ||
import java.util.function.Consumer | ||
import java.util.function.Supplier | ||
|
||
/** | ||
* This class builds an InitLoopCondAction | ||
* @param condition the condition for the action to complete | ||
*/ | ||
class ActionBuilder(val condition: Condition) { | ||
|
||
private var initInternal: Supplier<Unit> = Supplier { } | ||
private var loopInternal: Consumer<TelemetryPacket> = Consumer { } | ||
private var cleanupInternal: Supplier<Unit> = Supplier { } | ||
|
||
/** | ||
* Set the initialization function | ||
* @param init the initialization function | ||
*/ | ||
fun setInit(init: Supplier<Unit>): ActionBuilder { | ||
initInternal = init | ||
return this | ||
} | ||
|
||
/** | ||
* Set the loop function | ||
* @param loop the loop function | ||
*/ | ||
fun setLoop(loop: Consumer<TelemetryPacket>): ActionBuilder { | ||
loopInternal = loop | ||
return this | ||
} | ||
|
||
/** | ||
* Set the cleanup function | ||
* @param cleanup the cleanup function | ||
*/ | ||
fun setCleanup(cleanup: Supplier<Unit>): ActionBuilder { | ||
cleanupInternal = cleanup | ||
return this | ||
} | ||
|
||
/** | ||
* Builds the InitLoopCondAction | ||
*/ | ||
fun build(): InitLoopCondAction { | ||
return object: InitLoopCondAction(condition) { | ||
override fun init() = initInternal.get() | ||
|
||
override fun loop(p: TelemetryPacket) = loopInternal.accept(p) | ||
|
||
override fun cleanup() = cleanupInternal.get() | ||
} | ||
} | ||
} |