KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > TransformTest


1 package uk.co.jezuk.mango;
2
3 import junit.framework.*;
4 import java.util.List JavaDoc;
5 import java.util.ArrayList JavaDoc;
6
7 public class TransformTest extends TestCase
8 {
9   public TransformTest(String JavaDoc name)
10   {
11     super(name);
12   } // TransformTest
13

14   public static Test suite()
15   {
16     return new TestSuite(TransformTest.class);
17   } // suite
18

19   public void test1()
20   {
21     ArrayList JavaDoc in = new ArrayList JavaDoc();
22     in.add(new Integer JavaDoc(1));
23     in.add(new Integer JavaDoc(2));
24     in.add(new Integer JavaDoc(3));
25     List JavaDoc out = (List JavaDoc)Algorithms.transform(in.iterator(), new Square(), new ArrayList JavaDoc());
26     assertEquals(3, out.size());
27     assertEquals(1, ((Integer JavaDoc)out.get(0)).intValue());
28     assertEquals(4, ((Integer JavaDoc)out.get(1)).intValue());
29     assertEquals(9, ((Integer JavaDoc)out.get(2)).intValue());
30   } // test1
31

32   public void test2()
33   {
34     ArrayList JavaDoc in = new ArrayList JavaDoc();
35     in.add("A");
36     in.add("B");
37     in.add("C");
38     List JavaDoc out = (List JavaDoc)Algorithms.transform(in.iterator(), new Duplicator(), new ArrayList JavaDoc());
39     assertEquals(6, out.size());
40     assertEquals("A", out.get(0));
41     assertEquals("A", out.get(1));
42     assertEquals("B", out.get(2));
43     assertEquals("B", out.get(3));
44     assertEquals("C", out.get(4));
45     assertEquals("C", out.get(5));
46   } // test2
47

48   public class Square implements UnaryFunction
49   {
50     public Object JavaDoc fn(Object JavaDoc x)
51     {
52       if(x instanceof Integer JavaDoc)
53     return new Integer JavaDoc(((Integer JavaDoc)x).intValue() * ((Integer JavaDoc)x).intValue());
54       return null;
55     } // fn
56
} // Square
57

58   public class Duplicator implements UnaryFunction
59   {
60     public Object JavaDoc fn(Object JavaDoc x)
61     {
62       if(x instanceof String JavaDoc)
63       {
64         ArrayList JavaDoc ret = new ArrayList JavaDoc();
65         ret.add(new String JavaDoc((String JavaDoc)x));
66         ret.add(new String JavaDoc((String JavaDoc)x));
67         return ret;
68       } // if ...
69
return null;
70     } // fn
71
} // Duplicator
72
} // TransformTest
73

74
75
Popular Tags