Safe null check in enhanced-for loop

358 views
Skip to first unread message

andres....@gmail.com

unread,
Nov 10, 2013, 2:36:06 PM11/10/13
to guava-...@googlegroups.com
Hi all, 

I'm thinking that it will be nice to have a safe way to iterate from an enhanced-for loop without having to worry about the collection being null.


    public <T> List<T> emptyIfNull(List<T> list) {
        if (list == null) {
            return ImmutableList.of();
        }
        return list;
    }


I can then use it this way:

for (String str : emptyIfNull(listThatMightBeNull)) {
      //do something
}

I prefer this rather than a Collections.forEach() and having to provide a Closure.

Any thoughts?

Louis Wasserman

unread,
Nov 10, 2013, 2:40:17 PM11/10/13
to andres....@gmail.com, guava-...@googlegroups.com
http://stackoverflow.com/a/6921270/869736 says it better than I could: why are you passing null collections around in the first place?


--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/5a75c0de-a459-4b0b-a2bc-be842945bf0b%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Louis Wasserman

Andres Santana

unread,
Nov 10, 2013, 4:11:40 PM11/10/13
to Louis Wasserman, guava-...@googlegroups.com
Well there might be many reasons of why I might get a null Collection, I can mention:

1) JAXB xml-2-object mapping
2) Any other tool generating object and you don't have much control over it
3) Etc




Louis Wasserman

unread,
Nov 10, 2013, 4:16:43 PM11/10/13
to Andres Santana, guava-...@googlegroups.com
That StackOverflow post responds to that question, too:

What if some framework is setting it field to null and you can't control it (spring framework when on binding to list) although you explicitly set and use empty list in the object. 

My answer is the same: try to remove the ambiguity by normalizing null to an empty collection as soon as you can. If you just can't, then okay, you might be one of the <1% of users who really want to write their own trivial isNullOrEmpty(Collection) helper. 
--
Louis Wasserman

Joachim Durchholz

unread,
Nov 10, 2013, 4:48:37 PM11/10/13
to guava-...@googlegroups.com
Am 10.11.2013 20:36, schrieb andres....@gmail.com:
> Hi all,
>
> I'm thinking that it will be nice to have a safe way to iterate from an
> enhanced-for loop without having to worry about the collection being null.
>
>
> public <T> List<T> emptyIfNull(List<T> list) {
> if (list == null) {
> return ImmutableList.of();
> }
> return list;
> }
>
>
> I can then use it this way:
>
> for (String str : emptyIfNull(listThatMightBeNull)) {
> //do something
> }

I'd prefer something like this (the generic may be misplaced):

class Nulls {
/**
* Returns value normally, dflt if value is null.
*/
public static <T>dflt(T value, T dflt {
if (value == null) {
return default;
} else {
return value;
}
}
}

'cause collections isn't the only place where you are fed nulls, they
can come from databases, and in general from misdesigned APIs.
And no, that's not a <1% fraction of programmers who get bitten by this.

This gets me (very fast and loose, not checking the Collections API):

list = Nulls.dflt(listThatMightBeNull, Collections.emptyCollection());
for (String str: list) { ... }

Louis Wasserman

unread,
Nov 10, 2013, 4:49:46 PM11/10/13
to Joachim Durchholz, guava-...@googlegroups.com
@Joachim: That's exactly Guava's Objects.firstNonNull.


--
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss

This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
--- You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/527FFF35.7010501%40durchholz.org.

For more options, visit https://groups.google.com/groups/opt_out.



--
Louis Wasserman

Joachim Durchholz

unread,
Nov 11, 2013, 12:10:23 AM11/11/13
to guava-...@googlegroups.com
Am 10.11.2013 22:49, schrieb Louis Wasserman:
> @Joachim: That's exactly Guava's Objects.firstNonNull.

Ah, not unexpected.
I guess that's the solution for the OP then.

(Very OT: Reply-to-list does not work for guava-discuss in Thunderbird,
not sure about the technical details but I suppose something is wrong
with the message headers.)

Andres Santana

unread,
Nov 14, 2013, 3:00:10 PM11/14/13
to Joachim Durchholz, guava-...@googlegroups.com
Nice.

Thanks.


--- You received this message because you are subscribed to a topic in the Google Groups "guava-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/guava-discuss/b956iZ2D_yM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to guava-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/528066BF.8070007%40durchholz.org.
Reply all
Reply to author
Forward
0 new messages