<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
I’d recommend using the two argument <span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">StringTemplate</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); background-color: rgb(255, 255, 255);">.</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); font-style: italic;">interpolate</span> for
 this sort of use case. Process values as a stream and then feed to <span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">StringTemplate</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); background-color: rgb(255, 255, 255);">.</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); font-style: italic;">interpolate</span><span style="caret-color: rgb(8, 8, 8); color: rgb(8, 8, 8); font-family: "JetBrains Mono", monospace; font-size: 9.8pt; background-color: rgb(255, 255, 255);">(</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); background-color: rgb(255, 255, 255);">fragments,
</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">values</span><span style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(8, 8, 8); background-color: rgb(255, 255, 255);">).</span>
<div>
<div style="background-color: rgb(255, 255, 255); color: rgb(8, 8, 8);">
<pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: rgb(0, 51, 179);">static final </span><span style="color: rgb(0, 0, 0);">StringTemplate</span>.<span style="color: rgb(0, 0, 0);">Processor</span><<span style="color: rgb(0, 0, 0);">String</span>, <span style="color: rgb(0, 0, 0);">RuntimeException</span>> <span style="color: rgb(135, 16, 148); font-style: italic;">HEXER </span>= st -> {<br>    <span style="color: rgb(0, 0, 0);">List</span><<span style="color: rgb(0, 0, 0);">String</span>> <span style="color: rgb(0, 0, 0);">values </span>= st.values()<br>            .stream()<br>            .map(v -> v <span style="color: rgb(0, 51, 179);">instanceof </span><span style="color: rgb(0, 0, 0);">Integer </span>i ? <span style="color: rgb(6, 125, 23);">"0x" </span>+ <span style="color: rgb(0, 0, 0);">Integer</span>.<span style="font-style: italic;">toHexString</span>(i)<br>                                             : <span style="color: rgb(0, 0, 0);">String</span>.<span style="font-style: italic;">valueOf</span>(v))<br>            .toList();<br>    <span style="color: rgb(0, 51, 179);">return  </span><span style="color: rgb(0, 0, 0);">StringTemplate</span>.<span style="font-style: italic;">interpolate</span>(st.fragments(), <span style="color: rgb(0, 0, 0);">values</span>);<br> };<br><br><span style="color: rgb(0, 51, 179);">public static void </span><span style="color: rgb(0, 98, 122);">main</span>(<span style="color: rgb(0, 0, 0);">String</span>... args) {<br>    <span style="color: rgb(0, 51, 179);">int </span><span style="color: rgb(0, 0, 0);">x </span>= <span style="color: rgb(23, 80, 235);">10</span>, <span style="color: rgb(0, 0, 0);">y </span>= <span style="color: rgb(23, 80, 235);">20</span>;<br>    <span style="color: rgb(0, 0, 0);">System</span>.<span style="color: rgb(135, 16, 148); font-style: italic;">out</span>.println(<span style="color: rgb(135, 16, 148); font-style: italic;">HEXER</span>.<span style="color: rgb(6, 125, 23);">"\{</span><span style="color: rgb(0, 0, 0);">x</span><span style="color: rgb(6, 125, 23);">} + \{</span><span style="color: rgb(0, 0, 0);">y</span><span style="color: rgb(6, 125, 23);">} = \{</span><span style="color: rgb(0, 0, 0);">x </span>+ <span style="color: rgb(0, 0, 0);">y</span><span style="color: rgb(6, 125, 23);">}"</span>);<br>}<br></pre>
<pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br></pre>
<pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;">0xa + 0x14 = 0x1e</pre>
<pre style="font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><br></pre>
</div>
<div><br>
<blockquote type="cite">
<div>On Nov 27, 2023, at 1:00 PM, Tagir Valeev <amaembo@gmail.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div dir="ltr">Hello!<br>
<div><br>
</div>
<div>As we expect that people will create custom template processors, we can simplify their lives.</div>
<div><br>
</div>
<div>First, it could be common to add a finisher transformation to an existing processor. I think that for many purposes it would be enough to use STR processor as a starter, and then create a custom domain object from the resulting string. This could be simplified
 if we add a method 'andThen' to the Processor interface:</div>
