KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > ListIteratorWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.iterators;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.ListIterator JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * As the wrapped Iterator is traversed, ListIteratorWrapper
25  * builds a LinkedList of its values, permitting all required
26  * operations of ListIterator.
27  *
28  * @since Commons Collections 2.1
29  * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
30  *
31  * @author Morgan Delagrange
32  * @author Stephen Colebourne
33  */

34 public class ListIteratorWrapper implements ListIterator JavaDoc {
35
36     /** Holds value of property "iterator" */
37     private final Iterator JavaDoc iterator;
38     private final LinkedList JavaDoc list = new LinkedList JavaDoc();
39     
40     // position of this iterator
41
private int currentIndex = 0;
42     // position of the wrapped iterator
43
// this Iterator should only be used to populate the list
44
private int wrappedIteratorIndex = 0;
45
46     private static final String JavaDoc UNSUPPORTED_OPERATION_MESSAGE =
47         "ListIteratorWrapper does not support optional operations of ListIterator.";
48
49     // Constructor
50
//-------------------------------------------------------------------------
51

52     /**
53      * Constructs a new <code>ListIteratorWrapper</code> that will wrap
54      * the given iterator.
55      *
56      * @param iterator the iterator to wrap
57      * @throws NullPointerException if the iterator is null
58      */

59     public ListIteratorWrapper(Iterator JavaDoc iterator) {
60         super();
61         if (iterator == null) {
62             throw new NullPointerException JavaDoc("Iterator must not be null");
63         }
64         this.iterator = iterator;
65     }
66
67     // ListIterator interface
68
//-------------------------------------------------------------------------
69

70     /**
71      * Throws {@link UnsupportedOperationException}.
72      *
73      * @param o ignored
74      * @throws UnsupportedOperationException always
75      */

76     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
77         throw new UnsupportedOperationException JavaDoc(UNSUPPORTED_OPERATION_MESSAGE);
78     }
79
80
81     /**
82      * Returns true if there are more elements in the iterator.
83      *
84      * @return true if there are more elements
85      */

86     public boolean hasNext() {
87         if (currentIndex == wrappedIteratorIndex) {
88             return iterator.hasNext();
89         }
90
91         return true;
92     }
93
94     /**
95      * Returns true if there are previous elements in the iterator.
96      *
97      * @return true if there are previous elements
98      */

99     public boolean hasPrevious() {
100         if (currentIndex == 0) {
101             return false;
102         }
103
104         return true;
105     }
106
107     /**
108      * Returns the next element from the iterator.
109      *
110      * @return the next element from the iterator
111      * @throws NoSuchElementException if there are no more elements
112      */

113     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
114         if (currentIndex < wrappedIteratorIndex) {
115             ++currentIndex;
116             return list.get(currentIndex - 1);
117         }
118
119         Object JavaDoc retval = iterator.next();
120         list.add(retval);
121         ++currentIndex;
122         ++wrappedIteratorIndex;
123         return retval;
124     }
125
126     /**
127      * Returns in the index of the next element.
128      *
129      * @return the index of the next element
130      */

131     public int nextIndex() {
132         return currentIndex;
133     }
134
135     /**
136      * Returns the the previous element.
137      *
138      * @return the previous element
139      * @throws NoSuchElementException if there are no previous elements
140      */

141     public Object JavaDoc previous() throws NoSuchElementException JavaDoc {
142         if (currentIndex == 0) {
143             throw new NoSuchElementException JavaDoc();
144         }
145
146         --currentIndex;
147         return list.get(currentIndex);
148     }
149
150     /**
151      * Returns the index of the previous element.
152      *
153      * @return the index of the previous element
154      */

155     public int previousIndex() {
156         return currentIndex - 1;
157     }
158
159     /**
160      * Throws {@link UnsupportedOperationException}.
161      *
162      * @throws UnsupportedOperationException always
163      */

164     public void remove() throws UnsupportedOperationException JavaDoc {
165         throw new UnsupportedOperationException JavaDoc(UNSUPPORTED_OPERATION_MESSAGE);
166     }
167
168     /**
169      * Throws {@link UnsupportedOperationException}.
170      *
171      * @param o ignored
172      * @throws UnsupportedOperationException always
173      */

174     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
175         throw new UnsupportedOperationException JavaDoc(UNSUPPORTED_OPERATION_MESSAGE);
176     }
177
178 }
179
180
Popular Tags