分类: Java
2009-06-05 15:04:17
5、Groovy Math
l Groovy支持访问所有的Java Math类和操作
l 为了使math操作在脚本编写时尽可能直观,Groovy math模型支持文字化math操作
l 缺省计算使用的是精确的小数(BigDecimal),如:
1.1 + 0.1 == 1.2返回的是true,而不是false(不象在Java中使用float或double)
(1)数字的文字表示
l Groovy的小数文字表示是java.math.BigDecimal的实例,而不是浮点类型(Float或Double)
l Float和Double可以使用后面讲的后缀(F和D)方法来创建
l 小数的指数形式也支持,如12.3e-23
l 十六进制和八进制也支持,十六进制前缀0x,八进制前缀0
l 整数类型可以使用后面讲的后缀(I、L和G)方法来创建,如果不指定根据数值的大小使用合适的类型
l 数字类型的后缀文字表示
|
_Type_ |
_Suffix_ |
|
_BigInteger_ |
G |
|
_Long_ |
L |
|
_Integer_ |
I |
|
_BigDecimal_ |
(缺省) |
|
_Double_ |
D |
|
_Float_ |
F |
l 例子:
assert 42I == new Integer("42");assert 123L == new Long("123");assert 2147483648 == new Long("2147483648"); //Long type used, value too large for an Integerassert 456G == new java.math.BigInteger("456");assert 123.45 == new java.math.BigDecimal("123.45"); //default BigDecimal type usedassert 1.200065D == new Double("1.200065");assert 1.234F == new Float("1.234");assert 1.23E23D == new Double("1.23E23");(2)Math操作
l Groovy的Math实现很接近Java 1.5 BigDecimal Math模型的实践
l Java.lang.Number包括其子类的二元操作(除了除法)会根据下表自动转换参数类型
|
|
_BigDecimal_ |
_BigInteger_ |
_Double_ |
_Float_ |
_Long_ |
_Integer_ |
|
_BigDecimal_ |
BigDecimal |
BigDecimal |
Double |
Double |
BigDecimal | |
|
_BigInteger_ |
BigDecimal |
BigInteger |
Double |
Double |
BigInteger |
BigInteger |
|
_Double_ |
Double |
Double |
Double |
Double |
Double |
Double |
|
_Float_ |
Double |
Double |
Double |
Double |
Double |
Double |
|
_Long_ |
BigDecimal |
BigInteger |
Double |
Double |
Long |
Long |
|
_Integer_ |
BigDecimal |
BigInteger |
Double |
Double |
Long |
Integer |
l 注意:Byte、Character、Short都作为Integer类型
(3)除法
l 除法操作“/”和“/=”在操作数中有Float或Double类型时,结果为Double类型;其它情况,结果为BigDecimal类型
l BigDecimal类型的操作会这样做:
BigDecimal.divide(BigDecimal right, , BigDecimal.ROUND_HALF_UP) 其中
l 例子:
1/2 == new java.math.BigDecimal("0.5");1/3 == new java.math.BigDecimal("0.3333333333");2/3 == new java.math.BigDecimal("0.6666666667");l 整型除法使用“\”和“\=”操作,返回整型类型
l 由于“\”是Java中的转义符,在字符串中使用需要转义
" x = 8\\3 "(4)数字文字表示语法
IntegerLiteral: Base10IntegerLiteral HexIntegerLiteral OctalIntegerLiteral Base10IntegerLiteral: Base10Numeral IntegerTypeSuffix (optional) HexIntegerLiteral: HexNumeral IntegerTypeSuffix (optional) OctalIntegerLiteral: OctalNumeral IntegerTypeSuffix (optional) IntegerTypeSuffix: one of i I l L g G Base10Numeral: 0 NonZeroDigit Digits (optional) Digits: Digit Digits Digit Digit: 0 NonZeroDigit NonZeroDigit: one of \1 2 3 4 5 6 7 8 9 HexNumeral: 0 x HexDigits 0 X HexDigits HexDigits: HexDigit HexDigit HexDigits HexDigit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F OctalNumeral: 0 OctalDigits OctalDigits: OctalDigit OctalDigit OctalDigits OctalDigit: one of 0 1 2 3 4 5 6 7 DecimalPointLiteral: Digits . Digits ExponentPart (optional) DecimalTypeSuffix (optional) . Digits ExponentPart (optional) DecimalTypeSuffix (optional) Digits ExponentPart DecimalTypeSuffix (optional) Digits ExponentPart (optional) DecimalTypeSuffix (optional) ExponentPart: ExponentIndicator SignedInteger ExponentIndicator: one of e E SignedInteger: Signopt Digits Sign: one of + - DecimalTypeSuffix: one of f F d D g G6、I/O
l Groovy提供许多有用的方法来处理I/O,包括标准的Java Reader/Writer、InputStream/OutputStream、File和URL类
l 使用闭包允许处理资源时确保正确关闭而不管是否有异常,例如下面的例子遍历文件的每一行,即使闭包中发生异常,文件也能正确关闭:
import java.io.Filenew File("foo.txt").eachLine { println it }
l 使用Reader/Writer:通过闭包处理资源
import java.io.Filenew File("foo.txt").withReader { reader |
while (true) { line = reader.readLine() ... }}l Groovy提供简单的方法执行命令行进程,表达式返回java.lang.Process实例,具有in/out/err流(译者:没有测试过)
process = "ls -l".execute()
process.in.eachLine { line | println line }
7、逻辑分支
(1)if-else语句
l Groovy提供Java相同的if-else语句
x = falsey = falseif ( !x ) { x = true}assert x == trueif ( x ) { x = false} else { y = true}assert x == yl Groovy也支持三元操作符
y = 5x = (y > 1) ? "worked" : "failed"assert x == "worked"(2)switch语句
l Groovy的switch语句兼容Java代码,不同之处在于Groovy的switch语句能够处理各种类型的switch值,可以做各种类型的匹配
Ø case值为类名匹配switch值为类实例
Ø case值为正则表达式匹配switch值的字符串匹配该正则表达式
Ø case值为集合匹配switch值包含在集合中,这包括ranges
Ø 除了上面的,case值与switch值相等才匹配
x = 1.23result = ""switch ( x ) { case "foo": result = "found foo" // lets fall through case "bar": result += "bar" case [4, 5, 6, 'inList']: result = "list" break case 12..30: result = "range" break case Integer: result = "integer" break case Number: result = "number" break default: result = "default"}assert result == "number"l switch语句的工作原理:switch语句在做匹配case值时调用isCase(switchValue)方法,缺省调用equals(switchValue),但是已经被重载成各种类型,如类,正则表达式、集合等等
l 可以创建自定义的匹配类,增加isCase(switchValue)方法来提供自定义的匹配类型