You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/08/21 13:35:56 UTC
[commons-lang] branch master updated: Lang 1689 add optional to objectutils isempty (#933)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new eb07b19f4 Lang 1689 add optional to objectutils isempty (#933)
eb07b19f4 is described below
commit eb07b19f4a23d443a1fa1005c344d78f79c0f110
Author: Joseph Hendrix <he...@aol.com>
AuthorDate: Sun Aug 21 09:35:49 2022 -0400
Lang 1689 add optional to objectutils isempty (#933)
* LANG-1689: return the negation of Optional.isPresent when checking if an Optional is empty
* LANG-1689: test whether an optional is empty
* LANG-1689: test whether an optional is NOT empty
* LANG-1689 use spaces not tabs
* LANG-1689 update JavaDoc to reflect use of Optional
* LANG-1689 remove empty line to match code style from before changes
Co-authored-by: hendrixjoseph <he...@aol.com>
---
src/main/java/org/apache/commons/lang3/ObjectUtils.java | 11 +++++++++++
src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java | 9 +++++++++
2 files changed, 20 insertions(+)
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 8f7dfd513..e51131973 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -30,6 +30,7 @@ import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.TreeSet;
import java.util.function.Supplier;
@@ -1019,6 +1020,7 @@ public class ObjectUtils {
* <li>{@link 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>
+ * <li>{@link Optional}: Considered empty if {@link Optional#isPresent} returns false, regardless of the "emptiness" of the contents.</li>
* </ul>
*
* <pre>
@@ -1028,6 +1030,9 @@ public class ObjectUtils {
* ObjectUtils.isEmpty(new int[]{}) = true
* ObjectUtils.isEmpty(new int[]{1,2,3}) = false
* ObjectUtils.isEmpty(1234) = false
+ * ObjectUtils.isEmpty(1234) = false
+ * ObjectUtils.isEmpty(Optional.of("")) = false
+ * ObjectUtils.isEmpty(Optional.empty()) = true
* </pre>
*
* @param object the {@link Object} to test, may be {@code null}
@@ -1051,6 +1056,9 @@ public class ObjectUtils {
if (object instanceof Map<?, ?>) {
return ((Map<?, ?>) object).isEmpty();
}
+ if (object instanceof Optional<?>) {
+ return !((Optional<?>) object).isPresent();
+ }
return false;
}
@@ -1063,6 +1071,7 @@ public class ObjectUtils {
* <li>{@link 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>
+ * <li>{@link Optional}: Considered empty if {@link Optional#isPresent} returns false, regardless of the "emptiness" of the contents.</li>
* </ul>
*
* <pre>
@@ -1072,6 +1081,8 @@ public class ObjectUtils {
* ObjectUtils.isNotEmpty(new int[]{}) = false
* ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true
* ObjectUtils.isNotEmpty(1234) = true
+ * ObjectUtils.isNotEmpty(Optional.of("")) = true
+ * ObjectUtils.isNotEmpty(Optional.empty()) = false
* </pre>
*
* @param object the {@link Object} to test, may be {@code null}
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index fc55943f9..88d1b8bf0 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -42,6 +42,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
@@ -609,6 +610,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
assertTrue(ObjectUtils.isEmpty(Collections.emptyList()));
assertTrue(ObjectUtils.isEmpty(Collections.emptySet()));
assertTrue(ObjectUtils.isEmpty(Collections.emptyMap()));
+ assertTrue(ObjectUtils.isEmpty(Optional.empty()));
+ assertTrue(ObjectUtils.isEmpty(Optional.ofNullable(null)));
assertFalse(ObjectUtils.isEmpty(" "));
assertFalse(ObjectUtils.isEmpty("ab"));
@@ -616,6 +619,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_LIST));
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_SET));
assertFalse(ObjectUtils.isEmpty(NON_EMPTY_MAP));
+ assertFalse(ObjectUtils.isEmpty(Optional.of(new Object())));
+ assertFalse(ObjectUtils.isEmpty(Optional.ofNullable(new Object())));
}
/**
@@ -663,6 +668,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
assertFalse(ObjectUtils.isNotEmpty(Collections.emptyList()));
assertFalse(ObjectUtils.isNotEmpty(Collections.emptySet()));
assertFalse(ObjectUtils.isNotEmpty(Collections.emptyMap()));
+ assertFalse(ObjectUtils.isNotEmpty(Optional.empty()));
+ assertFalse(ObjectUtils.isNotEmpty(Optional.ofNullable(null)));
assertTrue(ObjectUtils.isNotEmpty(" "));
assertTrue(ObjectUtils.isNotEmpty("ab"));
@@ -670,6 +677,8 @@ public class ObjectUtilsTest extends AbstractLangTest {
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_LIST));
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_SET));
assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_MAP));
+ assertTrue(ObjectUtils.isNotEmpty(Optional.of(new Object())));
+ assertTrue(ObjectUtils.isNotEmpty(Optional.ofNullable(new Object())));
}
@Test