KickJava   Java API By Example, From Geeks To Geeks.

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


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.NoSuchElementException JavaDoc;
20
21 import org.apache.commons.collections.ResettableIterator;
22
23 /**
24  * <code>SingletonIterator</code> is an {@link Iterator} over a single
25  * object instance.
26  *
27  * @since Commons Collections 2.0
28  * @version $Revision: 1.14 $ $Date: 2004/04/09 22:52:48 $
29  *
30  * @author James Strachan
31  * @author Stephen Colebourne
32  * @author Rodney Waldhoff
33  */

34 public class SingletonIterator
35         implements Iterator JavaDoc, ResettableIterator {
36
37     /** Whether remove is allowed */
38     private final boolean removeAllowed;
39     /** Is the cursor before the first element */
40     private boolean beforeFirst = true;
41     /** Has the element been removed */
42     private boolean removed = false;
43     /** The object */
44     private Object JavaDoc object;
45
46     /**
47      * Constructs a new <code>SingletonIterator</code> where <code>remove</code>
48      * is a permitted operation.
49      *
50      * @param object the single object to return from the iterator
51      */

52     public SingletonIterator(Object JavaDoc object) {
53         this(object, true);
54     }
55
56     /**
57      * Constructs a new <code>SingletonIterator</code> optionally choosing if
58      * <code>remove</code> is a permitted operation.
59      *
60      * @param object the single object to return from the iterator
61      * @param removeAllowed true if remove is allowed
62      * @since Commons Collections 3.1
63      */

64     public SingletonIterator(Object JavaDoc object, boolean removeAllowed) {
65         super();
66         this.object = object;
67         this.removeAllowed = removeAllowed;
68     }
69
70     //-----------------------------------------------------------------------
71
/**
72      * Is another object available from the iterator?
73      * <p>
74      * This returns true if the single object hasn't been returned yet.
75      *
76      * @return true if the single object hasn't been returned yet
77      */

78     public boolean hasNext() {
79         return (beforeFirst && !removed);
80     }
81
82     /**
83      * Get the next object from the iterator.
84      * <p>
85      * This returns the single object if it hasn't been returned yet.
86      *
87      * @return the single object
88      * @throws NoSuchElementException if the single object has already
89      * been returned
90      */

91     public Object JavaDoc next() {
92         if (!beforeFirst || removed) {
93             throw new NoSuchElementException JavaDoc();
94         }
95         beforeFirst = false;
96         return object;
97     }
98
99     /**
100      * Remove the object from this iterator.
101      *
102      * @throws IllegalStateException if the <tt>next</tt> method has not
103      * yet been called, or the <tt>remove</tt> method has already
104      * been called after the last call to the <tt>next</tt>
105      * method.
106      * @throws UnsupportedOperationException if remove is not supported
107      */

108     public void remove() {
109         if (removeAllowed) {
110             if (removed || beforeFirst) {
111                 throw new IllegalStateException JavaDoc();
112             } else {
113                 object = null;
114                 removed = true;
115             }
116         } else {
117             throw new UnsupportedOperationException JavaDoc();
118         }
119     }
120     
121     /**
122      * Reset the iterator to the start.
123      */

124     public void reset() {
125         beforeFirst = true;
126     }
127     
128 }
129
Popular Tags