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 <hendixjoseph@aol.com>
This commit is contained in:
parent
db606c65b0
commit
eb07b19f4a
|
@ -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}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue