在java盒装数字基本类型的Guava等util库中是否有类似.isNullOrZero()的东西?
A整数,如果不是null
, Long不能为空;在这种情况下它可以是零。对于整型或长型你可以这样写-
public static boolean isNullOrZero( final Object obj) {
if(null == obj) return true;
if( obj instanceof Integer ){
Integer i = (Integer) obj;
return (i == 0);
}
if( obj instanceof Long ){
Long l = (Long) obj;
return (l == 0);
}
}
For Collection,如果不为空,则可以为空。然后您可以编写自己的isNullOrEmpty()
方法,如下所示-
public static boolean isNullOrEmpty( final Collection< ? > collection ) {
return (collection == null || collection.isEmpty() );
}