|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javautils.collections.Unfold
Template method for unfolding, or generating, a collection.
Unfolding is related to external iterators like the Iterator
-interface of the Java Collections Framework. While
an external iterator returns elements one-by-one, an unfold produces
the complete collection of elements.
Unlike the Iterator
-interface of the Java
Collections Framework, this template method for unfolding collections
clearly separates non-side-effecting queries and side-effecting
commands. As a consequence, it can be less error prone to write an
unfold than it is to write an iterator.
Because an unfold immediately tells that the intention is to produce a new collection or a sequence of elements. A loop only tells that the code performs iteration, but the intention behind the iteration is not immediately obvious from a loop.
The following example method creates an iterator that generates integers in a given range.
public static Iterator iteratorForIntegers(final int min, final int max) { return newUnfold
() { int next = min; protected booleanmore
() { return next <= max; } protected Objectvalue
() { return new Integer(next); } protected voidadvance
() { ++next; } }.unfoldAsIterator
(); }
Constructor Summary | |
Unfold()
|
Method Summary | |
protected abstract void |
advance()
Advances the internal state of the generator by using side-effects. |
protected void |
init()
Called once before other methods to initialize the unfold. |
protected abstract boolean |
more()
True if and only if there are more elements to generate. |
java.util.List |
unfold()
return unfoldTo (new ArrayList());
|
java.util.Iterator |
unfoldAsIterator()
An iterator that generates the same sequence of elements. |
java.util.Map |
unfoldMap()
return unfoldTo (new HashMap());
|
java.util.List |
unfoldRight()
return unfoldRightTo (new LinkedList());
|
java.util.List |
unfoldRightTo(java.util.LinkedList to)
init ();
while (more ()) {
to.addFirst(value ());
advance ();
}
return to;
|
java.util.Collection |
unfoldTo(java.util.Collection to)
init ();
while (more ()) {
to.add(value ());
advance ();
}
return to;
|
java.util.List |
unfoldTo(java.util.List to)
return (List) unfoldTo ((Collection)to);
|
java.util.Map |
unfoldTo(java.util.Map to)
init ();
while (more ()) {
Map.Entry entry = (Map.Entry)value ();
to.put(entry.getKey(), entry.getValue());
advance ();
}
return to;
|
java.util.Set |
unfoldTo(java.util.Set to)
return (Set) unfoldTo ((Set)to);
|
protected abstract java.lang.Object |
value()
The element to be added to the generated list. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public Unfold()
Method Detail |
protected void init()
Called once before other methods to initialize the unfold.
protected abstract boolean more()
True if and only if there are more elements to generate. This method should not have side-effects.
protected abstract java.lang.Object value()
The element to be added to the generated list. This method should not have side-effects.
protected abstract void advance()
Advances the internal state of the generator by using side-effects.
public final java.util.List unfold()
return unfoldTo
(new ArrayList());
public final java.util.Map unfoldMap()
return unfoldTo
(new HashMap());
public final java.util.Collection unfoldTo(java.util.Collection to)
init
(); while (more
()) { to.add(value
());advance
(); } return to;
public final java.util.Map unfoldTo(java.util.Map to)
init
(); while (more
()) { Map.Entry entry = (Map.Entry)value
(); to.put(entry.getKey(), entry.getValue());advance
(); } return to;
public final java.util.List unfoldTo(java.util.List to)
return (List)unfoldTo
((Collection)to);
public final java.util.Set unfoldTo(java.util.Set to)
return (Set)unfoldTo
((Set)to);
public final java.util.List unfoldRight()
return unfoldRightTo
(new LinkedList());
public final java.util.List unfoldRightTo(java.util.LinkedList to)
init
(); while (more
()) { to.addFirst(value
());advance
(); } return to;
public final java.util.Iterator unfoldAsIterator()
An iterator that generates the same sequence of elements. The returned iterator will not support the remove-operation.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |