01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2006 Robert Grimm
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public License
07: * version 2.1 as published by the Free Software Foundation.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.util;
20:
21: import java.util.Iterator;
22: import java.util.NoSuchElementException;
23:
24: /**
25: * An iterator over one element.
26: *
27: * @author Robert Grimm
28: * @version $Revision: 1.3 $
29: */
30: public class SingletonIterator<T> implements Iterator<T> {
31:
32: /** The flag for whether the element is available. */
33: private boolean available;
34:
35: /** The singleton element. */
36: private T element;
37:
38: /**
39: * Create a new singleton iterator.
40: *
41: * @param element The element.
42: */
43: public SingletonIterator(T element) {
44: this .element = element;
45: available = true;
46: }
47:
48: public boolean hasNext() {
49: return available;
50: }
51:
52: public T next() {
53: if (available) {
54: available = false;
55: return element;
56: } else {
57: throw new NoSuchElementException();
58: }
59: }
60:
61: public void remove() {
62: throw new UnsupportedOperationException();
63: }
64:
65: }
|