Class TemplateManager
java.lang.Object
com.blackbuild.klum.ast.runtime.internal.TemplateManager
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 TypeMethodDescriptionvoidaddTemplates(Map<Class<?>, ?> newTemplates) Adds the given templates to the current templates.static <T> TdoWithTemplates(Map<Class<?>, ?> newTemplates, groovy.lang.Closure<T> body) Executes the given closure with the given templates.static TemplateManagerReturns the current instance of the TemplateManager.<T> TgetTemplate(Class<T> type) Returns the currently active template for the given type.static booleanisTemplate(Object value) Returns whether the value is a materialized Template with persistent Template companion identity.<T> voidsetTemplate(Class<T> type, T template) Sets the template for the given type.voidsetTemplates(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> TwithTemplates(List<Object> newTemplates, groovy.lang.Closure<T> body) Executes the given closure with the given templates.static <T> TwithTemplates(Map<Class<?>, Map<String, ?>> newTemplates, groovy.lang.Closure<T> body) Executes the given closure with the given anonymous templates.
-
Method Details
-
getInstance
Returns the current instance of the TemplateManager. If no instance is active, a new one is created.- Returns:
- the current instance
-
isTemplate
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
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 appliedtemplate- the templatebody- 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 appliedtemplate- the Map to construct the template frombody- the closure to execute- Returns:
- the result of the closure
-
doWithTemplates
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 usewithTemplates(List, Closure), which maps the templates to their respective classes.- Parameters:
newTemplates- the templates to apply, Mapping classes to their respective templatesbody- 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 templatesbody- the closure to execute- Returns:
- the result of the closure
-
withTemplates
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 applybody- the closure to execute- Returns:
- the result of the closure
-
getTemplate
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
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 templatetemplate- the template
-
addTemplates
Adds the given templates to the current templates.- Parameters:
newTemplates- the templates to add
-
setTemplates
Sets the templates to the given templates. Removes all existing templates.- Parameters:
newTemplates- the templates to set
-