org.openbp.common.util.iterator
Class CascadeIterator

java.lang.Object
  extended by org.openbp.common.util.iterator.CascadeIterator
All Implemented Interfaces:
java.util.Iterator

public class CascadeIterator
extends java.lang.Object
implements java.util.Iterator

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:

Author:
Stephan Moritz

Field Summary
static int KEY
          Iterate through keys.
static int VALUE
          Iterate through values.
 
Constructor Summary
CascadeIterator()
          Default constructor
 
Method Summary
protected  java.util.Iterator getCustomIterator(java.lang.Object obj, int modus)
          This method checks if the object is of a known type that can be iterated.
protected  java.util.Iterator getIterator(java.lang.Object next, int modus)
          This static method returns an iterator over any given object.
 boolean hasNext()
          Returns true if there are elements left.
protected  CascadeIterator init(java.lang.Object target)
          Creates an iterator over the given object.
protected  CascadeIterator init(java.lang.Object[] targets)
          Creates an iterator over an array of Objects, chaining their values together.
protected  CascadeIterator init(java.lang.Object[] targets, int keyOrValue)
          Creates an iterator over an array of Objects, chaining their values together.
protected  CascadeIterator init(java.lang.Object target, int keyOrValue)
          Creates an iterator over the given object.
 java.util.Iterator iterator(java.lang.Object obj)
          Convenience method for iterating values (as opposed to keys).
 java.util.Iterator iterator(java.lang.Object[] objs, int modus)
          Iterate over a given object-array.
 java.util.Iterator iterator(java.lang.Object obj, int modus)
          Iterate over a given object.
static void main(java.lang.String[] args)
          Main method for test.
 java.lang.Object next()
          Returns the next Element of the iterator and advances the internal counter.
 void remove()
          Always throws an UnsupportedOperationException: removal is not supported by this iterator.
protected  void stepAhead()
          Internally used method to advance the pointer to the next element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY

public static final int KEY
Iterate through keys.

See Also:
Constant Field Values

VALUE

public static final int VALUE
Iterate through values.

See Also:
Constant Field Values
Constructor Detail

CascadeIterator

public CascadeIterator()
Default constructor

Method Detail

init

protected CascadeIterator init(java.lang.Object target)
Creates an iterator over the given object. (Target CAN be a single leaf, in that case a singleton iterator is returned) This traverses VALUES of maps.

Parameters:
target - The object to iterate
Returns:
The iterator

init

protected CascadeIterator init(java.lang.Object target,
                               int keyOrValue)
Creates an iterator over the given object. (Target CAN be a single leaf, in that case a singleton iterator is returned) This traverses VALUES or KEYS of maps.

Parameters:
target - The object to iterate
keyOrValue - Whether keys or values of maps should be traversed
Returns:
The iterator

init

protected CascadeIterator init(java.lang.Object[] targets)
Creates an iterator over an array of Objects, chaining their values together. This constructor returns values in maps (default)

Parameters:
targets - The object-array
Returns:
The iterator

init

protected CascadeIterator init(java.lang.Object[] targets,
                               int keyOrValue)
Creates an iterator over an array of Objects, chaining their values together. This constructor returns either values or keys in maps.

Parameters:
targets - the object-array
keyOrValue - Return keys or values in hastables
Returns:
The iterator

hasNext

public boolean hasNext()
Returns true if there are elements left.

Specified by:
hasNext in interface java.util.Iterator
Returns:
true: There are objects left.
false: Otherwise.

next

public java.lang.Object next()
Returns the next Element of the iterator and advances the internal counter.

Specified by:
next in interface java.util.Iterator
Returns:
The next element

remove

public void remove()
Always throws an UnsupportedOperationException: removal is not supported by this iterator.

Specified by:
remove in interface java.util.Iterator

stepAhead

protected void stepAhead()
Internally used method to advance the pointer to the next element.


getIterator

protected java.util.Iterator getIterator(java.lang.Object next,
                                         int modus)
This static method returns an iterator over any given object. if the object is either a hastable or a collection it returns a CascadeIterator, else a singleton iterator.
Do not overwrite this method in order to integrate custom types, overwrite getCustomIterator () instead.

Parameters:
next - The object to iterator
modus - Return keys (KEY) or values (VALUE) in maps
Returns:
The iterator

getCustomIterator

protected java.util.Iterator getCustomIterator(java.lang.Object obj,
                                               int modus)
This method checks if the object is of a known type that can be iterated. In order to add custom types, overwrite this method, what should look like:

Parameters:
obj - The object to iterate
modus - The modus (keyOrValue for maps)
Returns:
The iterator

iterator

public java.util.Iterator iterator(java.lang.Object obj)
Convenience method for iterating values (as opposed to keys).

Parameters:
obj - The object to iterate
Returns:
The iterator

iterator

public java.util.Iterator iterator(java.lang.Object obj,
                                   int modus)
Iterate over a given object.

Parameters:
obj - The object to iterate
modus - The modus (KEY, VALUE or a positive integer, see above)
Returns:
The iterator

iterator

public java.util.Iterator iterator(java.lang.Object[] objs,
                                   int modus)
Iterate over a given object-array.

Parameters:
objs - The object-array to iterate
modus - The modus (KEY, VALUE or a positive integer, see above)
Returns:
The iterator

main

public static void main(java.lang.String[] args)
Main method for test.

Parameters:
args - Argument vector; No arguments needed


Copyright © 2011. All Rights Reserved.