• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return Iterator of interface type  RSS feed

 
Toke Noer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my "application" I only use interface types from the domian in my GUI layer.
So I have in the domain layer:
public class Table implements ITable {
List<Section> sections; // Need to be Section, since I'm calling functions not available through ISection
}

This section list I want to export the iterator for, in a typesafe manner. So the ITable interface has:
Iterator<ISection> getSectionIterator();

Section implements ISection ofc.

But the only way I can get this to work, is to use the following code i Table:
public Iterator<ISection> getSectionIterator() {
Iterator iter = sections.iterator();
return iter;
}

But this doesn't look nice IMO, first getting the Iterator (without generics) and then return it as a ISection iterator, but I can't see any other way to return a Iterator<ISection> from my List<Section>. Anything obvious I'm missing here? Weird design maybe? I don't want to return Iterator<Section>, because I only want to export the functions known by ISection.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Return an Iterator<? extends ISection>. You will limit callers from using any interface other than ISection, and allow your implementation to return Iterator<Section>.
 
Toke Noer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the hint. Didn't know about that syntax for what I understand is co/contra variance.

Just to be sure I used it correctly, here is what I have:



And in the code where I have an ITable object, I need to use this syntax, right?

Can't say I like the syntax for getting the iterator though. I wish it was just:

But that won't compile for me.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code looks good to me. More important, it compiles :-)

Toke Noer wrote:
Can't say I like the syntax for getting the iterator though. I wish it was just:

But that won't compile for me.


There has to be extra syntax, though. There is a real difference between guaranteeing every object in the collection is of the exact type X, and guaranteeing every object in the collection is either of type X or some sub-type of X. So there would need to be different syntax to indicate the two situations:

1) all objects in the collection are of the exact type X ==> Iterator<X>
2) all objects in the collection are assignable to the type X ==> Iterator<? extends X>

I guess it could have been less wordy, but on the other hand it is rather expressive so it works well in a language like Java which tends to favor readability at the cost of wordiness over pithiness at the cost of readability.
 
Toke Noer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help and explanation. Appreciated!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!