Class VarTemplate
A VarTemplate is the result of compiling a template string against a VarResolver.
It separates the "parse" step from the "evaluate" step so that repeated resolutions of the same
input string skip tokenization, var-registry lookup, and (eventually) function-dispatch metadata.
Templates are obtained via VarResolver.compile(String) or
VarResolverSession.compile(String) — there is no public constructor.
Compile-binding contract:
A compiled template is bound to the VarResolver instance it was compiled against. Cached
Var (and, in the future, VarFunction) references are resolved at compile time.
Passing a VarTemplate produced by one resolver to a session belonging to a different
resolver is undefined behavior. Rebuilding the resolver invalidates any compiled templates from
the previous instance — caller's responsibility to re-compile.
Threadsafety:
VarTemplate instances are immutable and threadsafe — the same template can be safely
shared across threads. Note that VarResolverSession is not threadsafe; the
session passed to resolve(VarResolverSession) or asSupplier(VarResolverSession)
inherits the standard session-threadsafety contract. The asSupplierWithFreshSessions(VarResolver)
variant opens a fresh session per call and is safe to share across threads.
Which Supplier do I want?
asSupplierWithFreshSessions(VarResolver)— opens a fresh session per.get()call. Safe to share across threads. Use this for user-facing code, hot-reload patterns, and any situation where theSuppliermay be invoked on threads other than the caller's. Recommended default.asSupplier(VarResolverSession)— captures the existing session and reuses it on every.get()call. Inherits the session's threadsafety contract (i.e. not threadsafe). Use only in perf-sensitive paths where the caller owns the session lifecycle.
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionasSupplier(VarResolverSession session) Returns aSupplierthat resolves this template against the given session each time itsSupplier.get()method is called.Returns the raw input string this template was compiled from.booleanReturnstrueif this template contains no variable references and resolves to a fixed string.resolve(VarResolverSession session) Resolves this template against the given session.resolveTo(VarResolverSession session, Writer out) Resolves this template against the given session and writes the result directly toout.resolveToUnchecked(VarResolverSession session, Writer out) Convenience helper for callers that need toresolveTo(VarResolverSession, Writer)but don't want to handleIOException.toString()
-
Method Details
-
resolve
Resolves this template against the given session.Walks the cached segment array in order, accumulating the result in a
StringBuilder. Literal-only templates fast-path to a single allocation skip and return the source string directly.- Parameters:
session- The session to resolve against. Must not benull. The session is not required to belong to the sameVarResolverthis template was compiled against, but cachedVarreferences will only honor that resolver's registry — see the "compile-binding contract" note in the class javadoc.- Returns:
- The resolved string, or
nullif this template was compiled from anullsource (mirroringVarResolverSession.resolve(String)returningnullfornullinput). Otherwise nevernull.
-
resolveTo
Resolves this template against the given session and writes the result directly toout.Streamed-Var-bearing segments write through to
outwithout materializing an intermediateString. Used byVarResolver.resolveTo(String, Writer)so downstream serializers can stream large content efficiently.- Parameters:
session- The session to resolve against. Must not benull.out- The writer to append to. Must not benull.- Returns:
out, for fluent-API chaining.- Throws:
IOException- Thrown by the underlying stream.
-
asSupplier
Returns aSupplierthat resolves this template against the given session each time itsSupplier.get()method is called.The captured session is reused across
.get()calls; this Supplier is not threadsafe (it inherits the session's threadsafety contract). For a threadsafe variant useasSupplierWithFreshSessions(VarResolver).- Parameters:
session- The session to capture. Must not benull.- Returns:
- A
Supplier<String>bound tosession.
-
asSupplierWithFreshSessions
Returns a threadsafeSupplierthat opens a freshVarResolverSessionon everySupplier.get()call.Use this variant when the returned
Suppliermay be invoked from threads other than the caller's — e.g. when stashed on a long-lived bean field or shared across worker threads. The fresh-session-per-.get()pattern is the recommended default for user-facing code.- Parameters:
vr- The resolver to open sessions against. Must not benull. Typically the same resolver this template was compiled against.- Returns:
- A threadsafe
Supplier<String>.
-
isLiteral
Returnstrueif this template contains no variable references and resolves to a fixed string.Computed at compile time by inspecting the segment array:
trueiff the array is empty or contains onlyLiteralSegmententries. Stable-value folding may flip a template with stableVars to literal by replacing the relevantVarRefSegmententries withLiteralSegmententries at compile time.Callers can use this hint to fast-path purely-literal templates — for example, by skipping the
Supplierwrapping overhead for a@Value("plain string")field:var tpl =vr .compile(raw );if (tpl .isLiteral())// No need to wrap in a Supplier; the value never changes. return tpl .resolve(session );- Returns:
trueif the template resolves to a fixed string.
-
getSource
Returns the raw input string this template was compiled from.Useful for diagnostics, error messages, and debugging. The string is preserved verbatim including any escape sequences.
- Returns:
- The raw input string.
-
resolveToUnchecked
Convenience helper for callers that need toresolveTo(VarResolverSession, Writer)but don't want to handleIOException. Wraps any thrownIOExceptionin aRuntimeExceptionvia the standardThrowableUtils.toRexhelper.- Parameters:
session- The session to resolve against. Must not benull.out- The writer to append to. Must not benull.- Returns:
out.
-
toString
-