I have a class which contains an optional Map
:
private Optional<ImmutableMap<String, String>> stuff;
In my class constructor I am passed Map<String, String> inputStuff
, where inputStuff
could be:
null
- an empty
Map
- a populated
Map
For the first two instances I need to store Optional.absent()
and for the third instance I need to store an Optional
immutable copy of the map. The best that I can come up with in terms of handling this is:
final ImmutableMap<String, String> tmp = ImmutableMap.copyOf(Objects.firstNonNull(inputStuff, ImmutableMap.<String, String>of()));
if (inputStuff.isEmpty())
{
this.stuff = Optional.absent();
}
else
{
this.stuff = Optional.of(inputStuff);
}
Is there a cleaner way to handle this?