I have a method which takes an argument Collection
, which could be NULL. I want to end up with a local copy of the input as an ImmutableS
I don't see why you couldn't use Objects.firstNonNull
:
this.foos = ImmutableSet.copyOf(Objects.firstNonNull(foos, ImmutableSet.of()));
You can save some typing with static imports, if that's your thing:
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.ImmutableSet.of;
// snip...
this.foos = copyOf(Objects.firstNonNull(foos, of()));
A Collection
is a reference like any other, so you could do:
ImmutableSet.copyOf(Optional.fromNullable(foos).or(ImmutableSet.of()));
But that is becoming quite a handful to write. More simple:
foos == null ? ImmutableSet.of() : ImmutableSet.copyOf(foos);