Java Reference
In-Depth Information
and the
this
reference refers to the object where the category is invoked. The analogous
implementation for the currency category is
import java.text.NumberFormat
@Category(Number)
class AnnotationCurrencyCategory {
String asCurrency() {
NumberFormat.currencyInstance.format(this)
}
String asCurrency(Locale loc) {
NumberFormat.getCurrencyInstance(loc).format(this)
}
}
Number.mixin AnnotationCurrencyCategory
def amount = 1234567.89012
println amount.asCurrency()
println amount.asCurrency(Locale.GERMANY)
println amount.asCurrency(new Locale('hin','IN'))
Note also the use of the
mixin
method to add the category to the
Number
class.
Presumably, if the
ServletCategory
was being implemented now, it would use the an-
1
The topic source code includes the two ways of doing the currency category as well as a test case.
An example will make this clear. The next listing shows a class called
HelloName-Ser-
vlet
, implemented in Groovy, which receives a
name
parameter and replies with the
standard welcome.