Java Object NVL isEmpty(Object obj)

Here you can find the source of isEmpty(Object obj)

Description

Like the Groovy empty except doesn't consider empty 0 value numbers, false Boolean, etc; only null values, 0 length String (actually CharSequence to include GString, etc), and 0 size Collection/Map are considered empty.

License

Creative Commons License

Declaration

public static boolean isEmpty(Object obj) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .j a  v  a  2 s  .co m*/
 * This software is in the public domain under CC0 1.0 Universal plus a
 * Grant of Patent License.
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

import java.util.*;

public class Main {
    /** Like the Groovy empty except doesn't consider empty 0 value numbers, false Boolean, etc; only null values,
     *   0 length String (actually CharSequence to include GString, etc), and 0 size Collection/Map are considered empty. */
    public static boolean isEmpty(Object obj) {
        if (obj == null)
            return true;
        /* faster not to do this
        Class objClass = obj.getClass();
        // some common direct classes
        if (objClass == String.class) return ((String) obj).length() == 0;
        if (objClass == GString.class) return ((GString) obj).length() == 0;
        if (objClass == ArrayList.class) return ((ArrayList) obj).size() == 0;
        if (objClass == HashMap.class) return ((HashMap) obj).size() == 0;
        if (objClass == LinkedHashMap.class) return ((HashMap) obj).size() == 0;
        // hopefully less common sub-classes
        */
        if (obj instanceof CharSequence)
            return ((CharSequence) obj).length() == 0;
        if (obj instanceof Collection)
            return ((Collection) obj).size() == 0;
        return obj instanceof Map && ((Map) obj).size() == 0;
    }
}

Related

  1. isEmpty(Object value)
  2. isEmpty(Object... obj)
  3. isNotEmpty(Object object)
  4. isNullOrEmpty(final Object obj)