Groovy作为Java的一个有益补充,在最近的工作中使用日益增多。
今天主要是谈Groovy里的Truth对象,特别关注Array,Map,StringBuilder的实例。
//true
if(1) // any non-zero value is true
if(-1)
if(!null) // any non-null value is true
if("John" ) // any non-empty string is true
Map family = [dad:"John" , mom:"Jane" ]
if(family) // true since the map is populated
String[] sa = new String[1]
if(sa) // true since the array length is greater than 0
StringBuffer sb = new StringBuffer()
sb.append("Hi" )
if(sb) // true since the StringBuffer is populated
//false
if(0) // zero is false
if(null) // null is false
if("" ) // empty strings are false
Map family = [:]
if(family) // false since the map is empty
String[] sa = new String[0]
if(sa) // false since the array is zero length
StringBuffer sb = new StringBuffer();
if(1) // any non-zero value is true
if(-1)
if(!null) // any non-null value is true
if("John" ) // any non-empty string is true
Map family = [dad:"John" , mom:"Jane" ]
if(family) // true since the map is populated
String[] sa = new String[1]
if(sa) // true since the array length is greater than 0
StringBuffer sb = new StringBuffer()
sb.append("Hi" )
if(sb) // true since the StringBuffer is populated
//false
if(0) // zero is false
if(null) // null is false
if("" ) // empty strings are false
Map family = [:]
if(family) // false since the map is empty
String[] sa = new String[0]
if(sa) // false since the array is zero length
StringBuffer sb = new StringBuffer();
if(sb) // false since the StringBuffer is empty