{"id":2260,"date":"2011-04-18T10:55:31","date_gmt":"2011-04-18T08:55:31","guid":{"rendered":"https:\/\/test.viaboxx.de\/2011\/04\/18\/groovy-salutes-you\/"},"modified":"2021-08-11T10:00:24","modified_gmt":"2021-08-11T10:00:24","slug":"groovy-salutes-you","status":"publish","type":"post","link":"https:\/\/www.viaboxx.de\/en\/blog\/groovy-salutes-you\/","title":{"rendered":"Groovy salutes you!"},"content":{"rendered":"\n<p>Some time ago in one of my previous projects, we had to generate customer salutations in emails and SMS texts in various ways, like these example show:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Dear Mr. John Miller\", \"Dear Mrs. Dr. Miller\", \"Miller, John\"<\/pre>\n\n\n\n<p>The task was nasty because sometimes the user object had a first name or a title, but some fields were optional so that it could result in:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Dear Mr. null Miller\" (when firstname was null) or\n\"Miller, \" (when firstname as \"\")\n<\/pre>\n\n\n\n<p>It showed, that a utility class would be useful, that helps to generate the salutation strings and fill the gaps between \u201cfirstname\u201d, \u201clastname\u201d, \u201ctitle\u201d etc. only when the parts are not empty. A java implementation named \u201cSalutationUtil\u201d was implemented, consisting of a 230 line class \u201cSalutationParser\u201d, that parsed an expression string and a 140 line class \u201cSalutationUtil\u201d to present an API. We used a ResourceBundle to fill the \u201cDear\u201d and \u201cMr.\u201d parts according to the language to be used and the gender of the person to be addressed.<\/p>\n\n\n\n<p>Now, in our current project, we had the same problem and I was thinking to reuse the java implementation. No problem, because although we are using Grails\/Groovy nowadays, the \u201cSalutationUtil\u201d would still be compatible. After some minutes of looking at the java code, I dropped it and replaced the whole \u201cSalutationUtil\u201d and the parser-class with a 40 line Groovy implementation \u201cSalutation.groovy\u201d, which shows a much simpler approach to the same problem using a Groovy closure to create the salutation string. Here is the code, may it be useful for the rest of the world:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[codesyntax lang=\"java\"]\n&lt;pre>class Salutation {\n  private StringBuilder buf = new StringBuilder()\n  private boolean touched, said\n  String defaultSalutation = ''\n \n  static format(Closure builder, String defaultSalutation = '') {\n    return new Salutation(defaultSalutation: defaultSalutation).salute(builder)\n  }\n  \n  String salute(Closure builder) {\n    with builder\n    return toString()\n  }\n \n  String toString() {\n    if (touched) {\n      return buf.toString()\n    } else {\n     return defaultSalutation\n    }\n  }\n \n  void add(def text) {\n    if (text) {\n      touched = true\n      buf.append(text)\n      said = true\n    } else {\n      said = false\n    }\n  }\n \n  void gap(def text) {\n    if (text &amp;amp;&amp;amp; said) {\n      touched = true\n      buf.append(text)\n      said = false\n    }\n  }\n}&lt;\/pre>\n[\/codesyntax]<\/pre>\n\n\n\n<p>It helps to separate the optional properties (firstname, lastname, title, \u2026) with gaps to generate salutation strings. Here is an example how you can use the class (the \u2018person\u2019 Map is a placeholder for <strong>your<\/strong> User-Domain object)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[codesyntax lang=\"groovy\"]\n&lt;pre>\/\/ Example 1\ndef person = [firstName: 'Peter', lastName: 'Miller', title: 'Dr', gender: 'male']\n \ndef salutation = Salutation.format({\n add(person.gender=='male':'Mr.' ?'Mrs.')\n add(person.title)\n gap(' ')\n add(person.firstName)\n gap(' ')\n add(person.lastName)\n }) \/\/ results in =&amp;gt; \"Mr. Dr Peter Miller\"\n \n\/\/ Example 2\ndef person = [firstName: 'Peter', lastName: 'Miller']\n \ndef salutation = Salutation.format({\n add(person.title)\n gap(' ')\n add(person.lastName)\n gap(', ')\n add(person.firstName)\n }) \/\/ results in =&amp;gt; \"Miller, Peter\"\n \n\/\/ Example 3\ndef person = [lastName: 'Miller']\n \ndef salutation = Salutation.format({\n add(person.title)\n gap(' ')\n add(person.lastName)\n gap(', ')\n add(person.firstName)\n }, \"customer\") \/\/ results in =&amp;gt; \"Miller\"\n \n\/\/ Example 4\ndef person = [:]\n \ndef salutation = Salutation.format({\n add(person.title)\n gap(' ')\n add(person.lastName)\n gap(', ')\n add(person.firstName)\n }, \"customer\") \/\/ results in =&amp;gt; \"customer\"&lt;\/pre>\n[\/codesyntax]<\/pre>\n\n\n\n<p>A salute to Groovy! To write this post took more time than to implement the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago in one of my previous projects, we had to generate customer salutations in emails and SMS texts in various ways, like these example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2260","post","type-post","status-publish","format-standard","hentry","category-code"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Simon Tiffert","author_link":"https:\/\/www.viaboxx.de\/en\/blog\/author\/simon-tiffertviaboxx-de\/"},"uagb_comment_info":0,"uagb_excerpt":"Some time ago in one of my previous projects, we had to generate customer salutations in emails and SMS texts in various ways, like these example [&hellip;]","_links":{"self":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2260"}],"collection":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/comments?post=2260"}],"version-history":[{"count":2,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2260\/revisions"}],"predecessor-version":[{"id":7060,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2260\/revisions\/7060"}],"wp:attachment":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/media?parent=2260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/categories?post=2260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/tags?post=2260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}