I have checked similar questions and not found any that quite deal with this particular angle of the problem (that is, the specific problem of adapting cleanly an IEnumerator to an IIterator).
I am currently converting a (fairly large) Java project to C#. As anyone with experience of doing this may be aware, although visually the code between Java and C# looks similar there are some significant functional differences between Java and C#. C# is a tighter language, mainly I think for readability.
One of the core differences between Java and C# is IIterator in Java and IEnumerable in C#.
I am trying to adapt IEnumerator to IIterator. The problem is IIterator in Java has a .hasNext() method - which checks whether a next element is available without actually advancing the iterator to the next element - whereas IEnumerable has only the ability to return the current element in the iteration; and cannot inform you whether a next element is available without actuallyadvancing the iterator to the next element (you cannot go back).
My question is (and this may be due to my inexperience with Java) - whereas I can see IEnumerator may be able to iterate across all elements in a forward-only, readonly way withoutfirst evaluating all elements in the IEnumerator, how can IIterator provide the 'peek ahead' (.hasNext()) functionality without having a read-ahead evaluation requiring some or all elements in the iteration to be executed ahead of the current element? This seems to be self-defeating, in that the main iterator interface IIterator requires read-ahead - which is by concept incompatible with element-by-element iteration?
I ask this particular question because, currently, in order to adapt an IEnumerator and return an IIterator, I need to first execute the IEnumerator in order to provide a .HasNext property -which seems to defeat the purpose of a IEnumerator which does not require all elements to be evaluated before iterating through in a forward-only manner.
Is this just some misunderstanding on the purpose of an IIterator that I have, in that they are not meant to be equivalent? For example if I implemented an IIterator for reading the rows in a data table, it seems to me I would have to at least execute a cursor read of the next row on advancing through each element in the iterator to provide the HasNext functionality?
In sum, if anyone has a sound adaption of IEnumerator to IIterator I would be grateful if they would share.
Thanks
Markos101