Making Java Groovy: JavaRanch this week

This week Making Java Groovy is the featured book at JavaRanch, where I’ve been a member for many years. JavaRanch is yet another of Kathy Sierra’s (and Bert Bates’s) contributions to the community, which started out as a certification study site and evolved into an excellent message board. Until StackOverflow came along, it was my favorite place for getting answers to technical questions. It’s still an excellent example of how a friendly non-trolling technical community can operate.

If you drop by and ask a question in the Groovy forum, you can win a copy of my book.

Last week was a very busy one for me. I gave four talks at the NFJS event in Seattle, then went from there to the SpringOne2GX conference in Santa Clara, and then did the NFJS event in Boston area (Framingham, to be specific). I had a great time at all three events.

One of my favorite moments was in my “Advanced Groovy: Tips and Tricks” talks, when I noticed Paul King walk into the room. If you’ve read any of my book, you know I’m a big fan of his. In addition to being a Groovy committer, he’s also a co-author on Groovy in Action. If you’re interested in anything Groovy, be sure to check out his presentations at SlideShare.net.

While I was able to handle most of the questions, Paul did show me a few items after I was finished. For example, running collect on a map is even easier than I thought. To build a query string, I used to do this:
[sourcecode language=”groovy”]
String qs = [a:1, b:2, c:3].collect { k,v -> "$k=$v" }.join(‘&’)
assert qs == ‘a=1&b=2&c=3’
[/sourcecode]
Paul noticed I did that in a blog post and reminded me that the toString method on Map.Entry returns “key=value”, so I can reduce that to:
[sourcecode language=”groovy”]
String qs = [a:1, b:2, c:3].collect { it }.join(‘&’)
assert qs == ‘a=1&b=2&c=3’
[/sourcecode]
It turns out that now there’s even a default collect method that returns it, so I can make this even simpler:
[sourcecode language=”groovy”]
String qs = [a:1, b:2, c:3].collect().join(‘&’)
assert qs == ‘a=1&b=2&c=3’
[/sourcecode]
I need the parentheses on the collect method (or the compiler will think it’s a property), but it’s hard to get any simpler than that.

Another interesting question came up when I was talking about metaprogramming. For years I knew you could add a method to a class by adding a new property to its metaclass. For example, I can add a cook method to Map:
[sourcecode language=”groovy”]
Map.metaClass.cook = { BigDecimal purity -> "$purity% pure meth" }
assert ‘85% pure meth’ == [a:1, b:2].cook(85)
[/sourcecode]
(Sorry about the drug example. Like so many people, I’m totally caught up in the last few episodes of Breaking Bad. And btw, I have no idea what the actual ingredients are for meth, though I know one of them involves ephedra because I have to sign for my allergy medication at the pharmacy now.)

The question that came up was, can I overload that method using the same technique? I can certainly create a closure with different arguments, but can I assign it to the same property without messing up the original assignment?

Here’s an alternative implementation of the cook method:
[sourcecode language=”groovy”]
import java.awt.Color

Map.metaClass.cook = { Color c -> "$c-tinted meth" }
assert ‘java.awt.Color[r=0,g=0,b=255]-tinted meth’ ==
[a:1, b:2].cook(Color.blue)
[/sourcecode]
I can assign the two different implementations to the same property:
[sourcecode language=”groovy”]
import java.awt.Color

Map.metaClass.cook = { Color c -> "$c-tinted meth" }
Map.metaClass.cook = { BigDecimal purity -> "$purity% pure meth" }
assert ‘85% pure meth’ == [a:1, b:2].cook(85)
assert ‘java.awt.Color[r=0,g=0,b=255]-tinted meth’ ==
[a:1, b:2].cook(Color.blue)
[/sourcecode]
It still looks funny to me, but it works under the hood. Good to know.

Paul would probably appreciate it if I also mention that the example I chose was my own. He didn’t suggest drugs at any stage of development.

If you get a chance, please drop by the Groovy forum at JavaRanch this week and say hi. As another reminder, my Making Java Groovy presentation at JavaOne is Monday morning at 10am, so if you’re there, please drop by. I gave away over 20 print copies of my book at SpringOne2GX, and I’m sure I’ll have more at JavaOne. I’ll even deface sign one for you if you like. 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.