migrator / guava-libraries

Guava: Google Core Libraries for Java 1.6+
0 stars 0 forks source link

com.google.common.collect.ImmutableCollection.Builder<E> should contain a method to add an Optional<E> if present #47

Open migrator opened 10 years ago

migrator commented 10 years ago

It would be really useful if com.google.common.collect.ImmutableCollection contained a method of the following.

public com.google.common.collect.ImmutableSet.Builder add(Optional element);

and

public com.google.common.collect.ImmutableSet.Builder add(Optional... element);

which is adds to the builder if element.isPresent() is true.

If such a method existed bolier plate code like the following could be avoided

if (element.isPresent()) { builder.add(element.get()); }

relevance: 2

migrator commented 10 years ago

summary: Not Defined

You could just do

builder.addAll(element.asSet());

or, with Java 8's Optional, you'll be able to do

element.ifPresent(builder::add);

status Not Defined creator: lowas...@google.com created at: Jul 16, 2014

migrator commented 10 years ago

summary: Not Defined

builder.addAll(element.asSet());

Feels like smell and I am not sure if there are performance considerations of converting a single Optional value to a Set just to be added to the Builder as well.

Second requires use of Java 8 rather than Guava for Optional which has trade offs. Not to mention the requirement of actually using Java 8.

Again this is a nice to have enhancement, but I feel it is so simple and practical, not to mention fits with the paradigm that I am suppressed that something like this does not exist.

status Not Defined creator: anderson...@gmail.com created at: Jul 16, 2014

migrator commented 10 years ago

summary: Not Defined

\ surprised

status Not Defined creator: anderson...@gmail.com created at: Jul 16, 2014