KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > util > PrefetchIteratorTest


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * PrefetchIteratorTest.java
27  * -----------------
28  * (C) Copyright 2005-2006, by Assaf Lehr and Contributors.
29  *
30  * Original Author: Assaf Lehr
31  * Contributor(s): -
32  *
33  * $Id: PrefetchIteratorTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  */

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     //~ Methods ---------------------------------------------------------------
50

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 JavaDoc exceptionThrown = null;
60         try {
61             iterator.next();
62         } catch (Exception JavaDoc 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 JavaDoc exceptionThrown = null;
77         try {
78             enumuration.nextElement();
79         } catch (Exception JavaDoc e) {
80             exceptionThrown = e;
81         }
82         assertTrue(exceptionThrown instanceof NoSuchElementException);
83     }
84
85     //~ Inner Classes ---------------------------------------------------------
86

87     // This test class supplies enumeration of integer from 1 till 100.
88
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 JavaDoc>(
98                     new PrefetchIterator.NextElementFunctor<Integer JavaDoc>() {
99                         public Integer JavaDoc nextElement()
100                             throws NoSuchElementException
101                         {
102                             counter++;
103                             if (counter >= 100) {
104                                 throw new NoSuchElementException();
105                             } else {
106                                 return new Integer JavaDoc(counter);
107                             }
108                         }
109                     });
110         }
111
112         // forwarding to nextSupplier and return its returned value
113
public boolean hasMoreElements()
114         {
115             return this.nextSupplier.hasMoreElements();
116         }
117
118         // forwarding to nextSupplier and return its returned value
119
public Object JavaDoc nextElement()
120         {
121             return this.nextSupplier.nextElement();
122         }
123
124         public Object JavaDoc 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