How to write a generic isEmpty method which can check for null, empty?

This sounds like a bad design to me. Null is null, empty is empty, if it's a string it's a string, and so on. Don't try to jam everything up in one method. It's bad for maintainability and readability.

if (str == null || str.isEmpty())
    ...

and

if (coll == null || coll.isEmpty())

are both perfectly fine.

Personally however, I try to never ever equate null with an empty string or empty collection. I think it's a bad practice. A null collection is no collection at all, an empty collection is in fact still a collection. You can avoid many if (coll == null) checks by keeping a collection non-null. If you're worried about memory consumption, use use Collections.emptySet et al.


That being said, if you still want to go in this direction, I'd suggest you use plain method overloading and create one isEmpty(Collection<?> coll) and one isEmpty(String str) to avoid instanceof and casting.


Regarding your edit:

Don't do for instance

if (value == null || value.isEmpty()) {
    return true;
}
return false;

just do

return value == null || value.isEmpty();

For collections, you'll want to use isEmpty() instead of size(). For some collection types (such as LinkedList), size() is more expensive than isEmpty().


I like to have a utility class in a common library that handles this. Note that we use the object's own isEmpty, length, or size methods (in that order) if the object has one (after determining the object isn't null). By making calls to this, one doesn't need to worry about NPEs anymore -- you make your calls to this and you are good-to-go -- if it's true, then your collection/map/etc isn't null and has something in it; if it's false, skip over the item (it's either null or empty by it's own account). The second method checks an array to see if it's null or empty, but doesn't check the contents. When you iterate over the array, you simply do a check, then iterate, and as you iterate, check each element.

/**
 * Provides methods to perform input validation and boundary validation.
 */
public final class ValidationUtils {
    /**
     * Check to see if Object is empty or null.
     *
     * @param object
     *            The object to check
     * @return boolean {@code true} iff the Object is null or determined to be empty (using methods that it provides --
     *         if it doesn't provide such methods, it's only empty if it's null)
     */
    public static boolean isEmpty(@Nullable final Object object) {
        if (object == null)
            return true;

        try {
            // Try to use the object class's isEmpty method to check if we're empty, now that we know we're
            // not NULL
            final Method method = object.getClass().getMethod("isEmpty");
            final Object result = method.invoke(object);

            if (result instanceof Boolean)
                return Boolean.class.cast(result).booleanValue();
        } catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
                | IllegalAccessException | SecurityException ignored) {
            // We couldn't invoke... let's go to the next common method
        }

        try {
            // Try to use the object class's length method to check if we're empty, now that we know we're
            // not NULL
            final Method method = object.getClass().getMethod("length");
            final Object result = method.invoke(object);

            if (result instanceof Integer)
                return Integer.class.cast(result).intValue() <= 0;
            if (result instanceof Long)
                return Long.class.cast(result).longValue() <= 0L;
        } catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
                | IllegalAccessException | SecurityException ignored) {
            // We couldn't invoke... let's go to the next common method
        }

        try {
            // Try to use the object class's size method to check if we're empty, now that we know we're
            // not NULL
            final Method method = object.getClass().getMethod("size");
            final Object result = method.invoke(object);

            if (result instanceof Integer)
                return Integer.class.cast(result).intValue() <= 0;
            if (result instanceof Long)
                return Long.class.cast(result).longValue() <= 0L;
        } catch (@NotNull final NoSuchMethodException | InvocationTargetException | IllegalArgumentException
                | IllegalAccessException | SecurityException ignored) {
            // We couldn't invoke... but we're not null... treat it like an Object
        }

        // Let's treat it like an Object... we're not null, so we're not empty
        return false;
    }

    /**
     * Check to see if the array of Objects is empty or null.
     *
     * @param obj
     *            Object Array to check
     * @return boolean true if empty
     */
    public static boolean isEmpty(@Nullable final Object... obj) {
        return ((obj == null) || (obj.length == 0));
    }
}

Example uses:

    final Map<String, String[]> postData = ServletActionContext.getRequest().getParameterMap();
    // We're testing if the map is null or empty... we could just do a null check here because of how we're using the map after, but...
    if (!ValidationUtils.isEmpty(postData)) {
        for (final Map.Entry<String, String[]> reqKey : postData.entrySet()) {
            // We're checking if the array is null or doesn't have any length; again, the foreach does the latter, but this is perfectly fine
            if (!ValidationUtils.isEmpty(reqKey.getValue())) {
                for (final String value : reqKey.getValue()) {
                    // Checking the value
                    if (ValidationUtils.isEmpty(value)) {
                        continue;
                    }

                    ...
                }
            }
        }
    }