KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > algorithms > Transform


1 package uk.co.jezuk.mango.algorithms;
2
3 import uk.co.jezuk.mango.UnaryFunction;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * The algorith Transform applies the function <code>fn</code> to
9  * each element in the <code>iterator</code> sequence.
10  * The return value of <code>fn</code> is added to the collection <code>results</code>
11  * If the return value of <code>fn</code> is a collection, then each member of
12  * the return value is added to results.
13  * @version $Id: Transform.java 64 2002-07-31 21:56:02Z jez $
14  */

15 public class Transform
16 {
17   public static Collection JavaDoc execute(Iterator JavaDoc iterator, UnaryFunction fn, Collection JavaDoc results)
18   {
19     if(iterator == null || fn == null || results == null)
20       return results;
21
22     while(iterator.hasNext())
23     {
24       Object JavaDoc o = fn.fn(iterator.next());
25       if(o != null)
26       {
27     if(o instanceof Collection JavaDoc)
28           results.addAll((Collection JavaDoc)o);
29     else
30       results.add(o);
31       } // if(o != null)
32
} // while ...
33
return results;
34   } // execute
35

36   private Transform() {}
37 } // Tranform
38
Popular Tags