Package com.calebleavell.jatui.modules
Class TUIModule.Builder<B extends TUIModule.Builder<B>>
java.lang.Object
com.calebleavell.jatui.modules.TUIModule.Builder<B>
- All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B>
- Direct Known Subclasses:
ApplicationModule.Builder,ContainerModule.Builder,FunctionModule.Builder,ModuleTemplate,TextInputModule.Builder,TextModule.Builder
- Enclosing class:
TUIModule
public abstract static class TUIModule.Builder<B extends TUIModule.Builder<B>>
extends Object
implements DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>,B>
Builder for
Note: Module builders are lazily realized by the scheduler at runtime. Calling build() is generally not required (with the rare exception of cases like building an
Required fields:
Optional fields:
Utility fields (not set by user):
TUIModule.
The builder uses a Curiously Recurring Template Pattern,
which means Builders which extend this can use the fluent method
chains provided by this builder while retaining their type.
Note: Module builders are lazily realized by the scheduler at runtime. Calling build() is generally not required (with the rare exception of cases like building an
ApplicationModule first).
Required fields:
type, name Optional fields:
children, propertyUpdateFlags, application,
ansi, scanner, printStream, enableAnsi Utility fields (not set by user):
logger, usedNames-
Nested Class Summary
Nested classes/interfaces inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
DirectedGraphNode.PropertyUpdateFlag -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.fusesource.jansi.AnsiThe ansi that may be displayed (Jansi object).protected ApplicationModuleThe application this module is tied to.protected List<TUIModule.Builder<?>> Every child module that should be run.protected booleanWhether ansi will be displayed or not.protected static final org.slf4j.LoggerThe Logger for the module, provided by the slf4j facadeprotected StringThe identifier for this module.
It is highly recommended to try and keep this unique in order to allow identification methods (e.g., viaApplicationModule.getInput(java.lang.String)) to function properly.protected PrintStreamPrintStream that outputs data to the defined location.Specifies how property propagation functions forapplication,ansi,scanner,printStream, andenableAnsi.protected ScannerThe Scanner that reads input from the defined source.The class extending this class (CRTP).The frequency of names of all children of this module. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs an emptyTUIModule.Builder.Constructs a newTUIModule.Builder. -
Method Summary
Modifier and TypeMethodDescriptionaddChild(int index, TUIModule.Builder<?> child) Attempts to add a child at the given index.addChild(TUIModule.Builder<?> child) Add a child to the end of this module's children.addChildren(TUIModule.Builder<?>... children) Linearly append the given children to this module's children.addChildren(List<TUIModule.Builder<?>> children) Linearly append the given children to this module's children.appendStyle(org.fusesource.jansi.Ansi ansi) AppendsAnsito the end of the existing ansi for this module and recursively for its children.Sets theApplicationModulefor this module and recursively for its children.abstract TUIModulebuild()Builds and returns the configuredTUIModule.Removes all children from this module.protected abstract BGets a fresh instance of this type of Builder.protected BWrapsdeepCopy(Builder, Map)to createcopyMapfor us.protected BdeepCopy(B original, Map<TUIModule.Builder<?>, TUIModule.Builder<?>> visited) Copies all data oforiginalinto this module, including a deep copy of all children.enableAnsi(boolean enable) Enables or disables ansi for this module and recursively for its children.org.fusesource.jansi.AnsigetAnsi()If this module displays text, this is the ansi that determines the text styling of that module (e.g., coloring, bolding, etc.).booleanWhether ansi is enabled applies to modules who may display text (e.g.,TextModule).TheApplicationModulethis module is tied to.Finds a child matching the name.<T extends TUIModule.Builder<?>>
TFinds a child matching the name and class type.Get the TUIModule Builders that are the children of this builder, in the order they will be run.getCopy()Creates a deep copy of this node and it's children; delegates to getDeepCopy(), created for simplicity.Creates a deep copy of this node and it's childrengetName()PrintStream that outputs data to the defined location.ThePropertyUpdateFlagsdetermine the behavior of property propagation.The Scanner that reads input from the defined source.getType()Gets the type of this module; enables the CRTP.lockProperty(TUIModule.Property property) Set the propagation behavior ofpropertyto not allow updates when a parent module is updated.Sets the name of this module.prependStyle(org.fusesource.jansi.Ansi ansi) PrependsAnsito the beginning of the existing ansi for this module and recursively for its children.voidprependToName(String name) Prependsnameto the current name of this module.printStream(PrintStream printStream) Sets theScannerfor this module and recursively for its children.Sets theScannerfor this module and recursively for its children.self()Casts this builder into the type given by the CRTP.protected voidshallowCopy(B original) Creates a copy oforiginalby mutating this instance.booleanshallowStructuralEquals(B first, B second) Checks equality for properties given by the builder.static booleanstructuralEquals(TUIModule.Builder<?> first, TUIModule.Builder<?> second) This is the same as shallowStructuralEquals, but it's static and does include a recursive children check.style(org.fusesource.jansi.Ansi ansi) Sets theAnsifor this module and recursively for its children.toString()Returns the name of this module.unlockProperty(TUIModule.Property property) Set the propagation behavior ofpropertyto allow updates when a parent module is updated.updateFlag(TUIModule.Property property, DirectedGraphNode.PropertyUpdateFlag flag) Set the propagation behavior of a specific property.updateProperties(TUIModule.Builder<?> module) Recursively updates all properties in this module based on the given module.Methods 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
-
name
The identifier for this module.
It is highly recommended to try and keep this unique in order to allow identification methods (e.g., viaApplicationModule.getInput(java.lang.String)) to function properly.
Warnings and potentially errors will be logged if modules with duplicate names are created. -
children
Every child module that should be run. -
propertyUpdateFlags
Specifies how property propagation functions forapplication,ansi,scanner,printStream, andenableAnsi.
Property updating behavior is provided byDirectedGraphNode.PropertyUpdateFlag. All properties are set toDirectedGraphNode.PropertyUpdateFlag.UPDATEby default. -
application
The application this module is tied to. -
ansi
protected org.fusesource.jansi.Ansi ansiThe ansi that may be displayed (Jansi object). Note that not every module will display ansi. -
scanner
The Scanner that reads input from the defined source. It is set to System.in by default (provided byTUIModule.DEFAULT_SCANNER).- Implementation Note:
- See
TUIModule.scannerfor an explanation on why every module needs an individual reference to a scanner.
-
printStream
PrintStream that outputs data to the defined location. It is set toSystem.inby default.- Implementation Note:
- See
TUIModule.scannerfor an explanation on why every module needs an individual reference to a PrintStream.
-
enableAnsi
protected boolean enableAnsiWhether ansi will be displayed or not. -
type
The class extending this class (CRTP). -
logger
protected static final org.slf4j.Logger loggerThe Logger for the module, provided by the slf4j facade -
usedNames
The frequency of names of all children of this module. This is used to support name duplicate detection.
An error is logged if there are name collisions.
-
-
Constructor Details
-
Builder
Constructs a newTUIModule.Builder.- Parameters:
type- The type of the module. This is usually defined by the inheriting class (e.g.,typeforTextModule.Builderwould beTextModule.Builder.class).name- The unique name of this module.
-
Builder
Constructs an emptyTUIModule.Builder. Used for copying.- Parameters:
type- The type of the module. This is usually defined by the inheriting class (e.g.,typeforTextModule.Builderwould beTextModule.Builder.class).
-
-
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.- Returns:
- A fresh, empty instance.
-
deepCopy
Copies all data oforiginalinto this module, including a deep copy of all children.- 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).
-
deepCopy
WrapsdeepCopy(Builder, Map)to createcopyMapfor us.- Parameters:
original- The module to copy into this module.- Returns:
- self
-
getDeepCopy
Creates a deep copy of this node and it's children- Returns:
- A deep copy of self
-
shallowCopy
Creates a copy oforiginalby mutating this instance. Children are not copied. This is a utility method forgetCopy()- Parameters:
original- The builder to copy from
-
getCopy
Creates a deep copy of this node and it's children; delegates to getDeepCopy(), created for simplicity.- Returns:
- A deep copy of self
-
getType
Gets the type of this module; enables the CRTP.- Specified by:
getTypein interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B extends TUIModule.Builder<B>> - Returns:
- the class type of this module.
-
getChildren
Get the TUIModule Builders that are the children of this builder, in the order they will be run.- Specified by:
getChildrenin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B extends TUIModule.Builder<B>> - Returns:
- The list of children of this module.
-
getPropertyUpdateFlags
ThePropertyUpdateFlagsdetermine the behavior of property propagation. SeeTUIModule.PropertyandDirectedGraphNode.PropertyUpdateFlag.- Specified by:
getPropertyUpdateFlagsin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B extends TUIModule.Builder<B>> - Returns:
- The flags for each property which determine propagation behavior.
-
updateFlag
Set the propagation behavior of a specific property. SeeTUIModule.Propertyfor the properties andDirectedGraphNode.PropertyUpdateFlagfor behavior types.- Parameters:
property- The property given byTUIModule.Propertyflag- The propagation behavior given byDirectedGraphNode.PropertyUpdateFlag- Returns:
- self
-
lockProperty
Set the propagation behavior ofpropertyto not allow updates when a parent module is updated.- Parameters:
property- The property given byTUIModule.Property- Returns:
- self
- Implementation Note:
- Internally, this sets the property flag to
DirectedGraphNode.PropertyUpdateFlag.HALT
-
unlockProperty
Set the propagation behavior ofpropertyto allow updates when a parent module is updated. Properties are unlocked by default.- Parameters:
property- The property given byTUIModule.Property- Returns:
- self
- Implementation Note:
- Internally, this sets the property flag to
DirectedGraphNode.PropertyUpdateFlag.UPDATE
-
updateProperties
Recursively updates all properties in this module based on the given module. All the properties that are updated are given byTUIModule.Property. This method does not override the property update flags given byDirectedGraphNode.PropertyUpdateFlagfor either this module or for children.- Parameters:
module- The module to copy properties from.- Returns:
- self
-
addChildren
Linearly append the given children to this module's children.- Parameters:
children- The children to add.- Returns:
- self
-
addChildren
Linearly append the given children to this module's children.- Parameters:
children- The children to add.- Returns:
- self
-
addChild
Add a child to the end of this module's children.- Parameters:
child- The child to add.- Returns:
- self
-
addChild
Attempts to add a child at the given index.- Parameters:
index- The index to add the child at.child- The child to add.- Returns:
- self
-
clearChildren
Removes all children from this module.- Returns:
- self.
-
getChild
Finds a child matching the name. It is recommended to name all modules uniquely so this returns a unique module every time.- Parameters:
name- The name of the child- Returns:
- The first found child (DFS), or null if none is found
-
getChild
Finds a child matching the name and class type. It is recommended to name all modules uniquely so this returns a unique module every time.- Parameters:
name- The name of the childtype- the type of builder to search for (e.g.,TextModule.class)- Returns:
- The first found child (DFS), or null if none is found of the correct type.
-
getName
- Returns:
- The name of this module.
-
name
Sets the name of this module. Ideally, each module should get a unique name. A warning is logged if a module is given a name that has been given to another module.- Parameters:
name- The unique name of this module.- Returns:
- self
-
prependToName
Prependsnameto the current name of this module. Useful for doing things like adding the name of the parent module to this module.- Parameters:
name- The name to prepend to the current name.
-
getApplication
TheApplicationModulethis module is tied to. An application module is primarily used for TUI input storage, as well as for providing a clean way to enter/exit the TUI.- Returns:
- The
ApplicationModulethat this module is tied to.
-
getAnsi
public org.fusesource.jansi.Ansi getAnsi()If this module displays text, this is the ansi that determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.- Returns:
- The ansi stored in the module.
-
getScanner
The Scanner that reads input from the defined source. It is set to System.in by default (provided byTUIModule.DEFAULT_SCANNER).- Returns:
- The reference to the Scanner used by this module (Note that not every module will use the Scanner).
-
getPrintStream
PrintStream that outputs data to the defined location. It is set toSystem.inby default.- Returns:
- The reference to the PrintStream used by this module (Note that not every module will use the PrintStream).
-
getAnsiEnabled
public boolean getAnsiEnabled()Whether ansi is enabled applies to modules who may display text (e.g.,TextModule). If ansi is disabled, only the raw text is displayed.- Returns:
- Whether ansi is enabled for this module.
-
application
Sets theApplicationModulefor this module and recursively for its children.
An application module is primarily used for TUI input storage, as well as for providing a clean way to enter/exit the TUI.
For most cases, you only need one application module per TUI.
Note: To prevent a module's application from being updated when you set the property of a parent, uselockProperty(Property). You can also useupdateFlag(Property, PropertyUpdateFlag)for more fine-grained control.- Parameters:
app- TheApplicationModulethat this module will be tied to.- Returns:
- self
-
style
Sets theAnsifor this module and recursively for its children.
For modules that display text,the ansi determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.
Note: Setting the ansi automatically locks it from further updating, either directly or via updating a parent. If this is not desired, useunlockProperty(Property). You can also useupdateFlag(Property, PropertyUpdateFlag). Setting the ansi usesTUIModule.Property.SET_ANSI. Note that defining behavior for SET_ANSI does not affect behavior for merging ansi (e.g., viaprependStyle(Ansi)).- Parameters:
ansi- TheAnsithat this module may use.- Returns:
- self
-
prependStyle
PrependsAnsito the beginning of the existing ansi for this module and recursively for its children.
For modules that display text,the ansi determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.
Note: To prevent a module's ansi from being merged into when you set the property of a parent, uselockProperty(Property). Prepending the ansi usesTUIModule.Property.MERGE_ANSI. You can also useupdateFlag(Property, PropertyUpdateFlag)for more fine-grained control. Note that defining behavior for MERGE_ANSI does not affect behavior for setting ansi directly.- Parameters:
ansi- TheAnsithat this module may use.- Returns:
- self
-
appendStyle
AppendsAnsito the end of the existing ansi for this module and recursively for its children.
For modules that display text,the ansi determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.
Note: To prevent a module's ansi from being merged into when you set the property of a parent, uselockProperty(Property). Appending the ansi usesTUIModule.Property.MERGE_ANSI. You can also useupdateFlag(Property, PropertyUpdateFlag)for more fine-grained control. Note that defining behavior for MERGE_ANSI does not affect behavior for setting ansi directly.- Parameters:
ansi- TheAnsithat this module may use.- Returns:
- self
-
scanner
Sets theScannerfor this module and recursively for its children.
The Scanner reads input from the defined source (note that not every module reads input). It is set to System.in by default (provided byTUIModule.DEFAULT_SCANNER).
Note: Setting the scanner automatically locks it from further updating, either directly or via updating a parent. If this is not desired, useunlockProperty(Property). You can also useupdateFlag(Property, PropertyUpdateFlag)for more fine-grained control.- Parameters:
scanner- TheScannerthat this module may use.- Returns:
- self
-
printStream
Sets theScannerfor this module and recursively for its children.
The PrintStream outputs data to the defined location (note that not every module writes data). It is set toSystem.inby default.
Note: Setting the PrintStream automatically locks it from further updating, either directly or via updating a parent. If this is not desired, useunlockProperty(Property). You can also useupdateFlag(Property, PropertyUpdateFlag)for more fine-grained control.- Parameters:
printStream- ThePrintStreamthat this module may use.- Returns:
- self
-
enableAnsi
Enables or disables ansi for this module and recursively for its children.
LockingTUIModule.Property.SET_ANSIdoes not prevent ansi from being disabled via this method. You must lockTUIModule.Property.ENABLE_ANSI.
Ansi is enabled by default.
For modules that display text,the ansi determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.
Note: Changing whether ansi is enabled automatically locks it from further updating, either directly or via updating a parent. If this is not desired, useunlockProperty(Property). You can also useupdateFlag(Property, PropertyUpdateFlag). Changing whether ansi is enabled usesTUIModule.Property.ENABLE_ANSI. Note that defining behavior for ENABLE_ANSI does not affect behavior for setting or merging ansi (e.g., viaprependStyle(Ansi)).- Parameters:
enable- Whether ansi is enabled.- Returns:
- self
-
self
Casts this builder into the type given by the CRTP.- Specified by:
selfin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B extends TUIModule.Builder<B>> - Returns:
- self, casted to the appropriate type
- Implementation Note:
- This is a safe operation because
typeis enforced to be typeBat runtime.
-
shallowStructuralEquals
Checks equality for properties given by the builder.
For TUIModule, this includes:
- name
- application
- ansi
- scanner
- printStream
- enableAnsi
Note: Runtime properties (e.g., currentRunningChild, terminated), are not considered. Children are also not considered here, but are considered in equals().
- Specified by:
shallowStructuralEqualsin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, B extends TUIModule.Builder<B>> - Parameters:
first- The first TUIModule to comparesecond- The second TUIModule to compare- Returns:
trueiffirstandsecondare equal according to builder-provided properties- Implementation Note:
- This is the
Function<TUIModule<?>, TUIModule.Builder<?>, Boolean>that is passed intoDirectedGraphNode.structuralEquals(DirectedGraphNode)
-
structuralEquals
This is the same as shallowStructuralEquals, but it's static and does include a recursive children check.- Parameters:
first- The first TUIModule to comparesecond- The second TUIModule to compare- Returns:
trueiffirstandsecondare equal according to builder-provided properties- Implementation Note:
- Polymorphism is automatic here and thus this method does not generally need to be overloaded.
-
toString
Returns the name of this module. For a formatted string of the module hierarchy stemming from this module, seeTUIModule.toTreeString(). This will require building the module. -
build
Builds and returns the configuredTUIModule.- Returns:
- the constructed module.
-