StringTemplate instances in version 3 had a .reset()
method.
I am generating inside a for/each
loop and want to reset the instance to its default state at the end of each loop.
I have searched the JavaDoc and can not find out how to reset an instance of ST
to reuse it.
ST
instance in StringTemplate4
?The
.reset()
method was removed and there is no direct documentation that hints to how to accomplish this in version 4.By digging into the source code I discovered that
.getInstanceOf()
delivers a cached uninitialized copy of the template there is no need to call.reset()
on the instances anymore, just get a fresh cached instance.
/** The primary means of getting an instance of a template from this
* group. Names must be absolute, fully-qualified names like {@code /a/b}.
*/
public ST getInstanceOf(String name) {
if ( name==null ) return null;
if ( verbose ) System.out.println(getName()+".getInstanceOf("+name+")");
if ( name.charAt(0)!='/' ) name = "/"+name;
CompiledST c = lookupTemplate(name);
if ( c!=null ) {
return createStringTemplate(c);
}
return null;
}
/** Look up a fully-qualified name. */
public CompiledST lookupTemplate(String name) {
if ( name.charAt(0)!='/' ) name = "/"+name;
if ( verbose ) System.out.println(getName()+".lookupTemplate("+name+")");
CompiledST code = rawGetTemplate(name);
if ( code==NOT_FOUND_ST ) {
if ( verbose ) System.out.println(name+" previously seen as not found");
return null;
}
// try to load from disk and look up again
if ( code==null ) code = load(name);
if ( code==null ) code = lookupImportedTemplate(name);
if ( code==null ) {
if ( verbose ) System.out.println(name+" recorded not found");
templates.put(name, NOT_FOUND_ST);
}
if ( verbose ) if ( code!=null ) System.out.println(getName()+".lookupTemplate("+name+") found");
return code;
}