{"id":6549,"date":"2012-08-27T15:30:57","date_gmt":"2012-08-27T10:00:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6549"},"modified":"2016-12-19T14:46:40","modified_gmt":"2016-12-19T09:16:40","slug":"evaluating-expressions-with-groovy-util-eval","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/evaluating-expressions-with-groovy-util-eval\/","title":{"rendered":"Evaluating Expressions with groovy.util.Eval"},"content":{"rendered":"<p>A few days ago I faced a problem of evaluating dynamic expressions on domain objects. The scenario was that, depending upon the user input, I needed to sort a list of objects on some properties, which were nested deep in the object, e.g.,<\/p>\n<p>[java]<br \/>\nstudent.course?.college?.name  \/\/ if a user chose to sort students by college name<br \/>\nstudent.course.teacher.name \/\/ if a user chose to sort students by teacher name.<br \/>\n[\/java]<\/p>\n<p>Using if, else could not have been an elegant solution, considering that there were many more fields to be sorted upon. This is when groovy.util.Eval class came to my notice. I found a useful function there:<br \/>\n[java]Eval.x(java.lang.Object x, java.lang.String expression)[\/java]<\/p>\n<p>Its usage is like this:<\/p>\n<p>[java]assert 10 == Eval.x(2, &#8216; x * 4 + 2&#8217;)[\/java]<\/p>\n<p>and I wrote:<\/p>\n<p>[java]<br \/>\nstudents.sort {Eval.x(it, &amp;quot;x.${sortField}&amp;quot;)} \/\/ sortField could be student.course?.college?.name<br \/>\n[\/java]<\/p>\n<p>In addition to this, groovy.util.Eval has some more useful methods like:<\/p>\n<p>[java]<\/p>\n<p>assert 10 == Eval.me(&#8216; 2 * 4 + 2&#8217;)<br \/>\nassert 10 == Eval.xyz(2, 4, 2, &#8216; x * y + z&#8217;)<br \/>\nassert 10 == Eval.xy(2, 4, &#8216; x * y + 2&#8217;)<br \/>\n[\/java]<\/p>\n<p>Hope this helps.<br \/>\nImran Mir<br \/>\nimran[@]intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago I faced a problem of evaluating dynamic expressions on domain objects. The scenario was that, depending upon the user input, I needed to sort a list of objects on some properties, which were nested deep in the object, e.g., [java] student.course?.college?.name \/\/ if a user chose to sort students by college [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":37},"categories":[7],"tags":[896,895,894,4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6549"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6549"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6549\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}