org.openbp.common.util.iterator.CascadeIterator
This class provides an iterator over a treelike structure. It does this by using the Composite-pattern: a component (node) can either be composite (map or a collection) or a leaf (everything else). A composite contains components itself (again either comosites or leaves) and so on, forming a treelike structure with leaves at the ends.
This class creates an iterator over all LEAVES of such a structure. An optional second parameter (keyOrValue) specifies whether the iterator should return keys or values of maps.
KeyOrValue can either be KEY, VALUE, or a positive integer.
KEY:
The iterator traverses all keys of maps (which usually means it does not go beyond the first map/collection encountered)
VALUE (Default):
the iterator traverses all leaves of the structure (theoretically to any depth)\n
positive Integer n:
This is somewhat tricky. The class traverses the first n levels of the structure for values, and if an element on the n+1'th level is a map/collection, it returns it's keys (see example). Example:
@code 3Let's consider a structure of three chained map/collection-levels: top | ------------------------------------------------------------------------- | | | key1 key2 key3 hash1 hash2 hash3 | | | ------------------ --------------------------------------- --------------- | | | | | | | k11 k12 k21 k22 k23 k31 k32 h11 h12 h21 h22 h23 h31 h32 | | | | | | | ------------ | ----------- ---------------- ---- ------------ | | | | | | | | | | | | | k111 k112 k121 k211 k212 k221 k222 k223 k231 k311 k312 k321 v111 v112 v121 v211 v212 v221 v222 h223 v231 v311 v312 v321 | --------------------- | | k2231 k2232 v2231 v2232 (key/k means a String, hash/h a map/collection and v a value (an object), entries are written as KEY over VALUE) Same results (with varying keyOrValue's, syntax: new CascadeIterator (top, koV): VALUE: (v111, v112, v121, v211, v212, v221, v222, v2231, v2232, v231, v311, v312, v321) KEY: (key1, key2, key3) 1: (k11, k12, k21, k22, k23, k31, k32) 2: (k111, k112, k121, k211, k212, k221, k222, k223, k231, k311, k312, k321) 3: (v111, v112, v121, v211, v212, v221, v222, k2231, k2232, v231, v311, v312, v321) >3: same as values
@code As you see, using the positive-integer syntax with a non uniform structure can lead to quite unexpected (but predictable nonetheless) results.
@author Stephan Moritz