我有一个服务方法,如果方法参数为null / blank或不是数字,则必须抛出错误.
调用者发送一个Integer值但在被调用方法中如何检查它是数字还是null.
例如:
- def add(value1,value2){
- //have to check value1 is null/blank
- //check value1 is numeric
- }
- caller: class.add(10,20)
任何建议都将不胜感激.
解决方法
更具体的是
answer of Dan Cruz,可以使用
String.isInteger()
方法:
- def isValidInteger(value) {
- value.toString().isInteger()
- }
- assert !isValidInteger(null)
- assert !isValidInteger('')
- assert !isValidInteger(1.7)
- assert isValidInteger(10)
但是如果我们为我们的方法传递一个看起来像Integer的String会发生什么:
- assert !isValidInteger('10') // FAILS
我认为最简单的解决方案是使用instanceof运算符,所有断言都是有效的:
- def isValidInteger(value) {
- value instanceof Integer
- }