DaveAKing / guava-libraries

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

Feature Request: fluent conditional add/put on ImmutableBuilders #1390

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi!
Fluent interfaces of builders on ImmutableCollections are really nice but we 
can't use this feature if we have a conditional put/add like the example below:

ImmutableMap.Builder<String,Object> builder = ImmutableMap.<String,Object> 
builder().put("obj1",obj1);

if( obj2 != null ){
   builder.put( "obj2",obj2 );
}

return builder.build();

I think it would be really nice to have a method to do this, with a similar 
signature:
//On ImmutableMap 
Builder<K,V> putIf( boolean condition, K key, V value );

//On ImmutableList and ImmutableSet
Builder<V> addIf( boolean condition, V value );

With this feature the example above would look like this:

return ImmutableMap.<String,Object> builder()
   .put("obj1",obj1)
   .putIf(obj2 != null, "obj2", obj2 );

Original issue reported on code.google.com by luizcc...@gmail.com on 29 Apr 2013 at 1:10

GoogleCodeExporter commented 9 years ago
Why not use Map<String, Optional<?>> instead: 

ImmutableMap.<String, Optional<?>> builder()
    .put("obj1", Optional.fromNullable(couldBeNull))
.build();

Original comment by christof...@vimond.com on 29 Apr 2013 at 5:56

GoogleCodeExporter commented 9 years ago
The conditional I add is only to give an example, but with the signature I 
suggest it could be any condition (in my case the conditions wasn't a not null 
condition). I aggre the optional solves the not null but I think the suggest 
method has more uses

Original comment by luizcc...@gmail.com on 29 Apr 2013 at 6:07

GoogleCodeExporter commented 9 years ago
There has to be a point of diminishing returns, where pushing an operation into 
a builder just isn't worthwhile anymore.  Could you explain why you think that 
conditions are under that cutoff?  (Most builders I've seen in other contexts 
don't have this sort of thing.)

Original comment by lowas...@google.com on 29 Apr 2013 at 6:09

GoogleCodeExporter commented 9 years ago
Hi,

I think it's better if I give other example:

private Map<Property,Object> propertiesToChange(
    final Entity1 entity1,
    final Entity2 entity2,
    final Entity3 entity3 )
{
    final Builder<Property,Object> paramsBuilder = ImmutableMap.<Property,Object> builder().put(
        Property.ENTITY2, entity2.getId() );

    if( ! entity3.equals( entity1.getEntity3() ) ) {
        paramsBuilder.put( Property.ENTITY3, entity3.getId() );
    }
    return paramsBuilder.build();
}

Original comment by luizcc...@gmail.com on 29 Apr 2013 at 6:20

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 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

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