S
Stefan Ram
An iterator object, according to me, has the following three
atomic operations:
V - deliver the current value
A - deliver the current availability
(that is, whether there still is a value available)
I - attempt to increment the state to the next value
How does
http://download.java.net/jdk7/docs/api/java/util/Iterator.html
make these operations available? It has:
next = V + I and
hasNext = A
. How do I prefer to write code for iterators?
class Example
implements de.dclj.ram.Value<T>, de.dclj.ram.Advanceable
{ public T value(){ ... }
public boolean advance(){ ... }}
, where »advance« returns »true« iff the increment operation
was possible. So, I prefer two operations as follows:
value = V and
advance = A + I
(the »I« now has moved from »V« to »A«).
This allows to »peek« at the current value as often as seen
fit without risking to change the state of the iterator
inadvertently. Also, it seems to be natural to me to
/try to advance/ to the next state /and then return/
whether this was a success in one operation.
So, this style for me is both easier and more natural to code
and easier and more natural to use than the »Iterator«
interface of Java SE. Of course, I can't use these objects
directly with Java SE classes that expect an Iterator,
I need to wrap them into an adapter object for this.
Still, I really consider to go this way than to code
directly in the Java Iterator style.
I have no question, but feel free to comment.
atomic operations:
V - deliver the current value
A - deliver the current availability
(that is, whether there still is a value available)
I - attempt to increment the state to the next value
How does
http://download.java.net/jdk7/docs/api/java/util/Iterator.html
make these operations available? It has:
next = V + I and
hasNext = A
. How do I prefer to write code for iterators?
class Example
implements de.dclj.ram.Value<T>, de.dclj.ram.Advanceable
{ public T value(){ ... }
public boolean advance(){ ... }}
, where »advance« returns »true« iff the increment operation
was possible. So, I prefer two operations as follows:
value = V and
advance = A + I
(the »I« now has moved from »V« to »A«).
This allows to »peek« at the current value as often as seen
fit without risking to change the state of the iterator
inadvertently. Also, it seems to be natural to me to
/try to advance/ to the next state /and then return/
whether this was a success in one operation.
So, this style for me is both easier and more natural to code
and easier and more natural to use than the »Iterator«
interface of Java SE. Of course, I can't use these objects
directly with Java SE classes that expect an Iterator,
I need to wrap them into an adapter object for this.
Still, I really consider to go this way than to code
directly in the Java Iterator style.
I have no question, but feel free to comment.