maidh91 / guava-libraries

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

ImmutableCollection.Builder interface #396

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
ImmutableSortedSet.Builder extends ImmutableSet.Builder, but it would also be 
nice if there were a common interface uniting ImmutableSet.Builder and 
ImmutableList.Builder.

Original issue reported on code.google.com by ray.j.gr...@gmail.com on 11 Aug 2010 at 11:38

GoogleCodeExporter commented 9 years ago
Explain why this would be useful, please. Note that we never intended these 
builders to be used in any other way than simple create/populate/build. I don't 
think they're suitable for passing around through APIs and such, which is the 
only reason I can imagine for this request; though you may finally have the 
examples that convince me otherwise?

Original comment by kevinb@google.com on 8 Sep 2010 at 6:17

GoogleCodeExporter commented 9 years ago
It's really not that big of a deal. I have some code that can parse some 
compressed information and return a variety of Collection types. I pass my own 
Builder class into the inner armpit of the parser, and when that returns a 
method is called on my builder to return the collection. I can use one Builder 
class for any mutable Collection:

<snippet>
            final Collection<T> collection = createCollection(fclass, encoded); // may create a HashSet, or ArrayList, or who knows what.
            return new Builder<Collection<T>>() {
                public void add (T t) {
                    collection.add(t);
                }
                public Collection<T> build () {
                    return collection;
                }
            };

But if I want to build an immutable collection, I currently need to create a 
different Builder for ImmutableSet or ImmutableList...

               return new Builder<Collection<T>>() {
                    public void add (T t) {
                        _builder.add(t);
                    }
                    public Collection<T> build () {
                        return _builder.build();
                    }
                    protected ImmutableSet.Builder<T> _builder = ImmutableSet.builder();
                };

If I could create an ImmutableCollection.Builder from a method similar to how I 
create my Collection in the first example, then one anonymous Builder class 
could be shared for ImmutableSet and ImmutableCollection.

I completely understand if you consider this not worth adding a new interface 
to Guava.

Original comment by ray.j.gr...@gmail.com on 10 Sep 2010 at 11:16

GoogleCodeExporter commented 9 years ago
Issue 488 has been merged into this issue.

Original comment by kevinb@google.com on 27 Jan 2011 at 1:15

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 28 Jan 2011 at 5:03

GoogleCodeExporter commented 9 years ago
I guess http://code.google.com/p/guava-libraries/source/detail?r=442 solves 
this issue :)

Original comment by nev...@gmail.com on 3 Jun 2011 at 1:38

GoogleCodeExporter commented 9 years ago
Yes, if there is a common base interface or abstract class that both Builders 
are based on?
It does not look like this is the case, looking at the change, unless I am 
missing something.

Original comment by tsaloranta@gmail.com on 3 Jun 2011 at 4:03

GoogleCodeExporter commented 9 years ago
All builders seem to extend ImmutableCollection.Builder, which has been made 
public in r442:

http://code.google.com/p/guava-libraries/source/browse/trunk/guava/src/com/googl
e/common/collect/ImmutableList.java#551

http://code.google.com/p/guava-libraries/source/browse/trunk/guava/src/com/googl
e/common/collect/ImmutableSet.java#550

http://code.google.com/p/guava-libraries/source/browse/trunk/guava/src/com/googl
e/common/collect/ImmutableCollection.java#270

Original comment by nev...@gmail.com on 3 Jun 2011 at 5:35

GoogleCodeExporter commented 9 years ago
Oh. Ok, I did miss something then (related change(s) to make other builders 
extend it). :)

Original comment by tsaloranta@gmail.com on 4 Jun 2011 at 2:33

GoogleCodeExporter commented 9 years ago
No problem. I could have been more explicit in my previous comment ;)

Original comment by nev...@gmail.com on 4 Jun 2011 at 11:33

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago
Will be in release 10:
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collec
t/ImmutableCollection.Builder.html

Original comment by cpov...@google.com on 13 Jul 2011 at 8:34

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

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