Tuesday, September 29, 2009

Groovy JDK (GDK): Number Support

The Groovy JDK (GDK) provides several convenience methods related to numbers as part of its Number class extension. All of the numeric classes that extend Number inherit these methods.

The following Groovy script demonstrates use of several of the convenience methods on the GDK's Number class.


numberPi = Math.PI
numberE = Math.E
negativeFifteen = -15
positiveTen = 10
zero = 0
println "$numberPi is a ${numberPi.getClass().name}"
println "$numberE is a ${numberE.getClass().name}"
println "$negativeFifteen is a ${negativeFifteen.getClass().name}"

println "\n===== Number.abs() ====="
println "Number.abs() for $negativeFifteen: ${negativeFifteen.abs()}"

println "\n===== Number.downto(Number, Closure) ====="
positiveTen.downto(1) {println "$it / 2 = ${it / 2}"}

println "\n===== Number.upto(Number, Closure) ====="
zero.upto(5) {println "$it * 5 = ${it * 5}"}

println "\n===== Number.times(Closure) ====="
3.times() {print it}
println ""

println "\n===== Number.power(Number) ====="
println "5.power(2) = ${5.power(2)}"
println "5**2 = ${5**2}"

println "\n===== Number.multiply(Number) ====="
println "5.multiply(3) = ${5.multiply(3)}"
println "5*3 = ${5*3}"

println "\n===== Number.div(Number) ====="
println "25.div(5) = ${25.div(5)}"
println "25/5 = ${25/5}"

println "\n====== Number.intdiv(Number) ====="
println "13intdiv(2) = ${13.intdiv(2)}"
println "(int) (13/2) = ${(int) (13/2)}"

println "\n===== Number.plus(Number) ====="
println "1.1.plus(0.2) = ${1.1.plus(0.2)}"
println "1.1 + 0.2 = ${1.1 + 0.2}"

println "\n===== Number.plus(String) ====="
println "10.plus('Four'): ${10.plus('Four')}"
println "10 + 'Four': ${10 + 'Four'}"

println "\n===== Number.minus(Number) ====="
println "10.0.minus(9.9) = ${10.0.minus(9.9)}"
println "10.0 - 9.9 = ${10.0 - 9.9}"

println "\n===== Number.mod(Number) ====="
println "11.mod(2) = ${11.mod(2)}"
println "11 % 2 = ${11 % 2}"

println "\n===== Number.previous() ====="
println "11.previous() = ${11.previous()}"
println "--11 = ${--11}"

println "\n===== Number.next() ====="
println "5.5.next() = ${5.5.next()}"
println "++5.5 = ${++5.5}"

println "\n===== Number.leftShift(Number) ====="
println "8.leftShift(3) = ${8.leftShift(3)}"
println "8 << 3 = ${8 << 3}"

println "\n===== Number.rightShift(Number) ====="
println "8.rightShift(3) = ${8.rightShift(3)}"
println "8 >> 3 = ${8 >> 3}"


The output from the above script is shown in the next screen snapshots.





From the Groovy source code and its associated output, it is evident that the Groovy GDK provides many highly useful and convenient features. Among other things, the use of closures is demonstrated. Closures allow blocks of functionality to be passed as parameters to methods on the Number class.

The example above also demonstrates that the Groovy Number methods directly support the basic mathematical operators. Groovy code can either use these operators with their symbols or invoke the implementing named methods directly.

There are some nice "extras" in the Number class such as the absolute value method (abs()) and the closure-powered times() method. The leftShift() and rightShift() methods respectively provide numbers represented by left-shifting and right-shifting the underlying bits.

The Groovy JDK (GDK) provides the Groovy developer with many convenient methods. This blog posting has demonstrated only some of the convenient features exposed by the GDK's Number extension. In previous blog posts, I have covered other pieces of the GDK such as String, File generally, parsing File contents as String, and deleting a directory with the GDK File.

No comments: