1 package uk.co.jezuk.mango; 2 3 import junit.framework.*; 4 5 public class TransformIteratorTest extends TestCase 6 { 7 java.util.List list; 8 9 public TransformIteratorTest(String name) { super(name); } 10 public static Test suite() { return new TestSuite(TransformIteratorTest.class); } 11 12 protected void setUp() 13 { 14 list = new java.util.ArrayList (); 15 for(int i = 0; i < 10; ++i) 16 list.add(new Integer (i)); 17 } 19 class DoubleUp implements UnaryFunction 20 { 21 public Object fn(Object obj) 22 { 23 Integer i = (Integer )obj; 24 return new Integer (i.intValue() * 2); 25 } } 27 28 public void test1() 29 { 30 java.util.Iterator iter = Iterators.TransformIterator(list.iterator(), new DoubleUp()); 31 32 int i = 0; 33 while(iter.hasNext()) 34 { 35 Integer n = (Integer )iter.next(); 36 assertEquals(n.intValue(), i); 37 i += 2; 38 } } 41 class NameObject 42 { 43 public NameObject(String name) { name_ = name; } 44 45 public String getName() { return name_; } 46 47 private String name_; 48 } 50 public void test2() 51 { 52 list = new java.util.ArrayList (); 53 list.add(new NameObject("hawkeye pierce")); 54 list.add(new NameObject("sacremento")); 55 list.add(new NameObject("GOBBLE")); 56 list.add(new NameObject("SINGLETON")); 57 list.add(new NameObject("BILBO")); 58 NameObject theOneIWant = new NameObject("CORGAN"); 59 list.add(theOneIWant); 60 list.add(new NameObject("ERNEST")); 61 list.add(new NameObject("DAVID")); 62 list.add(new NameObject("BILLY")); 63 list.add(new NameObject("SCAGGS")); 64 list.add(new NameObject("CHARLES")); 65 list.add(new NameObject("SIMEON")); 66 67 String found = (String )Algorithms.find(Iterators.TransformIterator(list.iterator(), 69 Adapt.ArgumentMethod("getName")), 70 new String ("CORGAN")); 71 assertEquals("CORGAN", found); 72 } } 75 76 | Popular Tags |