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:
[sourcecode language=”groovy”]
import java.text.NumberFormat

class CurrencyCategory {
static String asCurrency(Number amount) {
NumberFormat.currencyInstance.format(amount)
}

static String asCurrency(Number amount, Locale loc) {
NumberFormat.getCurrencyInstance(loc).format(amount)
}
}
[/sourcecode]
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:
[sourcecode language=”groovy”]
import java.text.NumberFormat

@Category(Number)
class AnnotationCurrencyCategory {
String asCurrency() {
NumberFormat.currencyInstance.format(this)
}

String asCurrency(Locale loc) {
NumberFormat.getCurrencyInstance(loc).format(this)
}
}
[/sourcecode]
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:
[sourcecode language=”groovy”]
public class CurrencyCategoryTest {
public static final String EURO = ‘\u20ac’
def amount = 1234567.89012

@Test
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’
}
}

@Test
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’
}
}
[/sourcecode]

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.

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

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

Discover more from Stuff I've learned recently...

Subscribe now to keep reading and get access to the full archive.

Continue reading