{"id":3673,"date":"2011-04-15T00:48:30","date_gmt":"2011-04-14T19:18:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3673"},"modified":"2022-01-12T19:33:57","modified_gmt":"2022-01-12T14:03:57","slug":"groovy-few-ways-to-convert-string-into-enum","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-few-ways-to-convert-string-into-enum\/","title":{"rendered":"Groovy: Few ways to convert string into enum"},"content":{"rendered":"<p>Many at times, we have a string which needs to be converted into Enum. I will be sharing few options as stated by <a href=\"http:\/\/mrhaki.blogspot.com\/2010\/12\/groovy-goodness-transform-string-into.html\">Mr.Haki<\/a>, Isa Goksu and in the last the one I discovered during the process.\u00a0Lets say we have a Enum AccountType as given below :<\/p>\n<p>[groovy]<\/p>\n<p>enum AccountType {<br \/>\n     CHECKING,<br \/>\n     SAVING<br \/>\n}<\/p>\n<p>assert AccountType.CHECKING == &amp;quot;CHECKING&amp;quot; as AccountType<\/p>\n<p>assert AccountType.CHECKING == AccountType.valueOf(&amp;quot;CHECKING&amp;quot;)<br \/>\ndef className = AccountType.class<br \/>\nassert AccountType.CHECKING == Enum.valueOf(className, &amp;quot;CHECKING&amp;quot;)<\/p>\n<p>assert AccountType.CHECKING == AccountType[&amp;quot;CHECKING&amp;quot;]<br \/>\nString type = &amp;quot;CHECKING&amp;quot;<br \/>\nassert AccountType.CHECKING == AccountType[type]<\/p>\n<p>[\/groovy]<\/p>\n<p>Cheers!<br \/>\n~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many at times, we have a string which needs to be converted into Enum. I will be sharing few options as stated by Mr.Haki, Isa Goksu and in the last the one I discovered during the process.\u00a0Lets say we have a Enum AccountType as given below : [groovy] enum AccountType { CHECKING, SAVING } assert [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":62},"categories":[7],"tags":[557,555,9,556],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3673"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3673"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3673\/revisions"}],"predecessor-version":[{"id":54615,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3673\/revisions\/54615"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}