1 package uk.co.jezuk.mango; 2 3 import junit.framework.*; 4 import java.util.List ; 5 import java.util.ArrayList ; 6 7 public class TransformTest extends TestCase 8 { 9 public TransformTest(String name) 10 { 11 super(name); 12 } 14 public static Test suite() 15 { 16 return new TestSuite(TransformTest.class); 17 } 19 public void test1() 20 { 21 ArrayList in = new ArrayList (); 22 in.add(new Integer (1)); 23 in.add(new Integer (2)); 24 in.add(new Integer (3)); 25 List out = (List )Algorithms.transform(in.iterator(), new Square(), new ArrayList ()); 26 assertEquals(3, out.size()); 27 assertEquals(1, ((Integer )out.get(0)).intValue()); 28 assertEquals(4, ((Integer )out.get(1)).intValue()); 29 assertEquals(9, ((Integer )out.get(2)).intValue()); 30 } 32 public void test2() 33 { 34 ArrayList in = new ArrayList (); 35 in.add("A"); 36 in.add("B"); 37 in.add("C"); 38 List out = (List )Algorithms.transform(in.iterator(), new Duplicator(), new ArrayList ()); 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 } 48 public class Square implements UnaryFunction 49 { 50 public Object fn(Object x) 51 { 52 if(x instanceof Integer ) 53 return new Integer (((Integer )x).intValue() * ((Integer )x).intValue()); 54 return null; 55 } } 58 public class Duplicator implements UnaryFunction 59 { 60 public Object fn(Object x) 61 { 62 if(x instanceof String ) 63 { 64 ArrayList ret = new ArrayList (); 65 ret.add(new String ((String )x)); 66 ret.add(new String ((String )x)); 67 return ret; 68 } return null; 70 } } } 74 75 | Popular Tags |