Class ModuleTemplate<B extends ModuleTemplate<B>>

java.lang.Object
com.calebleavell.jatui.modules.TUIModule.Builder<B>
com.calebleavell.jatui.modules.ModuleTemplate<B>
Type Parameters:
B - The class extending this (e.g., class MyTemplate extends ModuleTemplate<MyTemplate>).
All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>,B>
Direct Known Subclasses:
ConfirmationPrompt, InputHandler, NumberedList, NumberedModuleSelector, PasswordInput, TextChain

public abstract class ModuleTemplate<B extends ModuleTemplate<B>> extends TUIModule.Builder<B>
This class is an extension of TUIModule.Builder that provides a main field which will be accessible inside the class extending this. The purpose of main is to separate children added from inside the class from children added from whoever is instantiating the class. For example, suppose MyTemplate extends Template, and a user calls it as follows:
MyTemplate = new MyTemplate("template").addChild(child, 2)

This adds the child at index 2, potentially puts the new child inside the automatically added children and breaking the atomicity of the template (i.e., that it runs as intended before user-added logic runs). Putting the children inside main ensures all automatically added children run as expected and are organized.

Template also enforces building to a ContainerModule, which improves modularity.
  • Field Details

    • main

      protected ContainerModule.Builder main
      A container for separating children essential to the function of the class from children added after instantiation. See TUIModule for intuition.
  • Constructor Details

    • ModuleTemplate

      public ModuleTemplate(Class<B> type, String name)
      Creates the new Template and adds main as the first child.
      Parameters:
      type - The type of the class extending ModuleTemplate
      name - The unique identifier for the module.
    • ModuleTemplate

      protected ModuleTemplate(Class<B> type)
      Creates a fresh instance for copying utility.
      Parameters:
      type - The type of the class extending ModuleTemplate
  • Method Details

    • deepCopy

      protected B deepCopy(B original, Map<TUIModule.Builder<?>,TUIModule.Builder<?>> visited)
      Copies all data of original into this module, including a deep copy of all children.
      Overrides:
      deepCopy in class TUIModule.Builder<B extends ModuleTemplate<B>>
      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 original hasn't been visited yet).
      Implementation Note:
      Re-assigns main to the copy that was created from the recursive children copying.
    • build

      public ContainerModule build()
      Builds the finalized ContainerModule

      Note: If you are going to override this method, ensure any changes made to main or other children are reset each time it's called. We want to ensure calling build() multiple times returns the same output. Most likely, you'll want to call main.clearChildren() as the first line of the override.
      Specified by:
      build in class TUIModule.Builder<B extends ModuleTemplate<B>>
      Returns:
      The built ContainerModule