You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stefan Bodewig <bo...@apache.org> on 2013/06/03 13:44:35 UTC
General API question: returning Iterator, Iterable or even Collection?
Hi,
in compress I've added a method to ZipFile that provides access to a
subset of the entries. It already contained a method returning an
Enumeration of all entries - Enumeration as it mimics java.util.ZipFile
which was invented in Java 1.0.
For this new method I've chosen to return an Iterator for now - there is
no reason to use Enumeration and it seemed to be closest. In fact I
first implemented it returning Iterable and changed it later.
I've been doing quite a bit of .NET over the past years and there
IEnumerable would have been the no-brainer choice - which translates
into Iterable in Java. Lot's of .NET APIs use IEnumerable.
My personal preference would be Iterable as well as the consumer may
iterate over the return value multiple times without copying stuff
around, but I may be missing something.
There don't seem to be many APIs in the Java classlib that consume
Iterators or Iterables at all. That's why I bring up Collection in the
subject.
Any insights or recommendations what to return?
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Stefan Bodewig <bo...@apache.org>.
On 2013-06-03, Gary Gregory wrote:
> Why not have the class implement Iterable?
Oh, I'm returning just a subset, so making the whole class iterable
doesn't make sense. I'm talking about
public Iterator<ZipArchiveEntry> getEntries(String name)
Stefan
PS: For the general case of "all entries" this might be an option for
Compress 2.x. Even then we'd still need a different method for
returning entries in a different order - central directory order vs
physical order.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Gary Gregory <ga...@gmail.com>.
Why not have the class implement Iterable?
Gary
On Jun 3, 2013, at 7:45, Stefan Bodewig <bo...@apache.org> wrote:
> Iterable
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Stefan Bodewig <bo...@apache.org>.
On 2013-06-03, Matt Benson wrote:
> On Mon, Jun 3, 2013 at 9:15 AM, Jörg Schaible
> <Jo...@scalaris.com>wrote:
>> Emmanuel Bourg wrote:
>>> Le 03/06/2013 13:44, Stefan Bodewig a écrit :
>>>> My personal preference would be Iterable as well as the consumer may
>>>> iterate over the return value multiple times without copying stuff
>>>> around, but I may be missing something.
>>> I like Iterable for the ease of use in a foreach loop. But there is a
>>> risk it uses more memory than necessary. The pros for an Iterator is to
>>> be able to stream the data without holding the whole structure in
>>> memory. I don't know if that makes a real difference for your use case.
>>> An Iterable than can only be iterated once is another solution.
>> An Iterable returns only an Iterator, no need to keep anything in memory
>> except of an (anonymous) Iterable instance.
> +1. I program to Iterable as often as possible. If iteration is all
> that is needed, then... um, it's all that is needed. ;)
Sounds like my gut feeling of "I'd prefer Iterable" wasn't too far off,
then. At least I'm not alone.
"The structure" will be a list that is kept in memory anyway for the
simple case and a temporary sorted version of it - most likely of length
1 - for the not-quite-so-simple case. Shouldn't make a significant
difference.
Thanks
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Matt Benson <gu...@gmail.com>.
On Mon, Jun 3, 2013 at 9:15 AM, Jörg Schaible
<Jo...@scalaris.com>wrote:
> Emmanuel Bourg wrote:
>
> > Le 03/06/2013 13:44, Stefan Bodewig a écrit :
> >
> >> My personal preference would be Iterable as well as the consumer may
> >> iterate over the return value multiple times without copying stuff
> >> around, but I may be missing something.
> >
> > I like Iterable for the ease of use in a foreach loop. But there is a
> > risk it uses more memory than necessary. The pros for an Iterator is to
> > be able to stream the data without holding the whole structure in
> > memory. I don't know if that makes a real difference for your use case.
> > An Iterable than can only be iterated once is another solution.
>
> An Iterable returns only an Iterator, no need to keep anything in memory
> except of an (anonymous) Iterable instance.
>
>
+1. I program to Iterable as often as possible. If iteration is all that
is needed, then... um, it's all that is needed. ;)
Matt
> - Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Jörg Schaible <Jo...@scalaris.com>.
Emmanuel Bourg wrote:
> Le 03/06/2013 13:44, Stefan Bodewig a écrit :
>
>> My personal preference would be Iterable as well as the consumer may
>> iterate over the return value multiple times without copying stuff
>> around, but I may be missing something.
>
> I like Iterable for the ease of use in a foreach loop. But there is a
> risk it uses more memory than necessary. The pros for an Iterator is to
> be able to stream the data without holding the whole structure in
> memory. I don't know if that makes a real difference for your use case.
> An Iterable than can only be iterated once is another solution.
An Iterable returns only an Iterator, no need to keep anything in memory
except of an (anonymous) Iterable instance.
- Jörg
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: General API question: returning Iterator, Iterable or even Collection?
Posted by Emmanuel Bourg <eb...@apache.org>.
Le 03/06/2013 13:44, Stefan Bodewig a écrit :
> My personal preference would be Iterable as well as the consumer may
> iterate over the return value multiple times without copying stuff
> around, but I may be missing something.
I like Iterable for the ease of use in a foreach loop. But there is a
risk it uses more memory than necessary. The pros for an Iterator is to
be able to stream the data without holding the whole structure in
memory. I don't know if that makes a real difference for your use case.
An Iterable than can only be iterated once is another solution.
Emmanuel Bourg
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org