KickJava   Java API By Example, From Geeks To Geeks.

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


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.ListIterator JavaDoc;
19 import java.util.NoSuchElementException JavaDoc;
20
21 import org.apache.commons.collections.ResettableListIterator;
22
23 /**
24  * <code>SingletonIterator</code> is an {@link ListIterator} over a single
25  * object instance.
26  *
27  * @since Commons Collections 2.1
28  * @version $Revision: 1.13 $ $Date: 2004/02/18 00:59:50 $
29  *
30  * @author Stephen Colebourne
31  * @author Rodney Waldhoff
32  */

33 public class SingletonListIterator implements ListIterator JavaDoc, ResettableListIterator {
34
35     private boolean beforeFirst = true;
36     private boolean nextCalled = false;
37     private boolean removed = false;
38     private Object JavaDoc object;
39
40     /**
41      * Constructs a new <code>SingletonListIterator</code>.
42      *
43      * @param object the single object to return from the iterator
44      */

45     public SingletonListIterator(Object JavaDoc object) {
46         super();
47         this.object = object;
48     }
49
50     /**
51      * Is another object available from the iterator?
52      * <p>
53      * This returns true if the single object hasn't been returned yet.
54      *
55      * @return true if the single object hasn't been returned yet
56      */

57     public boolean hasNext() {
58         return beforeFirst && !removed;
59     }
60
61     /**
62      * Is a previous object available from the iterator?
63      * <p>
64      * This returns true if the single object has been returned.
65      *
66      * @return true if the single object has been returned
67      */

68     public boolean hasPrevious() {
69         return !beforeFirst && !removed;
70     }
71
72     /**
73      * Returns the index of the element that would be returned by a subsequent
74      * call to <tt>next</tt>.
75      *
76      * @return 0 or 1 depending on current state.
77      */

78     public int nextIndex() {
79         return (beforeFirst ? 0 : 1);
80     }
81
82     /**
83      * Returns the index of the element that would be returned by a subsequent
84      * call to <tt>previous</tt>. A return value of -1 indicates that the iterator is currently at
85      * the start.
86      *
87      * @return 0 or -1 depending on current state.
88      */

89     public int previousIndex() {
90         return (beforeFirst ? -1 : 0);
91     }
92
93     /**
94      * Get the next object from the iterator.
95      * <p>
96      * This returns the single object if it hasn't been returned yet.
97      *
98      * @return the single object
99      * @throws NoSuchElementException if the single object has already
100      * been returned
101      */

102     public Object JavaDoc next() {
103         if (!beforeFirst || removed) {
104             throw new NoSuchElementException JavaDoc();
105         }
106         beforeFirst = false;
107         nextCalled = true;
108         return object;
109     }
110
111     /**
112      * Get the previous object from the iterator.
113      * <p>
114      * This returns the single object if it has been returned.
115      *
116      * @return the single object
117      * @throws NoSuchElementException if the single object has not already
118      * been returned
119      */

120     public Object JavaDoc previous() {
121         if (beforeFirst || removed) {
122             throw new NoSuchElementException JavaDoc();
123         }
124         beforeFirst = true;
125         return object;
126     }
127
128     /**
129      * Remove the object from this iterator.
130      * @throws IllegalStateException if the <tt>next</tt> or <tt>previous</tt>
131      * method has not yet been called, or the <tt>remove</tt> method
132      * has already been called after the last call to <tt>next</tt>
133      * or <tt>previous</tt>.
134      */

135     public void remove() {
136         if(!nextCalled || removed) {
137             throw new IllegalStateException JavaDoc();
138         } else {
139             object = null;
140             removed = true;
141         }
142     }
143     
144     /**
145      * Add always throws {@link UnsupportedOperationException}.
146      *
147      * @throws UnsupportedOperationException always
148      */

149     public void add(Object JavaDoc obj) {
150         throw new UnsupportedOperationException JavaDoc("add() is not supported by this iterator");
151     }
152     
153     /**
154      * Set sets the value of the singleton.
155      *
156      * @param obj the object to set
157      * @throws IllegalStateException if <tt>next</tt> has not been called
158      * or the object has been removed
159      */

160     public void set(Object JavaDoc obj) {
161         if (!nextCalled || removed) {
162             throw new IllegalStateException JavaDoc();
163         }
164         this.object = obj;
165     }
166     
167     /**
168      * Reset the iterator back to the start.
169      */

170     public void reset() {
171         beforeFirst = true;
172         nextCalled = false;
173     }
174     
175 }
176
Popular Tags