<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
I’d like to up-level this discussion a bit from the specific API suggestions, which are of course meant to be evocative examples.
<div class=""><br class="">
</div>
<div class="">The examples here are obviously useful; I can imagine use cases for all of them. I’m a little concerned about adding them as “point†solutions, though, because I’d have concerns about whether they will work together. Which makes me think that
the withToString example might prefer to be an abstract class rather than a combinator, so that if users want to override various things, those will work together. <br class="">
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Nov 27, 2023, at 12:00 PM, Tagir Valeev <<a href="mailto:amaembo@gmail.com" class="">amaembo@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">Hello!<br class="">
<div class=""><br class="">
</div>
<div class="">As we expect that people will create custom template processors, we can simplify their lives.</div>
<div class=""><br class="">
</div>
<div class="">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 class=""><br class="">
</div>
<div class="">
<div style="color:rgb(8,8,8)" class="">
<pre style="font-family:"JetBrains Mono",monospace" class=""><span style="color:rgb(0,66,140);font-weight:bold" class="">default </span><<span style="color:rgb(0,126,138)" class="">RR</span>> <span style="" class="">Processor</span><<span style="color:rgb(0,126,138)" class="">RR</span>, <span style="color:rgb(0,126,138)" class="">E</span>> <span style="color:rgb(18,19,20)" class="">andThen</span>(<span style="" class="">Function</span><<span style="color:rgb(0,126,138)" class="">R</span>, <span style="color:rgb(0,126,138)" class="">RR</span>> <span style="" class="">finisher</span>) {<br class=""> <span style="" class="">Objects</span>.<span style="font-style:italic" class="">requireNonNull</span>(<span style="" class="">finisher</span>);<br class=""> <span style="color:rgb(0,66,140);font-weight:bold" class="">return </span><span style="" class="">st </span>-> <span style="color:rgb(25,72,166)" class="">finisher</span>.apply(process(<span style="" class="">st</span>));<br class="">}<br class=""></pre>
</div>
</div>
<div class="">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 class=""><br class="">
</div>
<div class="">
<div style="color:rgb(8,8,8)" class="">
<pre style="font-family:"JetBrains Mono",monospace" class=""><span style="color:rgb(0,66,140);font-weight:bold" class="">static </span><span style="" class="">StringTemplate</span>.<span style="" class="">Processor</span><<span style="" class="">String</span>, <span style="" class="">RuntimeException</span>> <span style="color:rgb(18,19,20)" class="">withToString</span>(<span style="" class="">Function</span><<span style="" class="">Object</span>, <span style="" class="">String</span>> <span style="" class="">toStringFunction</span>) {<br class=""> <span style="" class="">Objects</span>.<span style="font-style:italic" class="">requireNonNull</span>(<span style="" class="">toStringFunction</span>);<br class=""> <span style="color:rgb(0,66,140);font-weight:bold" class="">return </span><span style="" class="">st </span>-> {<br class=""> <span style="" class="">StringBuilder sb </span>= <span style="color:rgb(0,66,140);font-weight:bold" class="">new </span>StringBuilder();<br class=""> <span style="" class="">Iterator</span><<span style="" class="">String</span>> <span style="" class="">fragIter </span>= <span style="" class="">st</span>.fragments().iterator();<br class=""><br class=""> <span style="color:rgb(0,66,140);font-weight:bold" class="">for </span>(<span style="" class="">Object value </span>: <span style="" class="">st</span>.values()) {<br class=""> <span style="" class="">sb</span>.append(<span style="" class="">fragIter</span>.next());<br class=""> <span style="" class="">sb</span>.append(<span style="color:rgb(25,72,166)" class="">toStringFunction</span>.apply(<span style="" class="">value</span>));<br class=""> }<br class=""><br class=""> <span style="" class="">sb</span>.append(<span style="" class="">fragIter</span>.next());<br class=""> <span style="color:rgb(0,66,140);font-weight:bold" class="">return </span><span style="" class="">sb</span>.toString();<br class=""> };<br class="">}<br class=""></pre>
</div>
</div>
<div class="">withToString(String::valueOf) should be equivalent to the STR template (in functionality, not in performance)<br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Now, one can easily build interesting things like:</div>
<div class=""><br class="">
</div>
<div class="">
<div style="color:rgb(8,8,8)" class="">
<pre style="font-family:"JetBrains Mono",monospace" class=""><span style="" class="">StringTemplate</span>.<span style="" class="">Processor</span><<span style="" class="">Pattern</span>, <span style="" class="">RuntimeException</span>> <span style="color:rgb(0,75,159);font-style:italic" class="">REGEXP </span>= <br class=""> <span style="font-style:italic" class="">withToString</span>(<span style="" class="">obj </span>-> <span style="" class="">Pattern</span>.<span style="font-style:italic" class="">quote</span>(<span style="" class="">obj</span>.toString())).andThen(<span style="" class="">Pattern</span>::<span style="font-style:italic" class="">compile</span>);<br class=""></pre>
</div>
</div>
<div class=""><br class="">
</div>
<div class="">What do you think?</div>
<div class=""><br class="">
</div>
<div class="">With best regards,</div>
<div class="">Tagir Valeev</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</body>
</html>