1 25 38 package org.jgrapht.util; 39 40 import java.util.*; 41 42 import junit.framework.*; 43 44 45 public class PrefetchIteratorTest 46 extends TestCase 47 { 48 49 51 public void testIteratorInterface() 52 { 53 Iterator iterator = new IterateFrom1To99(); 54 for (int i = 1; i < 100; i++) { 55 assertEquals(true, iterator.hasNext()); 56 assertEquals(i, iterator.next()); 57 } 58 assertEquals(false, iterator.hasNext()); 59 Exception exceptionThrown = null; 60 try { 61 iterator.next(); 62 } catch (Exception e) { 63 exceptionThrown = e; 64 } 65 assertTrue(exceptionThrown instanceof NoSuchElementException); 66 } 67 68 public void testEnumInterface() 69 { 70 Enumeration enumuration = new IterateFrom1To99(); 71 for (int i = 1; i < 100; i++) { 72 assertEquals(true, enumuration.hasMoreElements()); 73 assertEquals(i, enumuration.nextElement()); 74 } 75 assertEquals(false, enumuration.hasMoreElements()); 76 Exception exceptionThrown = null; 77 try { 78 enumuration.nextElement(); 79 } catch (Exception e) { 80 exceptionThrown = e; 81 } 82 assertTrue(exceptionThrown instanceof NoSuchElementException); 83 } 84 85 87 public static class IterateFrom1To99 89 implements Enumeration, Iterator 90 { 91 private int counter = 0; 92 private PrefetchIterator nextSupplier; 93 94 public IterateFrom1To99() 95 { 96 nextSupplier = 97 new PrefetchIterator<Integer >( 98 new PrefetchIterator.NextElementFunctor<Integer >() { 99 public Integer nextElement() 100 throws NoSuchElementException 101 { 102 counter++; 103 if (counter >= 100) { 104 throw new NoSuchElementException(); 105 } else { 106 return new Integer (counter); 107 } 108 } 109 }); 110 } 111 112 public boolean hasMoreElements() 114 { 115 return this.nextSupplier.hasMoreElements(); 116 } 117 118 public Object nextElement() 120 { 121 return this.nextSupplier.nextElement(); 122 } 123 124 public Object next() 125 { 126 return this.nextSupplier.next(); 127 } 128 129 public boolean hasNext() 130 { 131 return this.nextSupplier.hasNext(); 132 } 133 134 public void remove() 135 { 136 this.nextSupplier.remove(); 137 } 138 } 139 } 140 | Popular Tags |