简体   繁体   中英

Equivalent of Guava's Strings.isNullOrEmpty() for Long, Integer, etc

在诸如Java盒装数字基元类型的番石榴等实用程序库中是否有类似.isNullOrZero()的东西?

A Integer, Long can not be empty if it is not null ; it can be zero in this case. So for Integer or Long you can write something like this -

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, if it is not null then it can be empty. Then you can write your own isNullOrEmpty() method like this -

public static boolean isNullOrEmpty( final Collection< ? > collection ) {
    return (collection == null || collection.isEmpty() );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM