kucci / guava-libraries

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

Suggestion: FilteredView interface? #438

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This could be considered a follow-up to <a 
href="http://code.google.com/p/guava-libraries/issues/detail?id=314">issue 
314</a>.

As you know, a List can implement the marker interface RandomAccess to hint 
that it supports fast random access.

Perhaps any filtered Collection view that would require complete iteration to 
determine size() could implement a similar marker interface: FilteredView. 
Presently Collections2.filter() returns a FilteredCollection, but that is not a 
public class.

Then, code that wanted to could check if the Collection implemented the 
interface and avoid calling size() in preference to just iterating the 
elements. Essentially, it could treat the Collection as a plain Iterable.

For the example in Issue 314, this would first make a copy of the provided 
Collection in an ArrayList, and then build the ImmutableSet from that. But: it 
would only apply the Predicate once to each element. This becomes even more 
important if one is doing a non-trivial transformation and then filtering the 
results.

I suppose if one was thinking about all this, one could just avoid using the 
Collections2 methods in the first place, and use their counterparts in 
Iterables.

The major PITA I see with this suggestion is that, much like RandomAccess adds 
complexity to creating a view of a List (often two code paths are involved, one 
to generate a view that is also marked as RandomAccess, one not), so too would 
this multiply the non-public view classes that exist behind the scenes. For 
example, Collections2.transform() would want to "pass along" the FilteredView 
interface on the result it returned if its argument was itself a FilteredView.

Original issue reported on code.google.com by ray.j.gr...@gmail.com on 1 Oct 2010 at 12:46

GoogleCodeExporter commented 9 years ago
Corrected link to issue 314: 
http://code.google.com/p/guava-libraries/issues/detail?id=314

Original comment by ray.j.gr...@gmail.com on 1 Oct 2010 at 12:46

GoogleCodeExporter commented 9 years ago
Screw it! It would have to go too many places, with too little benefit.

Original comment by ray.j.gr...@gmail.com on 24 Oct 2010 at 3:52

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 5 Apr 2011 at 8:49

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