Package com.calebleavell.jatui.templates
Class InputHandler
java.lang.Object
com.calebleavell.jatui.modules.TUIModule.Builder<InputHandler>
com.calebleavell.jatui.modules.ModuleTemplate<InputHandler>
com.calebleavell.jatui.templates.InputHandler
- All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, InputHandler>
Handles management of the application state by running logic
on saved inputs from the application.
Example Usage:
Example Usage:
InputHandler handler = InputHandler.builder("handler-name", "input_identifier")
.setHandler("return_value_identifier",
// run main logic on input
s -> {
if(!s.equals("valid_input")) throw new RuntimeException("invalid input!!");
else return 10;
},
// handle the RuntimeException thrown above (optional)
s -> {
app.restart();
})
Note: You will likely want to use the wrapper methods provided by
TextInputModule.Builder (e.g, TextInputModule.Builder.addSafeHandler(String, Function, String))
if interfacing with user input. This class should be
used if more fine-grained control is required, or if implementing a custom ModuleTemplate
that works closely with application state.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enumSpecifies how to build the handler.Nested classes/interfaces inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
DirectedGraphNode.PropertyUpdateFlag -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected InputHandler.HandlerTypeThe type of the handler (as provided byInputHandler.HandlerType)protected StringThe name of the app state to readFields inherited from class com.calebleavell.jatui.modules.ModuleTemplate
mainFields inherited from class com.calebleavell.jatui.modules.TUIModule.Builder
ansi, application, children, enableAnsi, logger, name, printStream, propertyUpdateFlags, scanner, type, usedNames -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds a newInputHandlerbased on this configuration of this builder.static InputHandlerConstructs a newInputHandlerbuilder.protected InputHandlerGets a fresh instance of this type of Builder.exceptionHandlerhandles instances ofRuntimeExceptionthrown bylogicifhandlerTypeisInputHandler.HandlerType.SAFE_HANDLER.inputNameis the name of the app state to read.getLogic()logicis the logic that runs on input ifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.protected FunctionModule.Buildermoduleis the name of theFunctionModuleto create and thus the identifier of the returned value forlogicifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.protected StringmoduleNameis the name of theFunctionModuleto create and thus the identifier of the returned value forlogicifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.handler(FunctionModule.Builder handler) Configure the logic for thisInputHandler.Configure the logic for thisInputHandler.Configure the logic for thisInputHandler.inputNameis the name of the app state to read.protected voidshallowCopy(InputHandler original) CopiesinputName,handlerType,module,logic,exceptionHandler, andmoduleName, and delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).booleanshallowStructuralEquals(InputHandler first, InputHandler second) Checks equality for properties given by the builder.Methods inherited from class com.calebleavell.jatui.modules.ModuleTemplate
deepCopyMethods inherited from class com.calebleavell.jatui.modules.TUIModule.Builder
addChild, addChild, addChildren, addChildren, appendStyle, application, clearChildren, deepCopy, enableAnsi, getAnsi, getAnsiEnabled, getApplication, getChild, getChild, getChildren, getCopy, getDeepCopy, getName, getPrintStream, getPropertyUpdateFlags, getScanner, getType, lockProperty, name, prependStyle, prependToName, printStream, scanner, self, structuralEquals, style, toString, unlockProperty, updateFlag, updatePropertiesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
abstractSelf, containsNullNode, containsNullNode, dfs, dfs, forEach, forEachChild, structuralEquals, structuralEquals, updateProperty, updateProperty
-
Field Details
-
inputName
The name of the app state to read -
handlerType
The type of the handler (as provided byInputHandler.HandlerType)
-
-
Constructor Details
-
InputHandler
-
InputHandler
protected InputHandler()
-
-
Method Details
-
builder
Constructs a newInputHandlerbuilder.- Parameters:
name- The name of the builder.inputName- The name of the app state to read.- Returns:
- The new builder.
-
createInstance
Gets a fresh instance of this type of Builder. Note, this is intended only for copying utility and may have unknown consequences if used in other ways.- Specified by:
createInstancein classTUIModule.Builder<InputHandler>- Returns:
- A fresh, empty instance.
-
shallowCopy
CopiesinputName,handlerType,module,logic,exceptionHandler, andmoduleName, and delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).- Overrides:
shallowCopyin classTUIModule.Builder<InputHandler>- Parameters:
original- The builder to copy from.
-
getInputName
inputNameis the name of the app state to read.- Returns:
inputName.
-
inputName
inputNameis the name of the app state to read.- Returns:
- self.
-
getModule
moduleis the name of theFunctionModuleto create and thus the identifier of the returned value forlogicifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.- Returns:
module.
-
getLogic
logicis the logic that runs on input ifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.- Returns:
logic.
-
getExceptionHandler
exceptionHandlerhandles instances ofRuntimeExceptionthrown bylogicifhandlerTypeisInputHandler.HandlerType.SAFE_HANDLER.- Returns:
exceptionHandler.
-
getModuleName
moduleNameis the name of theFunctionModuleto create and thus the identifier of the returned value forlogicifhandlerTypeisInputHandler.HandlerType.HANDLERorInputHandler.HandlerType.SAFE_HANDLER.- Returns:
moduleName.
-
handler
Configure the logic for thisInputHandler. This overload does so via aFunctionModule, but logic can be directly inputted viahandler(String, Function)or similar. The app input is not injected intohandlerfor this overload and must be handled manually.
Note thathandlerdoes not get added as a child until the module is built, so property updates (e.g.,TUIModule.Builder.application(ApplicationModule)) won't propagate to it from this module.- Parameters:
handler- The module defining the logic for the handler.- Returns:
- self
-
handler
Configure the logic for thisInputHandler. The app input is injected into tologicand updated atname.- Parameters:
name- The name of theFunctionModulethat will be built, and thus the input identifier forApplicationModule.getInput(String)for whatlogicreturns.logic- TheFunctionthat runs on the input collected fromApplicationModule.getInput(String, Class)and returns some value with the identifier provided byname.- Returns:
- self
-
handler
public InputHandler handler(String name, Function<String, ?> logic, Consumer<String> exceptionHandler) Configure the logic for thisInputHandler. The app input is injected into tologicand updated atname.- Parameters:
name- The name of theFunctionModulethat will be built, and thus the input identifier forApplicationModule.getInput(String)for whatlogicreturns.logic- TheFunctionthat runs on the input collected fromApplicationModule.getInput(String, Class)and returns some value with the identifier provided byname.exceptionHandler- Fallback logic if aRuntimeExceptionis thrown during execution oflogic.- Returns:
- self
-
shallowStructuralEquals
Checks equality for properties given by the builder. ForInputHandler, this includesinputName,handlerType, andmoduleName, as well as other requirements provided byTUIModule.structuralEquals(TUIModule).- Specified by:
shallowStructuralEqualsin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, InputHandler> - Overrides:
shallowStructuralEqualsin classTUIModule.Builder<InputHandler>- Parameters:
first- The first TUIModule to comparesecond- The second TUIModule to compare- Returns:
trueiffirstandsecondare equal according to builder-provided properties
-
build
Builds a newInputHandlerbased on this configuration of this builder. Adds the handling logic based on how it was set, e.g., viahandler(String, Function, Consumer).- Overrides:
buildin classModuleTemplate<InputHandler>- Returns:
- The built
InputHandler. - Implementation Note:
- Clears the children first to ensure consistency when building multiple times.
-