diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index c3b031e52..6934f230c 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -21,6 +21,7 @@ import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -77,6 +78,79 @@ public ObjectUtils() { super(); } + // Empty checks + //----------------------------------------------------------------------- + /** + *
Checks if an Object is empty or null.
+ * + * The following types are supported: + *+ * ObjectUtils.isEmpty(null) = true + * ObjectUtils.isEmpty("") = true + * ObjectUtils.isEmpty("ab") = false + * ObjectUtils.isEmpty(new int[]{}) = true + * ObjectUtils.isEmpty(new int[]{1,2,3}) = false + * ObjectUtils.isEmpty(1234) = false + *+ * + * @param object the {@code Object} to test, may be {@code null} + * @return {@code true} if the object has a supported type and is empty or null, + * {@code false} otherwise + */ + public static boolean isEmpty(final Object object) { + if (object == null) { + return true; + } + if (object instanceof CharSequence) { + return ((CharSequence) object).length() == 0; + } + if (object.getClass().isArray()) { + return Array.getLength(object) == 0; + } + if (object instanceof Collection>) { + return ((Collection>) object).isEmpty(); + } + if (object instanceof Map, ?>) { + return ((Map, ?>) object).isEmpty(); + } + return false; + } + + /** + *
Checks if an Object is not empty and not null.
+ * + * The following types are supported: + *+ * ObjectUtils.isNotEmpty(null) = false + * ObjectUtils.isNotEmpty("") = false + * ObjectUtils.isNotEmpty("ab") = true + * ObjectUtils.isNotEmpty(new int[]{}) = false + * ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true + * ObjectUtils.isNotEmpty(1234) = true + *+ * + * @param object the {@code Object} to test, may be {@code null} + * @return {@code true} if the object has an unsupported type or is not empty + * and not null, {@code false} otherwise + */ + public static boolean isNotEmpty(final Object object) { + return !isEmpty(object); + } + // Defaulting //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 661722dac..2ec631fcb 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -31,9 +31,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.mutable.MutableObject; @@ -47,6 +52,13 @@ public class ObjectUtilsTest { private static final String FOO = "foo"; private static final String BAR = "bar"; + private static final String[] NON_EMPTY_ARRAY = new String[] { FOO, BAR, }; + private static final List