1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.iterators;
18
19 import java.util.Iterator;
20 import java.util.NoSuchElementException;
21
22 import org.apache.commons.collections.ResettableIterator;
23
24 /**
25 * <code>SingletonIterator</code> is an {@link Iterator} over a single
26 * object instance.
27 *
28 * @since 2.0
29 * @version $Id: SingletonIterator.java 1429905 2013-01-07 17:15:14Z ggregory $
30 */
31 public class SingletonIterator<E>
32 implements Iterator<E>, ResettableIterator<E> {
33
34 /** Whether remove is allowed */
35 private final boolean removeAllowed;
36 /** Is the cursor before the first element */
37 private boolean beforeFirst = true;
38 /** Has the element been removed */
39 private boolean removed = false;
40 /** The object */
41 private E object;
42
43 /**
44 * Constructs a new <code>SingletonIterator</code> where <code>remove</code>
45 * is a permitted operation.
46 *
47 * @param object the single object to return from the iterator
48 */
49 public SingletonIterator(final E object) {
50 this(object, true);
51 }
52
53 /**
54 * Constructs a new <code>SingletonIterator</code> optionally choosing if
55 * <code>remove</code> is a permitted operation.
56 *
57 * @param object the single object to return from the iterator
58 * @param removeAllowed true if remove is allowed
59 * @since 3.1
60 */
61 public SingletonIterator(final E object, final boolean removeAllowed) {
62 super();
63 this.object = object;
64 this.removeAllowed = removeAllowed;
65 }
66
67 //-----------------------------------------------------------------------
68 /**
69 * Is another object available from the iterator?
70 * <p>
71 * This returns true if the single object hasn't been returned yet.
72 *
73 * @return true if the single object hasn't been returned yet
74 */
75 public boolean hasNext() {
76 return beforeFirst && !removed;
77 }
78
79 /**
80 * Get the next object from the iterator.
81 * <p>
82 * This returns the single object if it hasn't been returned yet.
83 *
84 * @return the single object
85 * @throws NoSuchElementException if the single object has already
86 * been returned
87 */
88 public E next() {
89 if (!beforeFirst || removed) {
90 throw new NoSuchElementException();
91 }
92 beforeFirst = false;
93 return object;
94 }
95
96 /**
97 * Remove the object from this iterator.
98 *
99 * @throws IllegalStateException if the <tt>next</tt> method has not
100 * yet been called, or the <tt>remove</tt> method has already
101 * been called after the last call to the <tt>next</tt>
102 * method.
103 * @throws UnsupportedOperationException if remove is not supported
104 */
105 public void remove() {
106 if (removeAllowed) {
107 if (removed || beforeFirst) {
108 throw new IllegalStateException();
109 } else {
110 object = null;
111 removed = true;
112 }
113 } else {
114 throw new UnsupportedOperationException();
115 }
116 }
117
118 /**
119 * Reset the iterator to the start.
120 */
121 public void reset() {
122 beforeFirst = true;
123 }
124
125 }