- All Superinterfaces:
JStachioExtension
- All Known Subinterfaces:
JStachioTemplateFinder.SimpleTemplateFinder,TemplateProvider.GeneratedTemplateProvider
- All Known Implementing Classes:
TemplateCatalog
Finds templates based on the model type (class).
The default JStachio uses a combination of relection and the ServiceLoader to
find templates.
Other implementations may want to use their DI framework like Spring or CDI to find templates.
- Author:
- agentgt
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAn easier to implement template finder based on a sequence of templates. -
Method Summary
Modifier and TypeMethodDescriptionstatic JStachioTemplateFinderDecorates a template finder with a cache usingClassValuewith the modelType as the key.static JStachioTemplateFinderdefaultTemplateFinder(JStachioConfig config) The default template finder that uses reflection and or the ServiceLoader.default @Nullable TemplateInfofindOrNull(Class<?> modelType) Finds a template or null if no template is found.findTemplate(Class<?> modelType) Finds aTemplateif possible otherwise possibly falling back to aTemplateInfobased on annotation metadata or some other mechanism.static JStachioTemplateFinderof(Iterable<? extends TemplateInfo> templates, int order) Creates a template finder from an iterable of templates.static JStachioTemplateFinderof(List<? extends JStachioTemplateFinder> templateFinders) Creates a composite template finder from a list.default intorder()Hint on order of template finders.default booleansupportsType(Class<?> modelType) Determines if this template finder has a template for the model type (the class annotated by JStache).
-
Method Details
-
findTemplate
Finds aTemplateif possible otherwise possibly falling back to aTemplateInfobased on annotation metadata or some other mechanism.- Parameters:
modelType- the models class (the one annotated withJStacheand not the Templates class)- Returns:
- the template info which might be a
Templateif the generated template was found. - Throws:
Exception- if any reflection or runtime error happensNoSuchElementException- if the template is not found and there were no other errorsNullPointerException- if the modelType is null- API Note
- Callers can do an
instanceof Template tto see if a generated template was returned instead of the fallbackTemplateInfometadata.
-
findOrNull
Finds a template or null if no template is found. Should not throw an exception if a template is not found.- Parameters:
modelType- the models class (the one annotated withJStacheand not the Templates class)- Returns:
nullif the template is was not found or the template info which might be aTemplateif the generated template was found.- Throws:
NullPointerException- if the modelType is null- See Also:
-
supportsType
Determines if this template finder has a template for the model type (the class annotated by JStache).- Parameters:
modelType- the models class (the one annotated withJStacheand not the Templates class)- Returns:
- true if this finder has template for modelType
- Throws:
NullPointerException- if the modelType is null
-
order
Hint on order of template finders. The foundJStachioTemplateFinders are sorted naturally (lower number comes first) based on the returned number. Thus a template finder with a lower order number thatsupportsType(Class)the model class will be used.- Returns:
- default returns zero
-
defaultTemplateFinder
The default template finder that uses reflection and or the ServiceLoader.This implementation performs no caching. If you would like caching call
cachedTemplateFinder(JStachioTemplateFinder)on the returned finder.- Parameters:
config- used to help find templates as well as logging.- Returns:
- default template finder.
-
cachedTemplateFinder
Decorates a template finder with a cache usingClassValuewith the modelType as the key.The returned finder will only call
findTemplate(Class)on the passed in delegate finder to resolvesupportsType(Class)andfindOrNull(Class)!While the finder does not provide any eviction the cache will not prevent garbage collection of the model classes.
- Parameters:
finder- to be decorated unless the finder is already decorated thus it is a noop to repeateadly call this method on already cached template finder.- Returns:
- caching template finder
-
of
Creates a template finder from an iterable of templates. The returned finder will just loop through the templates and callTemplateInfo.supportsType(Class). To avoid the looping cost wrap the return withcachedTemplateFinder(JStachioTemplateFinder).- Parameters:
templates- templates to be searched in order of the iterableorder- order hint seeorder().- Returns:
- adapted template finder
-
of
Creates a composite template finder from a list. If the list only has a single element then it is returned without being wrapped.- Parameters:
templateFinders- list of template finders.- Returns:
- templateFinder searching in order of
order()then the list order
-