<div><br>
</div>
<div>
<div style="color:rgb(8,8,8)">
<pre style="font-family:"JetBrains Mono",monospace"><span style="color:rgb(0,66,140);font-weight:bold">default </span><<span style="color:rgb(0,126,138)">RR</span>> <span style="">Processor</span><<span style="color:rgb(0,126,138)">RR</span>, <span style="color:rgb(0,126,138)">E</span>> <span style="color:rgb(18,19,20)">andThen</span>(<span style="">Function</span><<span style="color:rgb(0,126,138)">R</span>, <span style="color:rgb(0,126,138)">RR</span>> <span style="">finisher</span>) {<br>    <span style="">Objects</span>.<span style="font-style:italic">requireNonNull</span>(<span style="">finisher</span>);<br>    <span style="color:rgb(0,66,140);font-weight:bold">return </span><span style="">st </span>-> <span style="color:rgb(25,72,166)">finisher</span>.apply(process(<span style="">st</span>));<br>}<br></pre>
</div>
</div>
<div>Second, I think that many processors may want to keep string parts intact but handle embedded expressions in a special way, effectively replacing the default 'String.valueOf' behavior of the standard STR processor. We can provide a way doing this, encapsulating
 all the ceremony. Something like this:</div>
<div><br>
</div>
<div>
<div style="color:rgb(8,8,8)">
<pre style="font-family:"JetBrains Mono",monospace"><span style="color:rgb(0,66,140);font-weight:bold">static </span><span style="">StringTemplate</span>.<span style="">Processor</span><<span style="">String</span>, <span style="">RuntimeException</span>> <span style="color:rgb(18,19,20)">withToString</span>(<span style="">Function</span><<span style="">Object</span>, <span style="">String</span>> <span style="">toStringFunction</span>) {<br>    <span style="">Objects</span>.<span style="font-style:italic">requireNonNull</span>(<span style="">toStringFunction</span>);<br>    <span style="color:rgb(0,66,140);font-weight:bold">return </span><span style="">st </span>-> {<br>        <span style="">StringBuilder sb </span>= <span style="color:rgb(0,66,140);font-weight:bold">new </span>StringBuilder();<br>        <span style="">Iterator</span><<span style="">String</span>> <span style="">fragIter </span>= <span style="">st</span>.fragments().iterator();<br><br>        <span style="color:rgb(0,66,140);font-weight:bold">for </span>(<span style="">Object value </span>: <span style="">st</span>.values()) {<br>            <span style="">sb</span>.append(<span style="">fragIter</span>.next());<br>            <span style="">sb</span>.append(<span style="color:rgb(25,72,166)">toStringFunction</span>.apply(<span style="">value</span>));<br>        }<br><br>        <span style="">sb</span>.append(<span style="">fragIter</span>.next());<br>        <span style="color:rgb(0,66,140);font-weight:bold">return </span><span style="">sb</span>.toString();<br>    };<br>}<br></pre>
</div>
</div>
<div>withToString(String::valueOf) should be equivalent to the STR template (in functionality, not in performance)<br>
</div>
<div><br>
</div>
<div>Now, one can easily build interesting things like:</div>
<div><br>
</div>
<div>
<div style="color:rgb(8,8,8)">
<pre style="font-family:"JetBrains Mono",monospace"><span style="">StringTemplate</span>.<span style="">Processor</span><<span style="">Pattern</span>, <span style="">RuntimeException</span>> <span style="color:rgb(0,75,159);font-style:italic">REGEXP </span>= <br>   <span style="font-style:italic">withToString</span>(<span style="">obj </span>-> <span style="">Pattern</span>.<span style="font-style:italic">quote</span>(<span style="">obj</span>.toString())).andThen(<span style="">Pattern</span>::<span style="font-style:italic">compile</span>);<br></pre>
</div>
</div>
<div><br>
</div>
<div>What do you think?</div>
<div><br>
</div>
<div>With best regards,</div>
<div>Tagir Valeev</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>