|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.ForEach
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 |
public ForEach(java.util.Collection c, ForEach.Filter filter)
c
- the collection on whose elements which the action will be executedfilter
- the filter to applypublic ForEach(java.util.Collection c)
c
- the collection on whose elements which the action will be executedpublic ForEach(java.lang.Object[] array, ForEach.Filter filter)
array
- the array on whose elements which the action will be executedfilter
- the filter to applypublic ForEach(java.lang.Object[] array)
array
- the array on whose elements which the action will be executedpublic ForEach(java.util.Iterator i, ForEach.Filter filter)
i
- an iterator, on whose elements which the action will be executedfilter
- the filter to applypublic ForEach(java.util.Iterator i)
i
- an iterator, on whose elements which the action will be executedMethod Detail |
public void setState(java.lang.Object state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(int state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(byte state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(char state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(long state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(float state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(double state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic void setState(boolean state)
action(Object)
to set the global state of the
cycle.
state
- the value to which to set the statepublic java.lang.Object execute()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic byte executeByte()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic int executeInt()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic short executeShort()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic long executeLong()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic char executeChar()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic boolean executeBoolean()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic float executeFloat()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic double executeDouble()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic java.lang.String executeString()
An action can set the state of the object by using one of the setState()
overloads.
java.lang.IllegalStateException
- if execute has already been invokedpublic void reInit(java.util.Collection c)
public void reInit(java.util.Iterator i)
public void action(java.lang.Object obj)
By default, the object's toString() method is invoked and the result printed on System.out.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |