01: /**
02: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */package nz.org.take.rt;
10:
11: /**
12: * Singleton iterator. This iterator index' a "Collection" containing only one single
13: * object. This object can be accessed only one single time.
14: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
15: * @param <T> the type of the iterated element
16: */
17: public class SingletonIterator<T> extends AbstractIterator<T> {
18: private T object = null;
19:
20: /**
21: * Construct a new <code>SingletonIterator</code> indexing the provided object.
22: * @param object the element indexed by this Iterator.
23: */
24: public SingletonIterator(T object) {
25: super ();
26: assert (object != null);
27: this .object = object;
28: }
29:
30: /* (non-Javadoc)
31: * @see java.util.Iterator#hasNext()
32: */
33: public boolean hasNext() {
34: return object != null;
35: }
36:
37: /* (non-Javadoc)
38: * @see java.util.Iterator#next()
39: */
40: public T next() {
41: T next = object;
42: object = null;
43: return next;
44: }
45:
46: /* (non-Javadoc)
47: * @see org.mandarax.compiler.rt.ResourceIterator#close()
48: */
49: public void close() {
50: // nothing to do here
51: }
52:
53: }
|