KickJava   Java API By Example, From Geeks To Geeks.

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


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 SubsetIterator
54     implements Iterator JavaDoc
55 {
56     public static final SubsetIterator EMPTY = new EmptySubsetIterator();
57
58     private Iterator JavaDoc i;
59     private final int offset;
60     private final int elements;
61     private int currentPosition;
62     
63     /**
64      * Constructs an itarator on a subset of the given iterator.
65      * The SubsetIterator starts with the offset-element and iterates
66      * over <code>elements</code> elements
67      *
68      * @param i the Iterator to subset
69      * @param offset the position of the first element of the subset
70      * @param element the number of elements the subset should have.
71      */

72     public SubsetIterator(Iterator JavaDoc i, int offset, int elements)
73     {
74      
75         this.i = i;
76         this.offset = offset;
77         this.elements = elements;
78         currentPosition = 0;
79     }
80
81     /**
82      * Constructs an itarator on a subset of the given iterator.
83      * The SubsetIterator starts with the offset-element and iterates
84      * over all elements
85      *
86      * @param i the Iterator to subset
87      * @param offset the position of the first element of the subset
88      */

89     public SubsetIterator(Iterator JavaDoc i, int offset)
90     {
91         this(i, offset, Integer.MAX_VALUE);
92     }
93     
94     private Boolean JavaDoc hasNext;
95     public boolean hasNext()
96     {
97         if (hasNext == null)
98         {
99             hasNext = (internalIterate())
100                 ? Boolean.TRUE : Boolean.FALSE;
101         }
102         return hasNext.booleanValue();
103     }
104
105     boolean firstCall = true;
106     private boolean internalIterate()
107     {
108         if (firstCall)
109         {
110             for (int m = 0; i.hasNext() && m < offset; m++)
111             {
112                 i.next();
113             }
114             firstCall = false;
115         }
116         currentPosition++;
117         return i.hasNext() && currentPosition <= elements;
118     }
119
120     public Object JavaDoc next()
121     {
122         if (hasNext())
123         {
124             hasNext = null;
125             return i.next();
126         }
127         else
128         {
129             throw new NoSuchElementException JavaDoc("Iterator is exhausted"); //EXCEPTION
130
}
131     }
132
133     public void remove()
134     {
135         i.remove();
136     }
137
138 }
139
140 class EmptySubsetIterator extends SubsetIterator
141 {
142     EmptySubsetIterator()
143     {
144         super(Collections.EMPTY_SET.iterator(), 0, 0);
145     }
146     
147     public boolean hasNext()
148     {
149         return false;
150     }
151     
152     public Object JavaDoc next()
153     {
154         throw new NoSuchElementException JavaDoc("This is an empty list."); //EXCEPTION
155
}
156     
157     public void remove()
158     {
159         throw new IllegalStateException JavaDoc("next() will throw exception, it is "
160                                         + "not possible to call this method."); //EXCEPTION
161
}
162 }
163
Popular Tags