View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.util.Enumeration;
23  import java.util.Iterator;
24  
25  
26  /**
27   * A simple implementation of both the <code>java.util.Enumeration</code> and
28   * <code>java.util.Iterator</code> that iterates over the array of type
29   * <code>Object[]</code>. Removal of elements is not permitted. The iteration
30   * is not thread safe.
31   *
32   * @author <a href="mailto:gasper.tkacik@cosylab.com">Gasper Tkacik</a>
33   * @version $id$
34   */
35  public class ArrayEnumeration implements Enumeration, Iterator
36  {
37  	/** The array storing the data over which this iterator will iterate. */
38  	protected Object[] array = null;
39  	private int ix = 0;
40  
41  	/**
42  	 * Creates a new instance of array enumeration, given the array over which
43  	 * this instance will iterate.
44  	 *
45  	 * @param array array of iteration, non-<code>null</code>
46  	 */
47  	public ArrayEnumeration(Object[] array)
48  	{
49  		assert (array != null);
50  		this.array = array;
51  	}
52  
53  	/**
54  	 * Returns <code>true</code> iff calling <code>nextElement()</code> will
55  	 * return a valid value.
56  	 *
57  	 * @return <code>true</code> if there are more elements to be iterated over
58  	 *         in the array
59  	 */
60  	public boolean hasMoreElements()
61  	{
62          return ix != array.length;
63  	}
64  
65  	/**
66  	 * Returns the next element in the array. If this method is called after
67  	 * the end of the array has been reached (when
68  	 * <code>hasMoreElements()</code> returns <code>false</code>, an array
69  	 * index out of bounds exception will be thrown.
70  	 *
71  	 * @return the next object in the array, can be <code>null</code>
72  	 */
73  	public Object nextElement()
74  	{
75  		Object retVal = array[ix];
76  		ix++;
77  
78  		return retVal;
79  	}
80  
81  	/**
82  	 * A shorthand for <code>hasMoreElements()</code>
83  	 *
84  	 * @return <code>true</code> iff this array has more elements to be
85  	 *         iterated over
86  	 */
87  	public boolean hasNext()
88  	{
89  		return hasMoreElements();
90  	}
91  
92  	/**
93  	 * A shorthand for <code>nextElement()</code>.
94  	 *
95  	 * @return the next element in the array
96  	 */
97  	public Object next()
98  	{
99  		return nextElement();
100 	}
101 
102 	/**
103 	 * Not implemented.
104 	 *
105 	 * @throws UnsupportedOperationException always
106 	 */
107 	public void remove()
108 	{
109 		throw new UnsupportedOperationException();
110 	}
111 }
112 
113 /* __oOo__ */