package my.groovy.category; import java.math.BigDecimal; import java.math.BigInteger; public class MyNumberCategory { /** A singleton Number instance to guarantee an exactly traceable return * value for demonstrating the category method overriding issue. */ public static final Number POW_RETURN = Double.valueOf( 1.2345 ); // When the base value is a Number sub-class that has a pow(int) method // (like BigDecimal or BigInteger), this method is not called in Groovy 1.5, // but was in Groovy 1.0. // // The Groovy 1.0 behavior seems to be the desired behavior, you would like // to be able to have a category method augmenting all sub-classes of a // parent class (or interface even). I did notice that Groovy 1.0 won't // work if the first argument of the category method is an interface type // versus a parent class. // public static Number pow( final Number base, final int exp ) { return POW_RETURN; } // Uncomment this method and the category works for instances of BigDecimal in Groovy 1.5 // public static Number pow( final BigDecimal base, final int exp ) { // return POW_RETURN; // } // Uncomment this method and the category works for instances of BigInteger in Groovy 1.5 // public static Number pow( final BigInteger base, final int exp ) { // return POW_RETURN; // } }