Categories, old and new, and status of Making Java Groovy

My book, Making Java Groovy, is almost finished. I mean, I’m done, except that I’m not.

(Picture Michael Corleone in “Godfather, Part III”: “Just when I thought I was out, they pull me back in.”)

I thought I was essentially done in early June, after I turned in the last chapter. Then the reviews came back, and most of them were really positive, so I was very hopeful I was actually finished.

That’s when the tech editor chimed in.

In addition to the regular reviewers, Manning hires a tech editor for each book. I didn’t realize what that meant until I saw the result. My tech editor, Valentin, ripped my poor baby apart.

Except he didn’t, really. He just identified all the flaws I knew about and many that I’d missed. He did a fantastic job, actually, darn him.

After recoiling in horror, I eventually developed the following pattern. For each chapter, I would

  1. Read all the comments, trying not to (over)react to them with anger, denial, frustration, or any other emotion
  2. Close the document
  3. Come back at least half a day later, or sometimes a whole day
  4. Read what he actually wrote and start making the changes necessary to improve the book

I would estimate that based on Valentin’s (and the other reviewers’) comments I probably rewrote as much as a quarter of the book during June. In general, I hate rewriting, but the improvements were so obvious I couldn’t even complain about the process, though I tried. I just wish I could have shortened it a bit, but so be it.
The book is much, much better now. I hope you like it too.

After that I finished the rest of the book, meaning the preface*, acknowledgements, part headings, dedication page, adding another appendix**, and uploading all the figures. I finished all that last weekend, so I was finally all done.

* Nobody, and I mean nobody, will write a preface like mine. You’ll understand when you read it. I’m sure no one else would ever add 16 footnotes to a 4 1/2 page preface, with comments ranging from Star Trek to Joseph Campbell to the new cover sheets for T.P.S. reports.***

** Installing Groovy, which is so much easier thanks to GVM.

*** We’re putting new cover sheets on all the T.P.S. reports before they go out now. Did you get the memo?

Except I’m not done, because the graphics people want a different format for many of my figures. Plus, my editor (who spent the last ten days in an igloo in Alaska, or some such nonsense) hasn’t signed off on it yet.

So I’m not really done, but I’m awfully, awfully close. I’m still hoping that the book will be available in print in time for JavaOne in September, where I’m giving a talk entitled, “Making Java Groovy”. Yes, that’s the same name as the book, by an astonishing coincidence. I’m part of a large Groovy contingent at JavaOne this year, which is really groo^H^H^H^H cool.

One side effect of finishing will hopefully be the resurrection of my blog. I stopped blogging when I discovered that there were only so many words I could write in a day, and they really needed to go to the book rather than here. Now that that’s done (hopefully), I can start bringing my blog back to life.

For example, I did far less metaprogramming in the book than I wanted to, but I did come up with a simple example of a category in Groovy, which I implemented both in the old way and the new way.

Here’s the old way, saved in a class called CurrencyCategory.groovy:

import java.text.NumberFormat

class CurrencyCategory {
    static String asCurrency(Number amount) {

    static String asCurrency(Number amount, Locale loc) {

Note that both methods are static, and that their first argument is java.text.NumberFormat. That means the methods are added to the NumberFormat class.

The new way (as of Groovy 2.0, so not all that new, really) is in a class called AnnotationCurrencyCategory.groovy:

import java.text.NumberFormat

class AnnotationCurrencyCategory {
    String asCurrency() {

    String asCurrency(Locale loc) {

This class uses the @Category annotation, and the included methods are now instance methods that are added to class that is the argument of the annotation.

Here’s a test case to make sure they’re working:

public class CurrencyCategoryTest {
    public static final String EURO = '\u20ac'
    def amount = 1234567.89012

    void testCurrencyCategory() {
        use(CurrencyCategory) {
            if (Locale.default == Locale.US) {
                assert amount.asCurrency() == '$1,234,567.89'
            assert amount.asCurrency(Locale.GERMANY) == "1.234.567,89 $EURO"
            assert amount.asCurrency(new Locale('hin','IN')) == 'INR 1,234,567.89'

    void testAnnotatedCurrencyCategory() {
        Number.mixin AnnotationCurrencyCategory
        if (Locale.default == Locale.US) {
            assert amount.asCurrency() == '$1,234,567.89'
        assert amount.asCurrency(Locale.GERMANY) == "1.234.567,89 $EURO"
        assert amount.asCurrency(new Locale('hin','IN')) == 'INR 1,234,567.89'

In the first test, I employ a use block. Inside the use block, the category methods are added to the NumberFormat class. In the second test, I use the mixin method to make the new methods available.

Pretty cool, eh? I have a lot more examples to present, but that’s enough for a single blog post. More to come, on a much more regular basis now.

I’ll let you know when I’m really, really done, at which point I start rolling out the Silly Marketing Ideas (abbreviated SMI, as opposed to the Serious Marketing Ideas, abbreviated SMI). You’ve been warned.

About Ken Kousen
I am a Java Champion and the author of the books "Modern Java Recipes" (O'Reilly Media), "Gradle Recipes for Android" (O'Reilly Media), and "Making Java Groovy" (Manning), as well as over a dozen video courses at Safari Books Online. I'm a regular member of the No Fluff, Just Stuff conference tour and have given talks all over the world. Through my company, Kousen IT, Inc, I've taught training courses to and worked with thousands of developers.

One Response to Categories, old and new, and status of Making Java Groovy

  1. Good example – I always love a good example! so please keep’em coming 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: