Package com.calebleavell.jatui.modules
Class TextInputModule.Builder
java.lang.Object
com.calebleavell.jatui.modules.TUIModule.Builder<TextInputModule.Builder>
com.calebleavell.jatui.modules.TextInputModule.Builder
- All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, TextInputModule.Builder>
- Enclosing class:
TextInputModule
- Implementation Note:
- The names for the
InputHandlerobjects monotonically increase based on an iteratorhandlerNum, so the name format follows"<this.name>-<this.handlerNum>".
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
DirectedGraphNode.PropertyUpdateFlag -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected TextModule.BuilderTheTextModulethat displays text for getting input (e.g., "Your Input: ").protected ContainerModule.BuilderThe container for theInputHandlerobjects that will operate on the input collected by this module.Fields 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 TypeMethodDescriptionaddHandler(FunctionModule.Builder handler) Adds a function module to execute after input is collected.addHandler(String name, Function<String, ?> logic) Adds anInputHandlerbased on the provided logic.addSafeHandler(String name, Function<String, ?> logic) Adds anInputHandlerbased on the provided logic.addSafeHandler(String name, Function<String, ?> logic, String exceptionMessage) Adds anInputHandlerbased on the provided logic.Adds anInputHandlerbased on the provided logic.build()Builds a newTextInputModulebased on the configuration of this builder.protected TextInputModule.BuilderGets a fresh instance of this type of Builder.protected TextInputModule.BuilderdeepCopy(TextInputModule.Builder original, Map<TUIModule.Builder<?>, TUIModule.Builder<?>> visited) In order to maintain the reference to the new children, access to thevisitedmap is used, and thusTUIModule.Builder.deepCopy(TUIModule.Builder, Map)needs to be overridden.TheTextModulethat displays text for getting input (e.g., "Your Input: ").Sets the name of the module.protected voidshallowCopy(TextInputModule.Builder original) CopieshandlerNumand delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).booleanChecks equality for properties given by the builder.Methods 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, 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
-
handlers
The container for theInputHandlerobjects that will operate on the input collected by this module. -
displayText
TheTextModulethat displays text for getting input (e.g., "Your Input: ").
-
-
Constructor Details
-
Builder
-
Builder
protected Builder()
-
-
Method Details
-
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<TextInputModule.Builder>- Returns:
- A fresh, empty instance.
-
shallowCopy
CopieshandlerNumand delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).- Overrides:
shallowCopyin classTUIModule.Builder<TextInputModule.Builder>- Parameters:
original- The builder to copy from
-
deepCopy
protected TextInputModule.Builder deepCopy(TextInputModule.Builder original, Map<TUIModule.Builder<?>, TUIModule.Builder<?>> visited) In order to maintain the reference to the new children, access to thevisitedmap is used, and thusTUIModule.Builder.deepCopy(TUIModule.Builder, Map)needs to be overridden.- Overrides:
deepCopyin classTUIModule.Builder<TextInputModule.Builder>- Parameters:
original- The module to copy from.visited- All children that have already been deep-copied.- Returns:
- The instance that was copied into (self if
originalhasn't been visited yet).
-
shallowStructuralEquals
public boolean shallowStructuralEquals(TextInputModule.Builder first, TextInputModule.Builder second) Checks equality for properties given by the builder. ForTextInputModule, this includesdisplayText, as well as other requirements provided byTUIModule.Builder.shallowStructuralEquals(TUIModule.Builder, TUIModule.Builder).- Specified by:
shallowStructuralEqualsin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, TextInputModule.Builder> - Overrides:
shallowStructuralEqualsin classTUIModule.Builder<TextInputModule.Builder>- Parameters:
first- The first TUIModule to comparesecond- The second TUIModule to compare- Returns:
trueiffirstandsecondare equal according to builder-provided properties
-
name
Sets the name of the module. Also updates the input name to check for any declaredInputHandlerobjects attached viaaddHandler(FunctionModule.Builder)or similar.- Overrides:
namein classTUIModule.Builder<TextInputModule.Builder>- Parameters:
name- The unique name of this module.- Returns:
- self
-
getDisplayText
TheTextModulethat displays text for getting input (e.g., "Your Input: ").- Returns:
- The display text for this module.
-
addHandler
Adds a function module to execute after input is collected.
Note: this module will not update application or other properties forhandler; this must be done manually. This is becausehandlergets wrapped in anInputHandler.- Parameters:
handler- The Function Module builder to execute after input is collected- Returns:
- self
-
addHandler
Adds anInputHandlerbased on the provided logic.- Parameters:
name- The name of the function module that theInputHandlerwill wrap. This means whateverlogicreturns can be accessed viaapp.getInput(<name>)or equivalent.logic- TheFunctionto execute, that receives the input the builtTextInputModulecollects and outputs some object that is saved to the application withnameas the identifier.- Returns:
- self
-
addSafeHandler
public <T> TextInputModule.Builder addSafeHandler(String name, Function<String, T> logic, Consumer<String> exceptionHandler) Adds anInputHandlerbased on the provided logic. If aRuntimeExceptionis thrown during the execution oflogic, it is caught andexceptionHandlercan perform some recovery action. If you just want to print some error message and rerun the input, useaddSafeHandler(String, Function, String)oraddSafeHandler(String, Function)for a default message (TextInputModule.INVALID).- Parameters:
name- The name of the function module that theInputHandlerwill wrap. This means whatever thislogicreturns can be accessed viaapp.getInput(<name>)or equivalent.logic- TheFunctionto execute, that receives the input the builtTextInputModulecollects and outputs some object that is saved to the application withnameas the identifier.exceptionHandler- The recovery mechanism iflogicthrows aRuntimeException.- Returns:
- self
-
addSafeHandler
public TextInputModule.Builder addSafeHandler(String name, Function<String, ?> logic, String exceptionMessage) Adds anInputHandlerbased on the provided logic. If aRuntimeExceptionis thrown during the execution oflogic, it is caught and the input is recollected. UseaddSafeHandler(String, Function)for a default message (TextInputModule.INVALID).- Parameters:
name- The name of the function module that theInputHandlerwill wrap. This means whatever thislogicreturns can be accessed viaapp.getInput(<name>)or equivalent.logic- TheFunctionto execute, that receives the input the builtTextInputModulecollects and outputs some object that is saved to the application withnameas the identifier.exceptionMessage- The message to display iflogicthrows aRuntimeException, before the input gets recollected.- Returns:
- self
-
addSafeHandler
Adds anInputHandlerbased on the provided logic. If aRuntimeExceptionis thrown during the execution oflogic, it is caught and the input is recollected. The message provided byTextInputModule.INVALIDis shown before rerunning. For a custom invalid message, useaddSafeHandler(String, Function, String).- Parameters:
name- The name of the function module that theInputHandlerwill wrap. This means whatever thislogicreturns can be accessed viaapp.getInput(<name>)or equivalent.logic- TheFunctionto execute, that receives the input the builtTextInputModulecollects and outputs some object that is saved to the application withnameas the identifier.- Returns:
- self
-
build
Builds a newTextInputModulebased on the configuration of this builder.- Specified by:
buildin classTUIModule.Builder<TextInputModule.Builder>- Returns:
- The new
TextInputModule. - Implementation Note:
- Removes and re-adds
displayTextandhandlersto the children in order to allowdisplayTextto a be a field of the built module, and forhandlersto be at the end.
-