Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1733

Add null-safe Consumers.accept() and Functions.apply()

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 3.15.0
    • None
    • None

    Description

      I have a new suggestion about null handling.

      I usually check a object is null or not before using it to avoid NPE.

      It is pretty obvious, but It is quite cumbersome and has some overhead.

      So I want to introduce the following null-safety methods in ObjectUtils class and make people easy to handle null without using if/else statement or Optional class, etc.

      public static <T, R> R applyIfNotNull(final T object, final Function<T, R> function) {
          return object != null ? function.apply(object) : null;
      }
      
      public static <T> void acceptIfNotNull(final T object, final Consumer<T> consumer) {
          if (object != null) {
              consumer.accept(object);
          }
      }
      

      What do you think about it?

      If it looks good, I will implement this feature.

       

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jay-bae Jongjin Bae
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: