KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > WindowIterator


1 package org.tigris.scarab.util;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.util.Collections JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.NoSuchElementException JavaDoc;
52
53 public class WindowIterator
54     implements Iterator JavaDoc
55 {
56     public static final WindowIterator EMPTY = new EmptyWindowIterator();
57     private static final Object JavaDoc NOT_INITIALIZED = new Object JavaDoc();
58
59     private final Iterator JavaDoc i;
60     private final Object JavaDoc[] window;
61     private final int bsize;
62     private final int fsize;
63     private final int size;
64     private Boolean JavaDoc hasNext;
65     
66     /**
67      *
68      */

69     public WindowIterator(Iterator JavaDoc i, int backSize, int forwardSize)
70     {
71         this.i = i;
72         this.fsize = Math.abs(forwardSize);
73         this.bsize = Math.abs(backSize);
74         size = fsize + bsize + 1;
75         window = new Object JavaDoc[size];
76
77         for (int m = 0; m < size; m++)
78         {
79             window[m] = NOT_INITIALIZED;
80         }
81     }
82
83     public boolean hasNext()
84     {
85         if (hasNext == null)
86         {
87             hasNext = (internalIterate())
88                 ? Boolean.TRUE : Boolean.FALSE;
89         }
90         return hasNext.booleanValue();
91     }
92
93     boolean firstCall = true;
94     /*
95     int whatsLeft;
96     boolean internalIteratorHadNext = true;
97     private boolean internalIterate()
98     {
99         for (int i = 1; i < size; i++)
100         {
101             window[i-1] = window[i];
102         }
103         window[size-1] = NOT_INITIALIZED;
104
105         boolean hasNext;
106         if (internalIteratorHadNext)
107         {
108             hasNext = i.hasNext();
109             internalIteratorHadNext = hasNext;
110             if (hasNext)
111             {
112                 if (firstCall)
113                 {
114                     for (m = 0; m <= fsize && internalIteratorHadNext; m++)
115                     {
116                         window[bsize + m] = i.next();
117                         internalIteratorHadNext = i.hasNext();
118                         whatsLeft++;
119                     }
120                     firstCall = false;
121                 }
122                 else
123                 {
124                     window[size-1] = i.next();
125                 }
126             }
127         }
128         else
129         {
130             hasNext = (whatsLeft > 0);
131             whatsLeft--;
132         }
133         
134         return hasNext;
135     }
136     */

137
138     private boolean internalIterate()
139     {
140         if (firstCall)
141         {
142             for (int m = 0; m <= fsize && i.hasNext(); m++)
143             {
144                 window[bsize + m] = i.next();
145             }
146             firstCall = false;
147         }
148         else
149         {
150             for (int i = 1; i < size; i++)
151             {
152                 window[i-1] = window[i];
153             }
154
155             if (i.hasNext())
156             {
157                 //System.out.println("WindowIterator: i.hasNext = true");
158
window[size-1] = i.next();
159             }
160             else
161             {
162                 //System.out.println("WindowIterator: i.hasNext = false");
163
window[size-1] = NOT_INITIALIZED;
164             }
165         }
166         
167         return window[bsize] != NOT_INITIALIZED;
168     }
169
170     public Object JavaDoc next()
171     {
172         if (hasNext())
173         {
174             hasNext = null;
175             return window[bsize];
176         }
177         else
178         {
179             throw new NoSuchElementException JavaDoc("Iterator is exhausted"); //EXCEPTION
180
}
181     }
182
183
184     public void remove()
185     {
186         throw new UnsupportedOperationException JavaDoc("'remove' is not implemented"); //EXCEPTION
187
}
188
189     /**
190      * Allows retrieving a given element some distance relative to the element
191      * last returned from next().
192      */

193     public Object JavaDoc get(int i)
194     {
195         if (i < 0 && (-1 * i) > bsize)
196         {
197             throw new ArrayIndexOutOfBoundsException JavaDoc("window was only defined "
198                 + bsize + " in the negative direction. Argument was " + i); //EXCEPTION
199
}
200
201         if (i > 0 && i > fsize)
202         {
203             throw new ArrayIndexOutOfBoundsException JavaDoc("window was only defined "
204                 + fsize + " in the positive direction. Argument was " + i); //EXCEPTION
205
}
206         
207         return window[bsize + i];
208     }
209     
210     public boolean hasValue(int i)
211     {
212         return get(i) != NOT_INITIALIZED;
213     }
214 }
215
216 class EmptyWindowIterator extends WindowIterator
217 {
218     EmptyWindowIterator()
219     {
220         super(Collections.EMPTY_SET.iterator(), 0, 0);
221     }
222
223     public boolean hasNext()
224     {
225         return false;
226     }
227     
228     public Object JavaDoc next()
229     {
230         throw new NoSuchElementException JavaDoc("This is an empty list."); //EXCEPTION
231
}
232     
233     public void remove()
234     {
235         throw new IllegalStateException JavaDoc("next() will throw exception, it is "
236                                         + "not possible to call this method."); //EXCEPTION
237
}
238     
239     public Object JavaDoc get(int i)
240     {
241         throw new NoSuchElementException JavaDoc("This is an empty list."); //EXCEPTION
242
}
243
244     public boolean hasValue(int i)
245     {
246         return false;
247     }
248 }
249
Popular Tags