Commons Lang
  1. Commons Lang
  2. LANG-531

Add defaultIfEmpty or nullIfEmpty to ArrayUtils

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: 2.4
    • Fix Version/s: 3.0
    • Component/s: lang.*
    • Labels:
      None

      Description

      StringUtils has method defaultIfEmpty and ObjectUtils has defaultIfNull. A parallel method is missing from ArrayUtils, and would provide a shorthand way of normalizing arrays. My first inclination was nullIfEmpty because no simple default value is readily available or easy to specify. But Henri mentioned that defaultIfEmpty might better parallel the other implementations. That option would work okay for me, as I can just specify null as the default.

      1. LANG-531.patch
        2 kB
        Henri Yandell

        Activity

        Hide
        Henri Yandell added a comment -

        Adding patch.

        Show
        Henri Yandell added a comment - Adding patch.
        Hide
        Henri Yandell added a comment -

        On reflection, my patch isn't what you were asking for. I created:

        + public static <T> T[] defaultIfNull(T[] array, T[] defaultValue)

        { + return array != null ? array : defaultValue; + }

        Thinking that that allows you to set a default value if array is null. I'm not sure you were asking for that - feels more that you were asking for:

        + public static <T> T[] defaultIfEmpty(T[] array, T[] defaultValue)

        { + return (array != null && array.length != 0) ? array : defaultValue; + }

        Which you would call as: array = defaultIfEmpty(array, null)

        That seems a little odd to me. To consider an empty array to be bad and want to override it with something else (such as null as you're suggesting above). Am I understanding correctly what you wanted?

        Show
        Henri Yandell added a comment - On reflection, my patch isn't what you were asking for. I created: + public static <T> T[] defaultIfNull(T[] array, T[] defaultValue) { + return array != null ? array : defaultValue; + } Thinking that that allows you to set a default value if array is null. I'm not sure you were asking for that - feels more that you were asking for: + public static <T> T[] defaultIfEmpty(T[] array, T[] defaultValue) { + return (array != null && array.length != 0) ? array : defaultValue; + } Which you would call as: array = defaultIfEmpty(array, null) That seems a little odd to me. To consider an empty array to be bad and want to override it with something else (such as null as you're suggesting above). Am I understanding correctly what you wanted?
        Hide
        Guy Rouillier added a comment -

        Yes, you are understanding correctly. For me, the benefit of these defaultIfEmpty() methods is to scrub incoming data. If I have a data bean and one of its members is an array, then the set method needs to look something like this:

        void setMyArray(Object[] array)

        { this.array = ArrayUtils.isEmpty(array) ? null, array; }

        Instead, the following is a little bit more intuitive:

        this.array = ArrayUtils.nullIfEmpty(array);

        So, I originally suggested nullIfEmpty, but Henri thought defaultIfEmpty would better parallel other Utils classes.

        Show
        Guy Rouillier added a comment - Yes, you are understanding correctly. For me, the benefit of these defaultIfEmpty() methods is to scrub incoming data. If I have a data bean and one of its members is an array, then the set method needs to look something like this: void setMyArray(Object[] array) { this.array = ArrayUtils.isEmpty(array) ? null, array; } Instead, the following is a little bit more intuitive: this.array = ArrayUtils.nullIfEmpty(array); So, I originally suggested nullIfEmpty, but Henri thought defaultIfEmpty would better parallel other Utils classes.
        Hide
        Henri Yandell added a comment -

        Resolving as wontfix. No interest shown on list - I think the nullIfEmpty use case isn't a very common one. It's common to protect from null, rather than introduce null.

        Show
        Henri Yandell added a comment - Resolving as wontfix. No interest shown on list - I think the nullIfEmpty use case isn't a very common one. It's common to protect from null, rather than introduce null.

          People

          • Assignee:
            Unassigned
            Reporter:
            Guy Rouillier
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development