org.sadun.util
Class ForEach

java.lang.Object
  extended byorg.sadun.util.ForEach

public class ForEach
extends java.lang.Object

Execute the same action for each object in a collection, optionally filtering the objects by the given filter.

By default, ForEach doesn't do anything, so the typical way to employ ForEach is by subclassing anonymously overriding the method action() does exactly this).

The class also carries a state, initially null (or zero). The state can be set with any of the setState() overloads and is returned by any one of the execute(),executeBoolean(),executeByte(),executeChar(),executeDouble(), executeFloat(),executeInt(),executeLong(),executeShort(),executeString() methods.

For example

 List list;
  ...create and populate a list of lists...
 if(
    new ForEach(l) {
      public void action(Object obj) {
         if ( ((List)obj).contains("mystring")) setState(true);
	    }
    }.executeBoolean() == true) System.out.println("'mystring' found");
 
prints out 'mystring found' if a given list in a list of lists contains the string "mystring".


Nested Class Summary
static interface ForEach.Filter
          Users can implement this interface (anonymously or via a named class) to apply the action only to a subset of the objects in the collection.
 
Constructor Summary
ForEach(java.util.Collection c)
          Creates a ForEach object applying to all the elements of the given collection
ForEach(java.util.Collection c, ForEach.Filter filter)
          Creates a ForEach object applying to the elements of the given collection satisfying the given filter
ForEach(java.util.Iterator i)
          Creates a ForEach object applying to all the elements of the given iterator
ForEach(java.util.Iterator i, ForEach.Filter filter)
          Creates a ForEach object applying to the elements of the given iterator satisfying the given filter
ForEach(java.lang.Object[] array)
          Creates a ForEach object applying to all the elements of the given array
ForEach(java.lang.Object[] array, ForEach.Filter filter)
          Creates a ForEach object applying to the elements of the given array satisfying the given filter
 
Method Summary
 void action(java.lang.Object obj)
          This method must be overridden by a subclass to define the actual action.
 java.lang.Object execute()
          Executes the action of the elements of the collection/iterator.
 boolean executeBoolean()
          Executes the action of the elements of the collection/iterator.
 byte executeByte()
          Executes the action of the elements of the collection/iterator.
 char executeChar()
          Executes the action of the elements of the collection/iterator.
 double executeDouble()
          Executes the action of the elements of the collection/iterator.
 float executeFloat()
          Executes the action of the elements of the collection/iterator.
 int executeInt()
          Executes the action of the elements of the collection/iterator.
 long executeLong()
          Executes the action of the elements of the collection/iterator.
 short executeShort()
          Executes the action of the elements of the collection/iterator.
 java.lang.String executeString()
          Executes the action of the elements of the collection/iterator.
 void reInit(java.util.Collection c)
          Re-initializes the ForEach object on the given collection
 void reInit(java.util.Iterator i)
          Re-initializes the ForEach object on the given iterator
 void setState(boolean state)
          Set the object state.
 void setState(byte state)
          Set the object state.
 void setState(char state)
          Set the object state.
 void setState(double state)
          Set the object state.
 void setState(float state)
          Set the object state.
 void setState(int state)
          Set the object state.
 void setState(long state)
          Set the object state.
 void setState(java.lang.Object state)
          Set the object state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ForEach

public ForEach(java.util.Collection c,
               ForEach.Filter filter)
Creates a ForEach object applying to the elements of the given collection satisfying the given filter

Parameters:
c - the collection on whose elements which the action will be executed
filter - the filter to apply

ForEach

public ForEach(java.util.Collection c)
Creates a ForEach object applying to all the elements of the given collection

Parameters:
c - the collection on whose elements which the action will be executed

ForEach

public ForEach(java.lang.Object[] array,
               ForEach.Filter filter)
Creates a ForEach object applying to the elements of the given array satisfying the given filter

Parameters:
array - the array on whose elements which the action will be executed
filter - the filter to apply

ForEach

public ForEach(java.lang.Object[] array)
Creates a ForEach object applying to all the elements of the given array

Parameters:
array - the array on whose elements which the action will be executed

ForEach

public ForEach(java.util.Iterator i,
               ForEach.Filter filter)
Creates a ForEach object applying to the elements of the given iterator satisfying the given filter

Parameters:
i - an iterator, on whose elements which the action will be executed
filter - the filter to apply

ForEach

public ForEach(java.util.Iterator i)
Creates a ForEach object applying to all the elements of the given iterator

Parameters:
i - an iterator, on whose elements which the action will be executed
Method Detail

setState

public void setState(java.lang.Object state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(int state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(byte state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(char state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(long state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(float state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(double state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

setState

public void setState(boolean state)
Set the object state. This method can be invoked within action(Object) to set the global state of the cycle.

Parameters:
state - the value to which to set the state

execute

public java.lang.Object execute()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state object
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeByte

public byte executeByte()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeInt

public int executeInt()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeShort

public short executeShort()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeLong

public long executeLong()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeChar

public char executeChar()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeBoolean

public boolean executeBoolean()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeFloat

public float executeFloat()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeDouble

public double executeDouble()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

executeString

public java.lang.String executeString()
Executes the action of the elements of the collection/iterator. If a filter has been defined, the action is executed only on the elements satisfying the filter.

An action can set the state of the object by using one of the setState() overloads.

Returns:
the resulting state
Throws:
java.lang.IllegalStateException - if execute has already been invoked

reInit

public void reInit(java.util.Collection c)
Re-initializes the ForEach object on the given collection


reInit

public void reInit(java.util.Iterator i)
Re-initializes the ForEach object on the given iterator


action

public void action(java.lang.Object obj)
This method must be overridden by a subclass to define the actual action.

By default, the object's toString() method is invoked and the result printed on System.out.