okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

nullToEmpty for collections? #1476

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What about this little cutie: 

    @SuppressWarnings("unchecked")
    public static <T, C extends Collection<T>> C nullToEmpty(C collection) {
        return firstNonNull(collection, (C) Collections.emptyList());
    }

This is one I really often need and would wish to have it in Collections2.
Maybe I miss another short way, but this one looks much more ugly:

    for(AbstractFooBarBlah fooBar : firstNonNull(collection, Collections.<AbstractFooBarBlah>emptyList())) {
      ...
    }

then this:

    for(AbstractFooBarBlah fooBar : nullToEmpty(collection)) {
      ...
    }

Greetings Danny

Original issue reported on code.google.com by DannyLade on 15 Jul 2013 at 12:12

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 15 Jul 2013 at 1:34

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08