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 NON_EMPTY_LIST = Arrays.asList(NON_EMPTY_ARRAY); + private static final Set NON_EMPTY_SET = new HashSet<>(NON_EMPTY_LIST); + private static final Map NON_EMPTY_MAP = new HashMap<>(); + static { + NON_EMPTY_MAP.put(FOO, BAR); + } //----------------------------------------------------------------------- @Test @@ -59,6 +71,41 @@ public void testConstructor() { assertFalse(Modifier.isFinal(ObjectUtils.class.getModifiers())); } + //----------------------------------------------------------------------- + @Test + public void testIsEmpty() { + assertTrue(ObjectUtils.isEmpty(null)); + assertTrue(ObjectUtils.isEmpty("")); + assertTrue(ObjectUtils.isEmpty(new int[] {})); + assertTrue(ObjectUtils.isEmpty(Collections.emptyList())); + assertTrue(ObjectUtils.isEmpty(Collections.emptySet())); + assertTrue(ObjectUtils.isEmpty(Collections.emptyMap())); + + assertFalse(ObjectUtils.isEmpty(" ")); + assertFalse(ObjectUtils.isEmpty("ab")); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_ARRAY)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_LIST)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_SET)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_MAP)); + } + + @Test + public void testIsNotEmpty() { + assertFalse(ObjectUtils.isNotEmpty(null)); + assertFalse(ObjectUtils.isNotEmpty("")); + assertFalse(ObjectUtils.isNotEmpty(new int[] {})); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptyList())); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptySet())); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptyMap())); + + assertTrue(ObjectUtils.isNotEmpty(" ")); + assertTrue(ObjectUtils.isNotEmpty("ab")); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_ARRAY)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_LIST)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_SET)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_MAP)); + } + //----------------------------------------------------------------------- @Test public void testIsNull() {