edu.ull.cgunay.utils
Class Iteration

java.lang.Object
  |
  +--edu.ull.cgunay.utils.Iteration
All Implemented Interfaces:
Serializable, Task
Direct Known Subclasses:
UninterruptedIteration

public abstract class Iteration
extends Object
implements Task

Allows iterating on the Collections of JAVA 2 SDK with a high level interface. In summary, a given Task is executed for all items in a given Collection when an appropriate loop() method is invoked.

This scheme allows generalizing commonly used iterations in template classes. This is a vast improvement allowing object oriented control over the primitive looping constructs for and while.

This file contains alternative methods for iteration. The collection on which to iterate can be chosen either at the constructor or at the loop() function for the non-static use. It is left to the user to decide if the class is made to iterate on a single collection or if it is rather a generic iteration facility that can be applied to many collections at different times.

On the other hand, static methods allow iterating without requiring an instance of this class. However, in this case an instance of the Task class has to be provided in calls to the loop(Collection,Task) method. See the examples below for details on use.

Note: The loop() methods of this class throws BreakOutOfIterationException to enable the user to have control on the iteration. If this is undesired, the subclass UninterruptedIteration should be used instead.

Example of static use:

 Iteration.loop(collection, new Task() {
   public void job(Object o) {
     // do something on o here
   }
 });
 

Example of non-static repeatedly use of same task on different collections:

 new Iteration() {
   public void job(Object o) {
     // do something on o here
   }
 }.loop(collection);
 

Example of non-static repeatedly use of same task with same collection:

 new Iteration(collection) {
   public void job(Object o) {
     // do something on o here
   }
 }.loop();
 

Created: Sat Nov 25 04:18:32 2000

Version:
$Revision: 1.10 $
Author:
Cengiz Gunay
See Also:
Collection, Iterator, Task, Serialized Form

Field Summary
(package private)  Collection collection
          Collection to be iterated on for non-static loop() function.
 
Constructor Summary
Iteration()
          Dummy.
Iteration(Collection collection)
          Sets the collection to be iterated on (for the non-static loop()).
 
Method Summary
 void loop()
          Iterates on collection set by the constructor.
 void loop(Collection c)
          Iterates on c with the Task given in this class.
static void loop(Collection c, Task t)
          Convenience method with Collection parameter, calls another loop() method.
static void loop(Iterator i, Task t)
          Loop on Iterator values calling Task.
 void loop(Object[] array)
          Convenience method with Collection parameter, calls another loop() method.
static void loop(Object[] array, Task t)
          Loop on Object[] values calling Task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.ull.cgunay.utils.Task
job
 

Field Detail

collection

Collection collection
Collection to be iterated on for non-static loop() function.

See Also:
loop()
Constructor Detail

Iteration

public Iteration()
Dummy.


Iteration

public Iteration(Collection collection)
Sets the collection to be iterated on (for the non-static loop()).

Parameters:
collection - a Collection value
See Also:
loop()
Method Detail

loop

public void loop()
          throws BreakOutOfIterationException
Iterates on collection set by the constructor.

Throws:
BreakOutOfIterationException - if received from given Task instance
See Also:
Iteration(Collection)

loop

public void loop(Collection c)
          throws BreakOutOfIterationException
Iterates on c with the Task given in this class.

Parameters:
c - a Collection value
Throws:
BreakOutOfIterationException - if received from given Task instance
See Also:
Task, loop()

loop

public static void loop(Iterator i,
                        Task t)
                 throws BreakOutOfIterationException
Loop on Iterator values calling Task.

Parameters:
i - an Iterator value
t - a Task value
Throws:
BreakOutOfIterationException - if received from given Task instance
See Also:
Task.job(java.lang.Object)

loop

public static void loop(Object[] array,
                        Task t)
                 throws BreakOutOfIterationException
Loop on Object[] values calling Task.

Parameters:
array - an Object[] value
t - a Task value
Throws:
BreakOutOfIterationException - if an error occurs

loop

public void loop(Object[] array)
          throws BreakOutOfIterationException
Convenience method with Collection parameter, calls another loop() method.

Parameters:
array - an Object[] value
Throws:
BreakOutOfIterationException - if an error occurs

loop

public static void loop(Collection c,
                        Task t)
                 throws BreakOutOfIterationException
Convenience method with Collection parameter, calls another loop() method.

Parameters:
c - a Collection value
t - a Task value
Throws:
BreakOutOfIterationException - if received from given Task instance
See Also:
loop(Iterator,Task)