You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2018/08/20 17:02:41 UTC

[1/3] [lang] LANG-1411: Add empty checks to ObjectUtils

Repository: commons-lang
Updated Branches:
  refs/heads/master c2bd8be86 -> d1e72ebed


LANG-1411: Add empty checks to ObjectUtils


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/2a116425
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/2a116425
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/2a116425

Branch: refs/heads/master
Commit: 2a11642511a3c69bac5aa1abd83d3219871395e3
Parents: c3de2d6
Author: Alexander Tsvetkov <al...@gmail.com>
Authored: Fri Aug 17 08:47:07 2018 +0300
Committer: Alexander Tsvetkov <al...@gmail.com>
Committed: Fri Aug 17 09:02:46 2018 +0300

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ObjectUtils.java   | 74 ++++++++++++++++++++
 .../apache/commons/lang3/ObjectUtilsTest.java   | 47 +++++++++++++
 2 files changed, 121 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2a116425/src/main/java/org/apache/commons/lang3/ObjectUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index c3b031e..6934f23 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.io.Serializable;
 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 class ObjectUtils {
         super();
     }
 
+    // Empty checks
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Checks if an Object is empty or null.</p>
+     *
+     * The following types are supported:
+     * <ul>
+     * <li>{@link CharSequence}: Considered empty if its length is zero.</li>
+     * <li>{@code Array}: Considered empty if its length is zero.</li>
+     * <li>{@link Collection}: Considered empty if it has zero elements.</li>
+     * <li>{@link Map}: Considered empty if it has zero key-value mappings.</li>
+     * </ul>
+     *
+     * <pre>
+     * 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
+     * </pre>
+     *
+     * @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;
+    }
+
+    /**
+     * <p>Checks if an Object is not empty and not null.</p>
+     *
+     * The following types are supported:
+     * <ul>
+     * <li>{@link CharSequence}: Considered empty if its length is zero.</li>
+     * <li>{@code Array}: Considered empty if its length is zero.</li>
+     * <li>{@link Collection}: Considered empty if it has zero elements.</li>
+     * <li>{@link Map}: Considered empty if it has zero key-value mappings.</li>
+     * </ul>
+     *
+     * <pre>
+     * 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
+     * </pre>
+     *
+     * @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
     //-----------------------------------------------------------------------
     /**

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2a116425/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 661722d..2ec631f 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.lang.reflect.Modifier;
 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 @@ import org.junit.Test;
 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<String> NON_EMPTY_LIST = Arrays.asList(NON_EMPTY_ARRAY);
+    private static final Set<String> NON_EMPTY_SET = new HashSet<>(NON_EMPTY_LIST);
+    private static final Map<String, String> NON_EMPTY_MAP = new HashMap<>();
+    static {
+        NON_EMPTY_MAP.put(FOO, BAR);
+    }
 
     //-----------------------------------------------------------------------
     @Test
@@ -61,6 +73,41 @@ public class ObjectUtilsTest {
 
     //-----------------------------------------------------------------------
     @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() {
         final Object o = FOO;
         final Object dflt = BAR;


[2/3] [lang] Merge branch 'LANG-1411'

Posted by br...@apache.org.
Merge branch 'LANG-1411'


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/44e32205
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/44e32205
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/44e32205

Branch: refs/heads/master
Commit: 44e32205a6f98f50af95e3940a48aa72971e9483
Parents: c2bd8be 2a11642
Author: Benedikt Ritter <br...@apache.org>
Authored: Mon Aug 20 18:59:21 2018 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Mon Aug 20 18:59:21 2018 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ObjectUtils.java   | 74 ++++++++++++++++++++
 .../apache/commons/lang3/ObjectUtilsTest.java   | 47 +++++++++++++
 2 files changed, 121 insertions(+)
----------------------------------------------------------------------



[3/3] [lang] LANG-1411: Add changes.xml entry and @since tags.

Posted by br...@apache.org.
LANG-1411: Add changes.xml entry and @since tags.

This fixes #342 from GitHub. Thanks to Alexander Tsvetkov.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/d1e72ebe
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/d1e72ebe
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/d1e72ebe

Branch: refs/heads/master
Commit: d1e72ebede93d73d1a3caf74e391fd83516e0685
Parents: 44e3220
Author: Benedikt Ritter <br...@apache.org>
Authored: Mon Aug 20 19:01:46 2018 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Mon Aug 20 19:01:46 2018 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                                 | 3 ++-
 src/main/java/org/apache/commons/lang3/ObjectUtils.java | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d1e72ebe/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4a11676..5e87a0b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,7 +45,8 @@ The <action> type attribute can be add,update,fix,remove.
   </properties>
   <body>
 
-  <release version="3.8" date="????-??-??" description="??">
+  <release version="3.9" date="????-??-??" description="??">
+    <action issue="LANG-1411" type="add" dev="britter" due-to="Alexander Tsvetkov">Add isEmpty method to ObjectUtils</action>
   </release>
 
   <release version="3.8" date="2018-08-15" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d1e72ebe/src/main/java/org/apache/commons/lang3/ObjectUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 6934f23..4a84c4f 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -103,6 +103,7 @@ public class ObjectUtils {
      * @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
+     * @since 3.9
      */
     public static boolean isEmpty(final Object object) {
         if (object == null) {
@@ -146,6 +147,7 @@ public class ObjectUtils {
      * @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
+     * @since 3.9
      */
     public static boolean isNotEmpty(final Object object) {
         return !isEmpty(object);