You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/08/15 19:05:15 UTC

[GitHub] [commons-lang] hendrixjoseph commented on pull request #933: Lang 1689 add optional to objectutils isempty

hendrixjoseph commented on PR #933:
URL: https://github.com/apache/commons-lang/pull/933#issuecomment-1215620301

   I was thinking about this, and if we wanted to look at the contents of the Optional rather than the optional itself, there are three ways I've though of doing it - two recursive and two non-recursive:
   
   ## Recursive with Functional Methods:
   
   ```java
   if (object instanceof Optional<?>) {
       return ((Optional<?>) object).map(ObjectUtils::isEmpty).orElse(false);
   }
   ```
   
   ## Recursive with Non-Functional Methods:
   
   ```java
   if (object instanceof Optional<?>) {
       return ((Optional<?>) object).isPresent() && isEmpty(((Optional<?>) object).get());
   }
   ```
   
   ## Non-recursive
   
   ```java
   public static boolean isEmpty(Object object) {
       if (object instanceof Optional<?>) {
           object = ((Optional<?>) object).orElse(null);
       }
       if (object == null) {
           return true;
       }
       if (object instanceof CharSequence) {
           return ((CharSequence) object).length() == 0;
       }
       if (isArray(object)) {
           return Array.getLength(object) == 0;
       }
       if (object instanceof Collection<?>) {
           return ((Collection<?>) object).isEmpty();
       }
       if (object instanceof Map<?, ?>) {
           return ((Map<?, ?>) object).isEmpty();
       }
       return false;
   }
   ```
   
   I like the non-recursive method best, however, it does change the `Object object` parameter from final to non-final.
   
   ---
   
   Also, there is an isEmpty() method on optionals that was added in Java 11: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html#isEmpty()


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org