Class TemplateManager

java.lang.Object
com.blackbuild.klum.ast.runtime.internal.TemplateManager

public class TemplateManager extends Object
Handles templates for a given dsl-class.

The template manager is a thread-local singleton that can be used to apply templates to objects created in a given scope. All important methods are static and can be used from anywhere in the code.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addTemplates(Map<Class<?>,?> newTemplates)
    Adds the given templates to the current templates.
    static <T> T
    doWithTemplates(Map<Class<?>,?> newTemplates, groovy.lang.Closure<T> body)
    Executes the given closure with the given templates.
    Returns the current instance of the TemplateManager.
    <T> T
    getTemplate(Class<T> type)
    Returns the currently active template for the given type.
    static boolean
    Returns whether the value is a materialized Template with persistent Template companion identity.
    <T> void
    setTemplate(Class<T> type, T template)
    Sets the template for the given type.
    void
    setTemplates(Map<Class<?>,?> newTemplates)
    Sets the templates to the given templates.
    static <T, C> T
    withTemplate(Class<C> type, C template, groovy.lang.Closure<T> body)
    Executes the given closure with the given template as the template for the given type.
    static <T, C> T
    withTemplate(Class<C> type, Map<String,?> template, groovy.lang.Closure<T> body)
    Executes the given closure with an anonymous template for the given type.
    static <T> T
    withTemplates(List<Object> newTemplates, groovy.lang.Closure<T> body)
    Executes the given closure with the given templates.
    static <T> T
    withTemplates(Map<Class<?>,Map<String,?>> newTemplates, groovy.lang.Closure<T> body)
    Executes the given closure with the given anonymous templates.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getInstance

      public static TemplateManager getInstance()
      Returns the current instance of the TemplateManager. If no instance is active, a new one is created.
      Returns:
      the current instance
    • isTemplate

      public static boolean isTemplate(Object value)
      Returns whether the value is a materialized Template with persistent Template companion identity. Active Template scopes and live Builders do not imply Template identity.
    • withTemplate

      public static <T, C> T withTemplate(Class<C> type, C template, groovy.lang.Closure<T> body)
      Executes the given closure with the given template as the template for the given type. This means that all objects of the given type created in the scope of the closure will use the given template, which also includes objects deeper in the structure. The old template is restored after the closure has been executed.
      Type Parameters:
      T - the type of the template
      Parameters:
      type - the type for which the template will be applied
      template - the template
      body - the closure to execute
      Returns:
      the result of the closure
    • withTemplate

      public static <T, C> T withTemplate(Class<C> type, Map<String,?> template, groovy.lang.Closure<T> body)
      Executes the given closure with an anonymous template for the given type. This means that all objects of the given type created in the scope of the closure will use the given template, which also includes objects deeper in the structure. The template will be created from the given map (using Create.AsTemplate(Map)). The old template is restored after the closure has been executed.
      Type Parameters:
      T - the type of the template
      Parameters:
      type - the type for which the template will be applied
      template - the Map to construct the template from
      body - the closure to execute
      Returns:
      the result of the closure
    • doWithTemplates

      public static <T> T doWithTemplates(Map<Class<?>,?> newTemplates, groovy.lang.Closure<T> body)
      Executes the given closure with the given templates. This means that all objects of the given types created in the scope of the closure will use the given template, which also includes objects deeper in the structure. The old templates are restored after the closure has been executed. Usually it is better to use withTemplates(List, Closure), which maps the templates to their respective classes.
      Parameters:
      newTemplates - the templates to apply, Mapping classes to their respective templates
      body - the closure to execute
      Returns:
      the result of the closure
    • withTemplates

      public static <T> T withTemplates(Map<Class<?>,Map<String,?>> newTemplates, groovy.lang.Closure<T> body)
      Executes the given closure with the given anonymous templates. This is done by converting the values of the map into template objects of the type defined by the keys.
      Parameters:
      newTemplates - the templates to apply, Mapping classes to their respective anonymous templates
      body - the closure to execute
      Returns:
      the result of the closure
    • withTemplates

      public static <T> T withTemplates(List<Object> newTemplates, groovy.lang.Closure<T> body)
      Executes the given closure with the given templates. This means that all objects of the given types created in the scope of the closure will use the given template, which also includes objects deeper in the structure. The old templates are restored after the closure has been executed.
      Parameters:
      newTemplates - the templates to apply
      body - the closure to execute
      Returns:
      the result of the closure
    • getTemplate

      public <T> T getTemplate(Class<T> type)
      Returns the currently active template for the given type.
      Type Parameters:
      T - the type of the template
      Parameters:
      type - The type of the template
      Returns:
      the template or null if no template is active
    • setTemplate

      public <T> void setTemplate(Class<T> type, T template)
      Sets the template for the given type. If the template is null, the template is removed.
      Type Parameters:
      T - the type of the template
      Parameters:
      type - the type of the template
      template - the template
    • addTemplates

      public void addTemplates(Map<Class<?>,?> newTemplates)
      Adds the given templates to the current templates.
      Parameters:
      newTemplates - the templates to add
    • setTemplates

      public void setTemplates(Map<Class<?>,?> newTemplates)
      Sets the templates to the given templates. Removes all existing templates.
      Parameters:
      newTemplates - the templates to set